Skip to content

Commit

Permalink
Merge pull request #157 from obgnail/dev
Browse files Browse the repository at this point in the history
fix window_tab:跨文件锚点
  • Loading branch information
obgnail authored Sep 9, 2023
2 parents 6225556 + ac8bf0c commit ccf0fa3
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
2 changes: 2 additions & 0 deletions plugin/global/settings/settings.default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
64 changes: 64 additions & 0 deletions plugin/test.js
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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 = {
Expand Down
24 changes: 24 additions & 0 deletions plugin/window_tab/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ccf0fa3

Please sign in to comment.