在BaseFileView.tsx中添加关闭状态管理,优化文件保存逻辑以防止数据丢失,确保在视图关闭时不进行保存操作。

This commit is contained in:
duanfuxiang
2025-07-19 09:26:52 +08:00
parent 519fc3769d
commit e2df9a7995
2 changed files with 18 additions and 6 deletions

View File

@@ -11,6 +11,7 @@ export default abstract class BaseView extends TextFileView {
protected state: { filePath?: string } | null = null;
protected isEditorLoaded: boolean = false;
protected currentFilePath: string | null = null;
protected isClosing: boolean = false;
protected constructor(leaf: WorkspaceLeaf, plugin: InfioPlugin) {
super(leaf);
@@ -79,7 +80,7 @@ export default abstract class BaseView extends TextFileView {
async onLoadFile(file: TFile): Promise<void> {
try {
const content = await this.app.vault.read(file);
const content = await this.app.vault.cachedRead(file);
this.setViewData(content, true);
} catch (error) {
console.error('Failed to load file content:', error);
@@ -103,8 +104,20 @@ export default abstract class BaseView extends TextFileView {
}
async save(clear?: boolean): Promise<void> {
// Prevent saving if the view is closing
if (this.isClosing) {
console.log("save() called during close, skipping to prevent data loss");
return;
}
const content = this.getViewData();
// Additional safety check: don't save if content is empty and we had content before
if (!content.trim() && this.currentFilePath) {
console.log("Refusing to save empty content, potential data loss prevented");
return;
}
if (this.file) {
// Regular file in vault
await this.app.vault.modify(this.file, content);
@@ -133,6 +146,7 @@ export default abstract class BaseView extends TextFileView {
}
onClose(): Promise<void> {
this.isClosing = true;
return super.onClose();
}
@@ -145,7 +159,7 @@ export default abstract class BaseView extends TextFileView {
}
protected onEditorUpdate(update: ViewUpdate): void {
if (update.docChanged) {
if (update.docChanged && !this.isClosing) {
this.requestSave();
}
}