Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Repair hidden tab selection when clicking empty content https://github.com/siyuan-note/siyuan/issues/19242

Daniel03eaa5d42fed
diff --git a/app/scripts/tabsNavigation.test.js b/app/scripts/tabsNavigation.test.js
index a3843ea115..28a6675f2f 100644
--- a/app/scripts/tabsNavigation.test.js
+++ b/app/scripts/tabsNavigation.test.js
@@ -19,6 +19,7 @@ const sources = () => {
return {
navigation: extract("editor/util.ts", ["switchEditor"]),
visibility: extract("protyle/render/tabsVisibility.ts", ["isHiddenTabContent"]),
+ selection: extract("protyle/util/tabsSelection.ts", ["repairHiddenTabSelection"]),
};
};
@@ -86,6 +87,37 @@ const cases = (source) => {
root.dispatchEvent(new PointerEvent("pointerdown", {bubbles: true}));
check.equal(disconnected, true);
root.remove();
+ // 引用跳转后切回页签,点击空列表项必须替换隐藏正文中的旧选区。
+ const repair = new Function(source.visibility + source.selection + "; return repairHiddenTabSelection;")();
+ const selectionRoot = document.createElement("div");
+ document.body.append(selectionRoot);
+ selectionRoot.innerHTML = '<div class="tab-item" data-tabs-hidden="false"><div class="tab-item-content">' +
+ '<div data-type="NodeList"><div data-type="NodeListItem"><div data-type="NodeParagraph">' +
+ '<div contenteditable="true"></div></div></div></div><div contenteditable="true">Visible</div></div></div>' +
+ '<div class="tab-item" data-tabs-hidden="true"><div class="tab-item-content">' +
+ '<div contenteditable="true">Previous target</div></div></div>';
+ const [empty, visible, hidden] = selectionRoot.querySelectorAll('[contenteditable="true"]');
+ const selection = window.getSelection();
+ const place = element => {
+ const range = document.createRange();
+ range.selectNodeContents(element);
+ range.collapse(true);
+ selection.removeAllRanges();
+ selection.addRange(range);
+ };
+ place(hidden);
+ repair(selectionRoot, empty);
+ check.equal(selection.anchorNode, empty);
+ check.equal(selection.anchorOffset, 0);
+ place(visible);
+ repair(selectionRoot, empty);
+ check.equal(selection.anchorNode, visible);
+ place(hidden);
+ repair(selectionRoot, hidden);
+ check.equal(selection.anchorNode, hidden);
+ repair(selectionRoot, visible);
+ check.equal(selection.anchorNode, hidden);
+ selectionRoot.remove();
return "Tabs title and navigation cases passed";
};
diff --git a/app/src/protyle/util/tabsSelection.ts b/app/src/protyle/util/tabsSelection.ts
new file mode 100644
index 0000000000..358fbd17e6
--- /dev/null
+++ b/app/src/protyle/util/tabsSelection.ts
@@ -0,0 +1,20 @@
+import {isHiddenTabContent} from "../render/tabsVisibility";
+
+// 点击空正文时,浏览器可能保留隐藏页签中的选区,先将失效的光标放到当前编辑区域。
+export const repairHiddenTabSelection = (root: HTMLElement, target: Element) => {
+ const selection = window.getSelection();
+ const anchor = selection?.anchorNode;
+ const anchorElement = anchor instanceof Element ? anchor : anchor?.parentElement;
+ const editable = target.closest<HTMLElement>('[contenteditable="true"]');
+ if (!anchor || !root.contains(anchor) || !isHiddenTabContent(anchorElement) ||
+ !editable || !root.contains(editable) || !editable.closest(".tab-item-content") ||
+ isHiddenTabContent(editable) || target.closest('[contenteditable="false"]') ||
+ editable.textContent !== "" || editable.querySelector("[data-node-id], img, input, video, audio, iframe")) {
+ return;
+ }
+ const range = document.createRange();
+ range.selectNodeContents(editable);
+ range.collapse(true);
+ selection.removeAllRanges();
+ selection.addRange(range);
+};
diff --git a/app/src/protyle/wysiwyg/index.ts b/app/src/protyle/wysiwyg/index.ts
index 945beaeda8..2ef54a4620 100644
--- a/app/src/protyle/wysiwyg/index.ts
+++ b/app/src/protyle/wysiwyg/index.ts
@@ -1,4 +1,5 @@
import {visibleTabsSelectionHTML} from "../render/tabsVisibility";
+import {repairHiddenTabSelection} from "../util/tabsSelection";
import {isTabTextBoundary} from "./tabsBoundary";
import {
beforePaste,
@@ -1140,6 +1141,10 @@ export class WYSIWYG {
const documentSelf = document;
documentSelf.onmouseup = null;
let target = event.target as HTMLElement;
+ if (event.button === 0 && !event.shiftKey && !event.ctrlKey && !event.metaKey &&
+ !event.altKey && !protyle.disabled) {
+ repairHiddenTabSelection(this.element, target);
+ }
const emptyCell = target.closest<HTMLTableCellElement>("td:empty, th:empty");
if (emptyCell && event.button === 0 && !event.ctrlKey && !event.metaKey && !event.shiftKey &&
!event.altKey && !protyle.disabled && emptyCell.closest(".protyle-wysiwyg") === this.element) {