今天研究了下这个关闭重复标签的问题,可以通过以下代码实现(放到ii-quicker的js代码片段中即可):
this.app.workspace.onLayoutReady(() => {
this.app.workspace.on("active-leaf-change", (activeLeaf) => {
const filePath = activeLeaf?.view.getState().file;
if (!filePath) return;
const viewType = activeLeaf?.view.getViewType();
// 获取已存在的其他叶子
let repeatLeaves = this.app.workspace.getLeavesOfType(viewType).filter((leaf) =>
// 不包含新打开的叶子
leaf !== activeLeaf &&
// 限制同一个分割窗口下的叶子
//leaf.parent.id === activeLeaf.parent.id &&
// 筛选文件路径相同的叶子
leaf.view?.getState().file === filePath
)
if(repeatLeaves.length > 0) {
repeatLeaves.push(activeLeaf)
// 排序规则,pinned优先,相同条件下,最先打开的优先
repeatLeaves = repeatLeaves.sort((leaf1, leaf2) => {
// 如果 leaf1 被固定但 leaf2 没有,leaf1 在前
if (leaf1.pinned && !leaf2.pinned) return -1;
// 如果 leaf2 被固定但 leaf1 没有,leaf2 在前
if (!leaf1.pinned && leaf2.pinned) return 1;
// 当两者都 pinned 或者都没有 pinned 的情况,根据 activeTime 排序
const leaf2ActiveTime =leaf2.activeTime || Date.now()
if (leaf1.activeTime !== leaf2ActiveTime) {
// 时间早的在前
return leaf1.activeTime - leaf2ActiveTime;
}
// activeTime 相同的情况下,保持原顺序
return 0;
});
// 激活排序后的第一个叶子
const newActiveLeaf = repeatLeaves.shift();
this.app.workspace.setActiveLeaf(newActiveLeaf, { focus: true });
// 关闭排序后的其他叶子
repeatLeaves.forEach((leaf) => {
leaf?.detach()
});
}
})
})
这个激活的规则是:锁定的叶子优先,相同条件下,最先打开的优先,其他的关闭。这样就保证了仅关闭最后打开的叶子,并激活之前已打开的叶子。
注意:按住CTRL重复打开某个文件,控制台可能会出现以下错误,
app.js:1 RangeError: Field is not present in this state
,这是因为叶子结点已销毁了,但ob销毁前要把叶子插入到访问历史,但又找不到对象引起的,这个应该是官方的bug,目前无法避免,但这个错误在使用中并没什么影响,按道理讲重复打开同一个文件也不应该产生很多重复的历史才对。