Skip to content
Closed
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
20 changes: 14 additions & 6 deletions src/composables/useSaveService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import type { InjectionKey, ShallowRef } from 'vue'
import type { Editor } from '@tiptap/core'
import type { Transaction } from '@tiptap/pm/state'
import type { InjectionKey, Raw, ShallowRef } from 'vue'

Check failure on line 8 in src/composables/useSaveService.ts

View workflow job for this annotation

GitHub Actions / NPM lint

Module '"vue"' has no exported member 'Raw'. Did you mean to use 'import Raw from "vue"' instead?
import type { Doc } from 'yjs'
import type { SaveData } from '../apis/save.ts'
import type { Document, SyncService } from '../services/SyncService.ts'
Expand All @@ -18,13 +20,15 @@
/**
*
* @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: Raw<Editor>,
syncService: SyncService,
serialize: () => string,
ydoc: Doc,
Expand All @@ -49,11 +53,15 @@
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