()
| 962 | |
| 963 | // 重写 save 方法,只在真正有未保存修改时才保存 |
| 964 | async save(): Promise<void> { |
| 965 | // 如果没有未保存修改,直接返回,不修改文件 |
| 966 | if (!this.isDirty) { |
| 967 | return; |
| 968 | } |
| 969 | |
| 970 | if (!this.file) return; |
| 971 | |
| 972 | try { |
| 973 | // 先清除 dirty 状态,避免保存时触发 modify 事件被误判为外部修改 |
| 974 | this.isDirty = false; |
| 975 | this.updateTitle(); |
| 976 | |
| 977 | // 使用 Obsidian 的标准保存方法 |
| 978 | await this.app.vault.modify(this.file, this.editorView.state.doc.toString()); |
| 979 | |
| 980 | // 更新缓存 |
| 981 | this.data = this.editorView.state.doc.toString(); |
| 982 | |
| 983 | // 保存成功后,更新侧边栏大纲 |
| 984 | type AppWithPlugins = App & { plugins: { getPlugin(id: string): CodeSpacePlugin | undefined } }; |
| 985 | const plugin = (this.app as unknown as AppWithPlugins).plugins.getPlugin("code-space"); |
| 986 | if (plugin && this.file) { |
| 987 | void plugin.updateOutline(this.file, this.editorView.state.doc.toString()); |
| 988 | } |
| 989 | } catch (error) { |
| 990 | // 保存失败,恢复 dirty 状态 |
| 991 | this.isDirty = true; |
| 992 | this.updateTitle(); |
| 993 | console.error("Code Space: Failed to save file:", error); |
| 994 | new Notice(t('NOTICE_SAVE_FAIL')); |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | // 更新标题显示未保存状态 |
| 999 | updateTitle() { |
no test coverage detected