From ac8bf0c9ac8b9b9a23ecfb00f6a0224188490abc Mon Sep 17 00:00:00 2001 From: obgnail Date: Sun, 10 Sep 2023 03:21:45 +0800 Subject: [PATCH] =?UTF-8?q?fix=20window=5Ftab=EF=BC=9A=E8=B7=A8=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E9=94=9A=E7=82=B9=E2=80=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin/global/settings/settings.default.toml | 2 + plugin/test.js | 64 ++++++++++++++++++++ plugin/window_tab/index.js | 24 ++++++++ 3 files changed, 90 insertions(+) diff --git a/plugin/global/settings/settings.default.toml b/plugin/global/settings/settings.default.toml index 08c7f49a..52324341 100644 --- a/plugin/global/settings/settings.default.toml +++ b/plugin/global/settings/settings.default.toml @@ -34,6 +34,8 @@ SWITCH_NEXT_TAB_HOTKEY = ["ctrl+PageDown", "ctrl+Tab"] SWITCH_PREVIOUS_TAB_HOTKEY = ["ctrl+PageUp", "ctrl+shift+Tab"] # 当鼠标位于标签页时,ctrl+wheel切换标签页 CTRL_WHEEL_TO_SCROLL = true +# typora1.1 版本以后,支持使用锚点跳转到本地文件,是否拦截处理(从新建窗口改成新建标签页) +INTERCEPT_INTERNAL_AND_LOCAL_LINKS = true # 内部使用 LOOP_DETECT_INTERVAL = 35 diff --git a/plugin/test.js b/plugin/test.js index 24dfd3b9..979cad4d 100644 --- a/plugin/test.js +++ b/plugin/test.js @@ -1,5 +1,30 @@ class testPlugin extends global._basePlugin { + init = () => { + console.log("-------- test.js") + const target = "decodeFromEscapedPath" + this.hadFound = false; + + global._findObject = (target, obj = File, level = 0, maxLevel = 10) => { + if (this.hadFound || level === this.maxLevel || typeof obj !== "object") return; + + for (let i of Object.keys(obj)) { + if (obj[i] != null) { + if (typeof obj[i] == "object") { + global._findObject(target, obj[i], level + 1) + } else { + if (i === target) { + this.hadFound = true + console.log(obj); + } + } + } + } + } + } + process() { + this.init(); + this.utils.decorate( () => (File && File.editor && File.editor.library && File.editor.library.openFileInNewWindow), "File.editor.library.openFileInNewWindow", @@ -9,6 +34,45 @@ class testPlugin extends global._basePlugin { JSBridge.invoke("window.toggleDevTools"); } + + // _findObject = (varName, from = File) => { + // let target = null; + // + // const _find = (from, level = 0, maxLevel = 10) => { + // if (this.hadFound || level === maxLevel || typeof from !== "object") return; + // + // for (let i of Object.keys(from)) { + // if (from[i] != null) { + // if (typeof from[i] == "object") { + // from[i].__parent__ = from; + // _find(from[i], level + 1) + // } else { + // if (i === varName) { + // this.hadFound = true + // target = from; + // } + // } + // } + // } + // } + // _find(varName, from) + // + // if (!target) { + // console.log("had not find") + // return + // } + // + // const result = [] + // let ele = target; + // while (ele) { + // result.push(ele); + // ele = ele.__parent__; + // } + // + // result.reverse() + // console.log(result) + // } + } module.exports = { diff --git a/plugin/window_tab/index.js b/plugin/window_tab/index.js index f95d6296..c6bb33ee 100644 --- a/plugin/window_tab/index.js +++ b/plugin/window_tab/index.js @@ -272,6 +272,30 @@ class windowTabBarPlugin extends global._basePlugin { } }) + if (this.config.INTERCEPT_INTERNAL_AND_LOCAL_LINKS) { + this.utils.decorate( + () => (JSBridge && JSBridge.invoke), + "JSBridge.invoke", + (...args) => { + if (args.length < 3 || args[0] !== "app.openFileOrFolder") return; + + const anchor = args[2]["anchor"]; + if (!anchor || typeof anchor !== "string" || !anchor.match(/^#/)) return; + + const filePath = args[1]; + this.openFile(filePath); + setTimeout(() => { + const ele = File.editor.EditHelper.findAnchorElem(anchor); + if (ele) { + File.editor.selection.jumpIntoElemBegin(ele); + File.editor.selection.scrollAdjust(ele, 10); + } + }, 1000) + return this.utils.stopCallError + } + ) + } + document.querySelector(".typora-quick-open-list").addEventListener("mousedown", ev => { const target = ev.target.closest(".typora-quick-open-item"); if (!target) return;