Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add keyboard shortcut to save lesson #1135

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading