From 93e2192fa1e7c41e37364fefc15f707a1af9c93b Mon Sep 17 00:00:00 2001 From: obgnail Date: Sun, 17 Sep 2023 17:19:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=8F=E7=BB=86=E8=8A=82=EF=BC=9A=E6=89=93?= =?UTF-8?q?=E5=BC=80=E7=9A=84=E6=96=87=E4=BB=B6=E6=98=AF=E5=90=8C=E4=B8=80?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=9A=84=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 +++-- plugin/custom/plugins/echarts/index.js | 9 +++++++-- plugin/fence_enhance.js | 8 ++++---- plugin/global/core/plugin.js | 17 +++++++---------- plugin/markmap/index.js | 10 +++++++--- 5 files changed, 28 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 9d576f5d..0aec0359 100644 --- a/README.md +++ b/README.md @@ -525,9 +525,10 @@ cmd = "cd $m && git add . && git commit -m \"message\"" - 只需使用 `style = () => "..."`,即可注册 css。 - 只需使用 `hint = () => "将当前标题的路径复制到剪切板"`,即可注册 hint。 +- 只需使用 `select = () => "..."`,即可注册允许运行命令的光标位置。 - 只需使用 `hotkey = () => ["ctrl+shift+y"]` ,即可注册快捷键。 - 只需使用 `this.modal` 函数即可自动生成自定义的模态框。 -- init、selector、html、process、callback 等等生命周期函数。 +- init、html、process、callback 等等生命周期函数。 ```js class fullPathCopy extends BaseCustomPlugin { @@ -668,7 +669,7 @@ class fullPathCopy extends BaseCustomPlugin { module.exports = { plugin: fullPathCopy }; // 1. 创建 class,继承 BaseCustomPlugin 类。此时,fullPathCopy 将自动拥有 utils、info、config 属性 和 modal 方法。 -// - utils:插件系统自带的静态工具类,其定义在 `./plugin/global/core/plugin.js/utils`。其中有两个最重要的函数:utils.getPlugin(fixed_name) 和 utils.getCustomPlugin(fixed_name) 用于获取已经实现的全部插件,调用其 API。具体的 API 可看 openPlatformAPI.md 文件 +// - utils:插件系统自带的静态工具类,其定义在 `./plugin/global/core/plugin.js/utils`。其中有三个重要的函数:utils.getPlugin(fixed_name) 和 utils.getCustomPlugin(fixed_name) 用于获取已经实现的全部插件,调用其 API,具体的 API 可看 openPlatformAPI.md 文件。utils.addEventListener(eventType, listener) 用于监听 Typora 的生命周期事件。 // - info:该插件在 custom_plugin.user.toml 里的所有字段 // - config:等同于 info.config // - modal:生成自定义的模态框,和用户交互。具体用法可以参考 __modal_example.js diff --git a/plugin/custom/plugins/echarts/index.js b/plugin/custom/plugins/echarts/index.js index 8c768aff..4e6582c3 100644 --- a/plugin/custom/plugins/echarts/index.js +++ b/plugin/custom/plugins/echarts/index.js @@ -2,13 +2,15 @@ class echartsPlugin extends BaseCustomPlugin { init = () => { this.map = {} // {cid: instance} this.FenceContent = ""; + this.filepath = ""; } callback = anchorNode => this.utils.insertFence(anchorNode, this.config.TEMPLATE) process = () => { this.utils.registerDiagramParser("echarts", false, this.render, this.cancel); - this.utils.addEventListener(this.utils.eventType.beforeFileOpen, this.destroyAll); + this.utils.addEventListener(this.utils.eventType.beforeFileOpen, () => this.filepath = this.utils.getFilePath()); + this.utils.addEventListener(this.utils.eventType.fileOpened, this.destroyAll); } render = async (cid, content, $pre) => { @@ -33,7 +35,10 @@ class echartsPlugin extends BaseCustomPlugin { delete this.map[cid]; } } - destroyAll = () => { + destroyAll = filepath => { + // 小细节:打开同一个文件的情况 + if (this.filepath === filepath) return; + for (let cid of Object.keys(this.map)) { this.map[cid].dispose(); } diff --git a/plugin/fence_enhance.js b/plugin/fence_enhance.js index 37285d27..2c9970aa 100644 --- a/plugin/fence_enhance.js +++ b/plugin/fence_enhance.js @@ -204,11 +204,11 @@ class fenceEnhancePlugin extends global._basePlugin { this.lastClickTime = ev.timeStamp; const result = this.utils.getFenceContent(copyButton.closest(".md-fences")) - navigator.clipboard.writeText(result); // File.editor.UserOp.setClipboard(null, null, result); - - copyButton.firstElementChild.className = "fa fa-check"; - setTimeout(() => copyButton.firstElementChild.className = "fa fa-clipboard", this.config.WAIT_RECOVER_INTERVAL); + navigator.clipboard.writeText(result).then(()=> { + copyButton.firstElementChild.className = "fa fa-check"; + setTimeout(() => copyButton.firstElementChild.className = "fa fa-clipboard", this.config.WAIT_RECOVER_INTERVAL); + }) } foldCode = (ev, foldButton) => { diff --git a/plugin/global/core/plugin.js b/plugin/global/core/plugin.js index bf9d8ac8..be6ab08f 100644 --- a/plugin/global/core/plugin.js +++ b/plugin/global/core/plugin.js @@ -88,17 +88,17 @@ class utils { // 注册新的代码块语法 // 1. lang(string): language - // 2. destroyWhenUpdate: 更新前是否清空preview里的html - // 3. renderFunc(async (cid, content, $pre) => null): 渲染函数,根据内容渲染所需的图像 + // 2. destroyWhenUpdate(boolean): 更新前是否清空preview里的html + // 3. async renderFunc(cid, content, $pre) => null: 渲染函数,根据内容渲染所需的图像 // cid: 当前代码块的cid // content: 代码块的内容 // $pre: 代码块的jquery element - // 4. cancelFunc(async cid => null): 取消函数,触发时机:1)修改为其他的lang 2)当代码块内容被清空 3)当代码块内容不符合语法 - // 5. extraStyleGetter(() => string): 用于导出时,新增css + // 4. async cancelFunc(cid) => null: 取消函数,触发时机:1)修改为其他的lang 2)当代码块内容被清空 3)当代码块内容不符合语法 + // 5. extraStyleGetter() => string: 用于导出时,新增css static registerDiagramParser = ( lang, destroyWhenUpdate, renderFunc, cancelFunc = null, extraStyleGetter = null ) => global._diagramParser.register(lang, destroyWhenUpdate, renderFunc, cancelFunc, extraStyleGetter) - // 当代码块内容出现语法错误时调用 + // 当代码块内容出现语法错误时调用,此时页面将显示错误信息 static throwParseError = (errorLine, reason) => global._diagramParser.throwParseError(errorLine, reason) // 当前插件系统拥有的event: @@ -109,11 +109,9 @@ class utils { // fileOpened: 打开文件之后 // fileContentLoaded: 文件内容加载完毕之后(依赖于window_tab) // beforeToggleSourceMode: 进入源码模式之前 - // beforeCloseWindow: 窗口关闭之前 + // beforeAddCodeBlock: 添加代码块之前 + // afterAddCodeBlock: 添加代码块之后 static eventType = { - // enable: "enable", - // disable: "disable", - allCustomPluginsHadInjected: "allCustomPluginsHadInjected", allPluginsHadInjected: "allPluginsHadInjected", beforeFileOpen: "beforeFileOpen", @@ -122,7 +120,6 @@ class utils { beforeToggleSourceMode: "beforeToggleSourceMode", beforeAddCodeBlock: "beforeAddCodeBlock", afterAddCodeBlock: "afterAddCodeBlock", - // beforeCloseWindow: "beforeCloseWindow", } static addEventListener = (eventType, listener) => global._eventHub.addEventListener(eventType, listener); static removeEventListener = (eventType, listener) => global._eventHub.removeEventListener(eventType, listener); diff --git a/plugin/markmap/index.js b/plugin/markmap/index.js index 92cb0ffe..538007a1 100644 --- a/plugin/markmap/index.js +++ b/plugin/markmap/index.js @@ -71,18 +71,19 @@ class fenceMarkmap { this.utils = this.controller.utils; this.config = this.controller.config; this.map = {}; // {cid: instance} + this.filepath = ""; } style = () => `.md-diagram-panel .plugin-fence-markmap-svg {line-height: initial !important;}` process = () => { this.utils.registerDiagramParser("markmap", false, this.render, this.cancel); - this.utils.addEventListener(this.utils.eventType.beforeFileOpen, this.destroyAll); + this.utils.addEventListener(this.utils.eventType.beforeFileOpen, () => this.filepath = this.utils.getFilePath()); + this.utils.addEventListener(this.utils.eventType.fileOpened, this.destroyAll); } call = async type => type === "draw_fence" && this.utils.insertFence(null, this.config.FENCE_TEMPLATE) - render = async (cid, content, $pre) => { if (!this.controller.transformer || !this.controller.Markmap) { await this.controller.lazyLoad(); @@ -101,7 +102,10 @@ class fenceMarkmap { delete this.map[cid]; } }; - destroyAll = () => { + destroyAll = filepath => { + // 小细节:打开同一个文件的情况 + if (this.filepath === filepath) return; + for (let cid of Object.keys(this.map)) { this.map[cid].destroy(); }