Skip to content

Commit

Permalink
小细节:打开的文件是同一文件的情况
Browse files Browse the repository at this point in the history
  • Loading branch information
obgnail committed Sep 17, 2023
1 parent f404b10 commit 93e2192
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 21 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions plugin/custom/plugins/echarts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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();
}
Expand Down
8 changes: 4 additions & 4 deletions plugin/fence_enhance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
17 changes: 7 additions & 10 deletions plugin/global/core/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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",
Expand All @@ -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);
Expand Down
10 changes: 7 additions & 3 deletions plugin/markmap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
}
Expand Down

0 comments on commit 93e2192

Please sign in to comment.