From bc138da385cd07704d3e8f5a50b69805c644677d 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 08:53:37 +0800 Subject: [PATCH] =?UTF-8?q?=E7=83=AD=E9=94=AE=E8=A6=86=E7=9B=96=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E7=94=B1=E6=8F=92=E4=BB=B6=E7=B1=BB=E8=BD=AC=E7=A7=BB?= =?UTF-8?q?=E5=88=B0=E8=A7=86=E5=9B=BE=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- README_en.md | 2 +- plugin/SmmEditView.js | 46 +++++++++++++++++++++++++++-- plugin/main.js | 5 ++++ plugin/ob/Commands.js | 67 ++----------------------------------------- 5 files changed, 53 insertions(+), 69 deletions(-) diff --git a/README.md b/README.md index ff8d2a8..49abff9 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ 【[English](./README_en.md) | 简体中文】 -# SimpleMindMap Obsidian 插件 +# SimpleMindMap 插件 为 Obsidian 提供一个好用的思维导图插件。 diff --git a/README_en.md b/README_en.md index 4c223ea..946017d 100644 --- a/README_en.md +++ b/README_en.md @@ -1,6 +1,6 @@ 【English | [简体中文](./README_zh.md)】 -# SimpleMindMap Obsidian Plugin +# SimpleMindMap Plugin Provides a user-friendly mind map plugin for Obsidian. diff --git a/plugin/SmmEditView.js b/plugin/SmmEditView.js index 05c3112..2f90784 100644 --- a/plugin/SmmEditView.js +++ b/plugin/SmmEditView.js @@ -62,6 +62,7 @@ class SmmEditView extends TextFileView { this.savingTipEl = null this.isReadonlyMode = false this.toggleReadonlyButton = null + this.popScope = null } // 获取视图类型 @@ -130,6 +131,9 @@ class SmmEditView extends TextFileView { } }) } + + // 注册热键覆盖 + this._registerHotkeyOverrides() } // 获取视图数据(保存到文件) @@ -155,9 +159,8 @@ class SmmEditView extends TextFileView { this.parsedMindMapData = parseMarkdownText(rawData) const content = this.parsedMindMapData.metadata.content if (content) { - this.parsedMindMapData.metadata.content = LZString.decompressFromBase64( - content - ) + this.parsedMindMapData.metadata.content = + LZString.decompressFromBase64(content) } else { throw new Error('文件格式错误') } @@ -521,6 +524,8 @@ class SmmEditView extends TextFileView { const nowActive = leaf?.view === this if (nowActive && !this.isActive) { // 标签被激活 + // 注册热键覆盖 + this._registerHotkeyOverrides() this.isActive = true // 激活后刷新视图 if (this.mindMapAPP) { @@ -540,6 +545,8 @@ class SmmEditView extends TextFileView { } } else if (!nowActive && this.isActive) { // 标签失去激活 + // 清理热键覆盖 + this._clearPopScope() const rect = this.warpEl.getBoundingClientRect() this.isHidden = rect.width === 0 || rect.height === 0 if (this.mindMapAPP) { @@ -596,6 +603,7 @@ class SmmEditView extends TextFileView { } async onClose() { + this._clearPopScope() await this.save() // 手动保存 this.clear() this.saveButton = null @@ -843,6 +851,38 @@ class SmmEditView extends TextFileView { return '' } } + + // 清理之前的热键覆盖 + _clearPopScope() { + if (this.popScope) { + this.popScope() + this.popScope = null + } + } + + // 注册热键覆盖 + _registerHotkeyOverrides() { + this._clearPopScope() + const scope = this.app.keymap.getRootScope() + const ctrlFHandler = scope.register(['Mod'], 'f', evt => { + evt.preventDefault() + return true + }) + const ctrlSHandler = scope.register(['Mod'], 's', evt => { + evt.preventDefault() + const view = this.plugin._getActiveSmmView() + if (view) { + view.forceSave() + } + return true + }) + scope.keys.unshift(scope.keys.pop()) + scope.keys.unshift(scope.keys.pop()) + this.popScope = () => { + scope.unregister(ctrlFHandler) + scope.unregister(ctrlSHandler) + } + } } export default SmmEditView diff --git a/plugin/main.js b/plugin/main.js index 6574852..c2362ab 100644 --- a/plugin/main.js +++ b/plugin/main.js @@ -328,6 +328,11 @@ export default class SimpleMindMapPlugin extends Plugin { }) } + // 获取当前激活的思维导图视图 + _getActiveSmmView() { + return this.app.workspace.getActiveViewOfType(SmmEditView, IGNORE_CHECK_SMM) + } + // 判断文件是否为思维导图 _isSmmFile(file) { if (!file) { diff --git a/plugin/ob/Commands.js b/plugin/ob/Commands.js index 88190c3..92857ce 100644 --- a/plugin/ob/Commands.js +++ b/plugin/ob/Commands.js @@ -1,72 +1,15 @@ import { Notice } from 'obsidian' -import { IGNORE_CHECK_SMM } from './constant.js' -import SmmEditView from '../SmmEditView.js' export default class Commands { constructor(plugin) { this.plugin = plugin this.app = plugin.app - this.popScope = null - this._initHotkeyOverrides() this._addCreatesCommand() this._addActionsCommand() } - clear() { - if (this.popScope) this.popScope() - } - - _initHotkeyOverrides() { - this._registerHotkeyOverrides() - - // 监听视图切换事件 - this.plugin.registerEvent( - this.app.workspace.on('active-leaf-change', leaf => { - this._registerHotkeyOverrides() - }) - ) - } - - _registerHotkeyOverrides() { - // 清理之前的覆盖 - if (this.popScope) { - this.popScope() - this.popScope = null - } - const activeView = this.app.workspace.activeLeaf?.view - if (!(activeView instanceof SmmEditView)) { - return - } - - const scope = this.app.keymap.getRootScope() - - // 1. 覆盖 Ctrl+F - const ctrlFHandler = scope.register(['Mod'], 'f', evt => { - evt.preventDefault() // 阻止浏览器默认查找 - return true // 表示已处理 - }) - - // 2. 覆盖 Ctrl+S - const ctrlSHandler = scope.register(['Mod'], 's', evt => { - evt.preventDefault() // 阻止浏览器保存对话框 - const view = this._getActiveSmmView() - if (view) { - view.forceSave() - } - return true - }) - - // 提升处理器优先级 - scope.keys.unshift(scope.keys.pop()) // 移动 Ctrl+S - scope.keys.unshift(scope.keys.pop()) // 移动 Ctrl+F - - // 设置清理函数 - this.popScope = () => { - scope.unregister(ctrlFHandler) - scope.unregister(ctrlSHandler) - } - } + clear() {} addCommand(command) { this.plugin.addCommand(command) @@ -120,7 +63,7 @@ export default class Commands { id: 'enter-smm-mindmap-demonstrate', name: this._t('action.enterDemonstrate'), checkCallback: checking => { - const view = this._getActiveSmmView() + const view = this.plugin._getActiveSmmView() if (view) { if (!checking) { view.mindMapAPP.$bus.$emit('enter_demonstrate') @@ -141,7 +84,7 @@ export default class Commands { } ], checkCallback: checking => { - const view = this._getActiveSmmView() + const view = this.plugin._getActiveSmmView() if (view) { if (!checking) { view.forceSaveAndUpdateImage() @@ -152,8 +95,4 @@ export default class Commands { } }) } - - _getActiveSmmView() { - return this.app.workspace.getActiveViewOfType(SmmEditView, IGNORE_CHECK_SMM) - } }