From 577b52f8df20455f5ffaa75cd208fcefd6f788e1 Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 7 Oct 2026 18:38:50 +0200 Subject: [PATCH] fix(autosave): trigger on updates rather than sync * Only autosave on updates that have `addToHistory` meta so automated and remote updates do not trigger autosave. * Decouple autosave from sync. * Trigger autosave explicitely when reconnecting with offline changes. Signed-off-by: Max --- src/components/Editor.vue | 3 ++- src/composables/useSaveService.ts | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/components/Editor.vue b/src/components/Editor.vue index 52542786bba..1e50bc48dcc 100644 --- a/src/components/Editor.vue +++ b/src/components/Editor.vue @@ -296,6 +296,7 @@ export default defineComponent({ const { document, saveService } = provideSaveService( connection, + editor, syncService, serialize, ydoc, @@ -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) diff --git a/src/composables/useSaveService.ts b/src/composables/useSaveService.ts index 10015c6577e..d678fc87aa2 100644 --- a/src/composables/useSaveService.ts +++ b/src/composables/useSaveService.ts @@ -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' @@ -18,6 +20,7 @@ const saveServiceKey = Symbol('text:save') as InjectionKey /** * * @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 @@ -25,6 +28,7 @@ const saveServiceKey = Symbol('text:save') as InjectionKey */ export function provideSaveService( connection: ShallowRef, + editor: Editor, syncService: SyncService, serialize: () => string, ydoc: Doc, @@ -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) }) /**