🐛 Fix document saving logic

This commit is contained in:
2025-05-17 17:18:09 +08:00
parent 1246166231
commit 4e291b889b
3 changed files with 12 additions and 31 deletions

View File

@@ -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'));
}

View File

@@ -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<boolean>
*/
export async function saveDocument(view: EditorView): Promise<boolean> {
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;
}
}
}

View File

@@ -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<boolean> {
async function forceSaveDocument(): Promise<boolean> {
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;
}