Skip to content

Commit

Permalink
feat: add keyboard shortcut to save lesson
Browse files Browse the repository at this point in the history
  • Loading branch information
iamejaaz committed Nov 16, 2024
1 parent dfb22c8 commit a4fa2ef
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions frontend/src/pages/LessonForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
class="sticky top-0 z-10 flex flex-col md:flex-row md:items-center justify-between border-b overflow-hidden bg-white px-3 py-2.5 sm:px-5"
>
<Breadcrumbs class="text-ellipsis" :items="breadcrumbs" />
<Button variant="solid" @click="saveLesson()" class="mt-3 md:mt-0">
<Button
variant="solid"
@click="saveLesson({ showSuccessMessage: true })"
class="mt-3 md:mt-0"
>
{{ __('Save') }}
</Button>
</header>
Expand Down Expand Up @@ -94,6 +98,7 @@ const instructorEditor = ref(null)
const user = inject('$user')
const openInstructorEditor = ref(false)
let autoSaveInterval
let showSuccessMessage = false
const props = defineProps({
courseName: {
Expand All @@ -117,6 +122,7 @@ onMounted(() => {
capture('lesson_form_opened')
editor.value = renderEditor('content')
instructorEditor.value = renderEditor('instructor-notes')
window.addEventListener('keydown', keyboardShortcut)
})
const renderEditor = (holder) => {
Expand Down Expand Up @@ -186,12 +192,24 @@ const addInstructorNotes = (data) => {
const enableAutoSave = () => {
autoSaveInterval = setInterval(() => {
saveLesson()
saveLesson({ showSuccessMessage: false })
}, 10000)
}
const keyboardShortcut = (e) => {
if (
e.key === 's' &&
(e.ctrlKey || e.metaKey) &&
!e.target.classList.contains('ProseMirror')
) {
saveLesson({ showSuccessMessage: true })
e.preventDefault()
}
}
onBeforeUnmount(() => {
clearInterval(autoSaveInterval)
window.removeEventListener('keydown', keyboardShortcut)
})
const newLessonResource = createResource({
Expand Down Expand Up @@ -343,7 +361,11 @@ const convertToJSON = (lessonData) => {
return blocks
}
const saveLesson = () => {
const saveLesson = (e) => {
showSuccessMessage = false
if (typeof e != 'undefined' && e.showSuccessMessage) {
showSuccessMessage = true
}
editor.value.save().then((outputData) => {
lesson.content = JSON.stringify(outputData)
instructorEditor.value.save().then((outputData) => {
Expand Down Expand Up @@ -392,6 +414,11 @@ const editCurrentLesson = () => {
validate() {
return validateLesson()
},
onSuccess() {
showSuccessMessage
? showToast('Success', 'Lesson updated successfully', 'check')
: ''
},
onError(err) {
showToast('Error', err.message, 'x')
},
Expand Down

0 comments on commit a4fa2ef

Please sign in to comment.