Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion src/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ export default defineComponent({

const { document, saveService } = provideSaveService(
connection,
editor,
syncService,
serialize,
ydoc,
Expand Down Expand Up @@ -618,8 +619,8 @@ export default defineComponent({
// Save and push unsaved changes from offline editing session.
Promise.all([this.whenSynced, this.editorReady]).then(() => {
if (this.dirty) {
// the update will trigger an autosave
this.syncProvider.sendUpdateFromDoc('offline', this.ydoc)
this.saveService.autosave()
}
})
this.updateUser(session)
Expand Down
18 changes: 13 additions & 5 deletions src/composables/useSaveService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import type { Editor } from '@tiptap/core'
import type { Transaction } from '@tiptap/pm/state'
import type { InjectionKey, ShallowRef } from 'vue'
import type { Doc } from 'yjs'
import type { SaveData } from '../apis/save.ts'
Expand All @@ -18,13 +20,15 @@ const saveServiceKey = Symbol('text:save') as InjectionKey<SaveService>
/**
*
* @param connection to the api
* @param editor the tiptap instance
* @param syncService mostly used for the event bus and events
* @param serialize to extract the document markdown content
* @param ydoc to extract the document state from
* @param setDirty set the dirty state for the editor
*/
export function provideSaveService(
connection: ShallowRef<Connection | undefined>,
editor: Editor,
syncService: SyncService,
serialize: () => string,
ydoc: Doc,
Expand All @@ -49,11 +53,15 @@ export function provideSaveService(
getSaveData,
})

syncService.bus.on('changesPushed', saveService.autosave)
syncService.bus.on('close', saveService.clear)
onUnmounted(() => {
syncService.bus.off('changesPushed', saveService.autosave)
syncService.bus.off('close', saveService.clear)
const autosaveOnUpdate = ({ transaction }: { transaction: Transaction }) => {
if (transaction.getMeta('addToHistory') !== false) {
saveService.autosave()
}
}

editor.on('update', autosaveOnUpdate)
editor.on('destroy', () => {
editor.off('update', autosaveOnUpdate)
})

/**
Expand Down
Loading