Merge pull request #1135 from iamejaaz/add-keyboard-shortcut

feat: add keyboard shortcut to save lesson
This commit is contained in:
Jannat Patel
2024-11-19 21:54:37 +05:30
committed by GitHub

View File

@@ -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" 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" /> <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') }} {{ __('Save') }}
</Button> </Button>
</header> </header>
@@ -94,6 +98,7 @@ const instructorEditor = ref(null)
const user = inject('$user') const user = inject('$user')
const openInstructorEditor = ref(false) const openInstructorEditor = ref(false)
let autoSaveInterval let autoSaveInterval
let showSuccessMessage = false
const props = defineProps({ const props = defineProps({
courseName: { courseName: {
@@ -117,6 +122,7 @@ onMounted(() => {
capture('lesson_form_opened') capture('lesson_form_opened')
editor.value = renderEditor('content') editor.value = renderEditor('content')
instructorEditor.value = renderEditor('instructor-notes') instructorEditor.value = renderEditor('instructor-notes')
window.addEventListener('keydown', keyboardShortcut)
}) })
const renderEditor = (holder) => { const renderEditor = (holder) => {
@@ -186,12 +192,24 @@ const addInstructorNotes = (data) => {
const enableAutoSave = () => { const enableAutoSave = () => {
autoSaveInterval = setInterval(() => { autoSaveInterval = setInterval(() => {
saveLesson() saveLesson({ showSuccessMessage: false })
}, 10000) }, 10000)
} }
const keyboardShortcut = (e) => {
if (
e.key === 's' &&
(e.ctrlKey || e.metaKey) &&
!e.target.classList.contains('ProseMirror')
) {
saveLesson({ showSuccessMessage: true })
e.preventDefault()
}
}
onBeforeUnmount(() => { onBeforeUnmount(() => {
clearInterval(autoSaveInterval) clearInterval(autoSaveInterval)
window.removeEventListener('keydown', keyboardShortcut)
}) })
const newLessonResource = createResource({ const newLessonResource = createResource({
@@ -343,7 +361,11 @@ const convertToJSON = (lessonData) => {
return blocks return blocks
} }
const saveLesson = () => { const saveLesson = (e) => {
showSuccessMessage = false
if (typeof e != 'undefined' && e.showSuccessMessage) {
showSuccessMessage = true
}
editor.value.save().then((outputData) => { editor.value.save().then((outputData) => {
lesson.content = JSON.stringify(outputData) lesson.content = JSON.stringify(outputData)
instructorEditor.value.save().then((outputData) => { instructorEditor.value.save().then((outputData) => {
@@ -392,6 +414,11 @@ const editCurrentLesson = () => {
validate() { validate() {
return validateLesson() return validateLesson()
}, },
onSuccess() {
showSuccessMessage
? showToast('Success', 'Lesson updated successfully', 'check')
: ''
},
onError(err) { onError(err) {
showToast('Error', err.message, 'x') showToast('Error', err.message, 'x')
}, },