Skip to content

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

:bug: Keep active tab when clicking read-only document whitespace https://github.com/siyuan-note/siyuan/issues/19554

Daniel8d3bd1521f0d
diff --git a/app/src/protyle/wysiwyg/getBlock.test.ts b/app/src/protyle/wysiwyg/getBlock.test.ts
index a2e66cffaf..839d138028 100644
--- a/app/src/protyle/wysiwyg/getBlock.test.ts
+++ b/app/src/protyle/wysiwyg/getBlock.test.ts
@@ -3,6 +3,7 @@ import * as assert from "node:assert/strict";
let getEmbedChildOperationContext: typeof import("./getBlock").getEmbedChildOperationContext;
let getEmbedGutterOperationContext: typeof import("./getBlock").getEmbedGutterOperationContext;
+let getContenteditableElement: typeof import("./getBlock").getContenteditableElement;
let isNotEditBlock: typeof import("./getBlock").isNotEditBlock;
before(async () => {
@@ -10,7 +11,8 @@ before(async () => {
SIYUAN_VERSION: "test",
NODE_ENV: "test",
});
- ({getEmbedChildOperationContext, getEmbedGutterOperationContext, isNotEditBlock} = await import("./getBlock"));
+ ({getContenteditableElement, getEmbedChildOperationContext, getEmbedGutterOperationContext, isNotEditBlock} =
+ await import("./getBlock"));
});
class TestElement {
@@ -51,12 +53,25 @@ class TestElement {
return this.attributes.has(name);
}
+ get firstElementChild() {
+ return this.children[0] || null;
+ }
+
+ querySelector(selector: string) {
+ if (selector === ":scope > .tab-item-content > [data-node-id]") {
+ return this.children.find(child => child.classes.has("tab-item-content"))?.children
+ .find(child => child.hasAttribute("data-node-id")) || null;
+ }
+ return this.querySelectorAll(selector)[0] || null;
+ }
+
querySelectorAll(selector: string) {
const nodeID = /^\[data-node-id="(.+)"\]$/.exec(selector)?.[1];
const matches: TestElement[] = [];
const visit = (element: TestElement) => {
element.children.forEach(child => {
- if (nodeID && child.getAttribute("data-node-id") === nodeID) {
+ if ((selector === "[data-node-id]" && child.hasAttribute("data-node-id")) ||
+ (nodeID && child.getAttribute("data-node-id") === nodeID)) {
matches.push(child);
}
visit(child);
@@ -124,3 +139,31 @@ describe("isNotEditBlock", () => {
assert.equal(isNotEditBlock(paragraph as unknown as Element), false);
});
});
+
+describe("getContenteditableElement", () => {
+ it("uses the active item when a tab block is the first document block", () => {
+ const editor = new TestElement().addClass("protyle-wysiwyg");
+ const tabs = new TestElement().addClass("tabs")
+ .setAttribute("data-node-id", "tabs")
+ .setAttribute("data-type", "NodeTabs")
+ .setAttribute("tabs-active-id", "second");
+ const createItem = (id: string, hidden: boolean) => {
+ const item = new TestElement().addClass("tab-item")
+ .setAttribute("data-node-id", id)
+ .setAttribute("data-type", "NodeTabItem")
+ .setAttribute("data-tabs-hidden", String(hidden));
+ const content = new TestElement().addClass("tab-item-content");
+ const paragraph = new TestElement()
+ .setAttribute("data-node-id", `${id}-paragraph`)
+ .setAttribute("data-type", "NodeParagraph");
+ const editable = new TestElement();
+ item.append(content.append(paragraph.append(editable)));
+ return {item, editable};
+ };
+ const first = createItem("first", true);
+ const second = createItem("second", false);
+ editor.append(tabs.append(first.item, second.item));
+
+ assert.equal(getContenteditableElement(editor as unknown as Element), second.editable as unknown as Element);
+ });
+});
diff --git a/app/src/protyle/wysiwyg/getBlock.ts b/app/src/protyle/wysiwyg/getBlock.ts
index 90d4959ae7..5bc3321289 100644
--- a/app/src/protyle/wysiwyg/getBlock.ts
+++ b/app/src/protyle/wysiwyg/getBlock.ts
@@ -206,6 +206,9 @@ export const getContenteditableElement = (element: Element, target?: Node): Elem
let blockElement = element;
if (!blockElement.getAttribute("data-node-id")) {
blockElement = element.querySelector("[data-node-id]");
+ if (blockElement?.classList.contains("tabs")) {
+ return getContenteditableElement(blockElement, target);
+ }
}
if (!blockElement) {
const tempBlockElement = hasClosestBlock(element);