From 4e291b889b47e69934e53f7022a178aa25e12581 Mon Sep 17 00:00:00 2001 From: landaiqing Date: Sat, 17 May 2025 17:18:09 +0800 Subject: [PATCH] :bug: Fix document saving logic --- frontend/src/editor/Editor.vue | 6 +++-- .../editor/extensions/autoSaveExtension.ts | 26 +++---------------- frontend/src/stores/documentStore.ts | 11 +++----- 3 files changed, 12 insertions(+), 31 deletions(-) diff --git a/frontend/src/editor/Editor.vue b/frontend/src/editor/Editor.vue index ee68570..481665c 100644 --- a/frontend/src/editor/Editor.vue +++ b/frontend/src/editor/Editor.vue @@ -122,8 +122,10 @@ const handleManualSave = async () => { const view = editorStore.editorView as EditorView; const content = view.state.doc.toString(); - // 使用文档存储的强制保存方法 - const success = await documentStore.forceSaveDocument(content); + // 先更新内容 + await DocumentService.UpdateActiveDocumentContent(content); + // 然后调用强制保存方法(不再传递content参数) + const success = await documentStore.forceSaveDocument(); if (success) { logStore.info(t('document.manualSaveSuccess')); } diff --git a/frontend/src/editor/extensions/autoSaveExtension.ts b/frontend/src/editor/extensions/autoSaveExtension.ts index d51c30b..49885ac 100644 --- a/frontend/src/editor/extensions/autoSaveExtension.ts +++ b/frontend/src/editor/extensions/autoSaveExtension.ts @@ -25,10 +25,10 @@ export function createAutoSavePlugin(options: AutoSaveOptions = {}) { class { private isActive: boolean = true; private isSaving: boolean = false; - private contentUpdateFn: (view: EditorView) => void; + private readonly contentUpdateFn: (view: EditorView) => void; constructor(private view: EditorView) { - // 创建内容更新函数 + // 创建内容更新函数,简单传递内容给后端 this.contentUpdateFn = this.createDebouncedUpdateFn(debounceDelay); } @@ -45,6 +45,7 @@ export function createAutoSavePlugin(options: AutoSaveOptions = {}) { const content = view.state.doc.toString(); try { + // 简单将内容传递给后端,让后端处理保存策略 await DocumentService.UpdateActiveDocumentContent(content); onSave(true); } catch (err) { @@ -95,23 +96,4 @@ export function createSaveShortcutPlugin(onSave: () => void) { return false; } }); -} - -/** - * 手动触发文档保存 - * @param view 编辑器视图 - * @returns Promise - */ -export async function saveDocument(view: EditorView): Promise { - try { - const content = view.state.doc.toString(); - // 更新内容 - await DocumentService.UpdateActiveDocumentContent(content); - // 强制保存到磁盘 - await DocumentService.ForceSave(); - return true; - } catch (err) { - console.error('Failed to save document:', err); - return false; - } -} \ No newline at end of file +} \ No newline at end of file diff --git a/frontend/src/stores/documentStore.ts b/frontend/src/stores/documentStore.ts index 048783c..17aa3a6 100644 --- a/frontend/src/stores/documentStore.ts +++ b/frontend/src/stores/documentStore.ts @@ -48,7 +48,7 @@ export const useDocumentStore = defineStore('document', () => { await DocumentService.UpdateActiveDocumentContent(content); lastSaved.value = new Date(); - // 如果我们有活动文档,更新本地副本 + // 更新本地副本 if (activeDocument.value) { activeDocument.value.content = content; activeDocument.value.meta.lastUpdated = lastSaved.value; @@ -66,21 +66,18 @@ export const useDocumentStore = defineStore('document', () => { } // 强制保存文档到磁盘 - async function forceSaveDocument(content: string): Promise { + async function forceSaveDocument(): Promise { if (isSaving.value) return false; isSaving.value = true; try { - // 先更新内容 - await DocumentService.UpdateActiveDocumentContent(content); - // 然后强制保存 + // 直接调用强制保存API await DocumentService.ForceSave(); lastSaved.value = new Date(); - // 如果我们有活动文档,更新本地副本 + // 更新时间戳 if (activeDocument.value) { - activeDocument.value.content = content; activeDocument.value.meta.lastUpdated = lastSaved.value; }