From d3b678fc81b26ad511808cf8c1994020b05a47cb Mon Sep 17 00:00:00 2001 From: wood chen Date: Fri, 7 Mar 2025 15:23:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20activeFile=20=E5=A4=84?= =?UTF-8?q?=E7=90=86=E9=80=BB=E8=BE=91=EF=BC=8C=E5=A2=9E=E5=BC=BA=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E9=80=89=E6=8B=A9=E5=92=8C=E6=A8=A1=E6=80=81=E6=A1=86?= =?UTF-8?q?=E7=A8=B3=E5=AE=9A=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在打开类别模态框前确保 activeFile 正确初始化 - 添加对未选择文件场景的错误处理 - 在类别选择模态框中增加 activeFile 存在性检查 - 提升插件在不同文件选择状态下的健壮性 --- src/main.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/main.ts b/src/main.ts index 6c83544..e2924b2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -517,6 +517,23 @@ export default class DiscourseSyncPlugin extends Plugin { } private async openCategoryModal() { + // 确保activeFile已设置 + if (!this.activeFile) { + const activeFile = this.app.workspace.getActiveFile(); + if (!activeFile) { + new NotifyUser(this.app, t('NO_ACTIVE_FILE')).open(); + return; + } + + const content = await this.app.vault.read(activeFile); + const fm = this.getFrontMatter(content); + this.activeFile = { + name: activeFile.basename, + content: content, + postId: fm?.discourse_post_id + }; + } + const [categories, tags] = await Promise.all([ this.fetchCategories(), this.fetchTags() @@ -616,6 +633,13 @@ export class SelectCategoryModal extends Modal { contentEl.empty(); contentEl.addClass('discourse-sync-modal'); + // 确保activeFile存在 + if (!this.plugin.activeFile) { + contentEl.createEl("h1", { text: t('ERROR') }); + contentEl.createEl("p", { text: t('NO_ACTIVE_FILE') }); + return; + } + const isUpdate = this.plugin.activeFile.postId !== undefined; contentEl.createEl("h1", { text: isUpdate ? t('UPDATE_POST') : t('PUBLISH_TO_DISCOURSE') });