From aade07e08c81b71066b93d7f6bd94c731ebda080 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A1=97=E8=A7=92=E5=B0=8F=E6=9E=97?= <1013335014@qq.com> Date: Wed, 3 Sep 2025 09:05:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=BB=BA=E5=B9=B6=E6=8F=92=E5=85=A5?= =?UTF-8?q?=E6=80=9D=E7=BB=B4=E5=AF=BC=E5=9B=BE=E7=9A=84=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin/ob/Commands.js | 45 +++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/plugin/ob/Commands.js b/plugin/ob/Commands.js index 563f169..9affe1d 100644 --- a/plugin/ob/Commands.js +++ b/plugin/ob/Commands.js @@ -1,4 +1,4 @@ -import { Notice } from 'obsidian' +import { Notice, MarkdownView } from 'obsidian' export default class Commands { constructor(plugin) { @@ -32,27 +32,30 @@ export default class Commands { this.addCommand({ id: 'create-smm-mindmap-insert-markdown', name: this._t('action.createMindMapInsertToMd'), - editorCallback: editor => { - this.plugin._createSmmFile('', fileName => { - try { - const file = this.app.vault.getFileByPath(fileName) - const currentFile = this.app.workspace.getActiveFile() - if (currentFile?.extension !== 'md') { - // 当前文件不是markdown文件 - new Notice(this._t('tip.fileIsNotMd')) - return - } - const linkText = this.app.fileManager.generateMarkdownLink( - file, - currentFile?.path || '' - ) - editor.replaceSelection('!' + linkText) - } catch (error) { - console.error(error) - // 新建思维导图失败 - new Notice(this._t('tip.createMindMapFail')) + checkCallback: checking => { + const markdownView = + this.app.workspace.getActiveViewOfType(MarkdownView) + if (markdownView) { + if (!checking) { + this.plugin._createSmmFile('', fileName => { + try { + const file = this.app.vault.getFileByPath(fileName) + const currentFile = this.app.workspace.getActiveFile() + const linkText = this.app.fileManager.generateMarkdownLink( + file, + currentFile?.path || '' + ) + const activeEditor = this.app.workspace.activeEditor + activeEditor.editor.replaceSelection('!' + linkText) + } catch (error) { + console.error(error) + new Notice(this._t('tip.createMindMapFail')) + } + }) } - }) + return true + } + return false } }) }