效果是右键文档树上的定位按钮时,先折叠其他打开的笔记本,再定位到当前文档
JS 代码片段:
设置开头的变量为 true 时是“左键定位, 右键折叠定位”,为 false 时互换为“左键折叠定位, 右键定位”
// 右键文档树定位按钮时先折叠再定位 JS片段 // author by JeffreyChen https://ld246.com/article/1727980528164 (function() { // 设置为 true 时是“左键定位, 右键折叠定位”,为 false 时互换为“左键折叠定位, 右键定位” const originalBehavior = true; function addFocusHandler() { const MAX_RETRIES = 100; // 最大重试次数 const RETRY_INTERVAL = 200; // 重试间隔(毫秒) let retryCount = 0; const FOCUS_BUTTON_SELECTOR = '.layout-tab-container > .file-tree .block__icon[data-type="focus"]'; const FILE_TREE_SELECTOR = ".layout-tab-container > .file-tree > .fn__flex-1 > ul.b3-list[data-url]"; function collapseLists() { const fileTreeLists = document.querySelectorAll(FILE_TREE_SELECTOR); fileTreeLists.forEach(list => { const fragment = new DocumentFragment(); Array.from(list.children).forEach(item => { if (item.matches('li.b3-list-item')) { const toggleElement = item.querySelector('.b3-list-item__toggle'); const arrowElement = item.querySelector('.b3-list-item__arrow'); if (toggleElement && arrowElement.classList.contains('b3-list-item__arrow--open')) { arrowElement.classList.remove('b3-list-item__arrow--open'); const nextSibling = item.nextElementSibling; if (nextSibling && nextSibling.matches('ul')) { fragment.appendChild(nextSibling); } } } }); // 批量清空节点 requestAnimationFrame(() => fragment.replaceChildren()); }); } function tryModifyAriaLabelAndAddHandler() { const focusButton = document.querySelector(FOCUS_BUTTON_SELECTOR); if (focusButton) { if (originalBehavior) { // 原始逻辑 focusButton.setAttribute('aria-label', '左键定位, 右键折叠定位'); focusButton.addEventListener('contextmenu', function(e) { e.preventDefault(); // 阻止默认右键行为 collapseLists(); focusButton.click(); // 模拟点击当前按钮 }); } else { // 左右互换逻辑 focusButton.setAttribute('aria-label', '左键折叠定位, 右键定位'); function leftClickHandler(e) { e.preventDefault(); // 阻止左键点击的默认行为 collapseLists(); focusButton.click(); // 模拟点击当前按钮 } function contextMenuHandler(e) { e.preventDefault(); // 阻止右键点击的默认行为 focusButton.removeEventListener('click', leftClickHandler); focusButton.click(); // 模拟点击当前按钮 setTimeout(() => { focusButton.addEventListener('click', leftClickHandler); }, 0); } // 为左键和右键添加相应的监听器 focusButton.addEventListener('click', leftClickHandler); focusButton.addEventListener('contextmenu', contextMenuHandler); } } else { retryCount++; if (retryCount < MAX_RETRIES) { setTimeout(tryModifyAriaLabelAndAddHandler, RETRY_INTERVAL); // 继续重试 } else { console.error('代码片段报错:无法找到文档树上的定位按钮'); } } } // 初次调用 tryModifyAriaLabelAndAddHandler(); } // 添加处理程序 addFocusHandler(); })();
目前已知的问题是:
- 如果当前文档所在的笔记本是展开的,使用时会有一点不必要的抖动。开发需求已经反馈到:Issue #12695 · siyuan-note/siyuan
- 有时候不能定位到预期中的文档,这个是思源的问题,已经反馈到:Issue #12694 · siyuan-note/siyuan
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于