An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Restore tab attributes and header layout during undo https://github.com/siyuan-note/siyuan/issues/19580
Daniel536fcf222df5
diff --git a/app/src/protyle/render/tabsRender.ts b/app/src/protyle/render/tabsRender.tsindex d8d5430ae6..9d74907b7e 100644--- a/app/src/protyle/render/tabsRender.ts+++ b/app/src/protyle/render/tabsRender.ts@@ -202,6 +202,9 @@ export const tabsRender = (element: Element, options: ITabsRenderOptions = {}) =if (!header) {header = document.createElement("div");header.className = "tabs-header protyle-action";+ }+ // 移动及撤销可能把页签项插到控件前面,导航栏始终位于正文之前。+ if (tabs.firstElementChild !== header) {tabs.prepend(header);}header.setAttribute("contenteditable", "false");diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.tsindex 6b697f26a9..c1b2f47680 100644--- a/app/src/protyle/wysiwyg/transaction.ts+++ b/app/src/protyle/wysiwyg/transaction.ts@@ -142,11 +142,13 @@ const removeTopElement = (updateElement: Element, protyle: IProtyle) => {}};-const syncFoldAndStyleAttrs = (element: Element, operation: Extract<IOperation, {action: "setAttrs"}>) => {+const syncBlockAttrs = (element: Element, operation: Extract<IOperation, {action: "setAttrs"}>) => {const attrs = JSON.parse(operation.data);const hasFold = Object.prototype.hasOwnProperty.call(attrs, "fold");const hasStyle = Object.prototype.hasOwnProperty.call(attrs, "style");- if (!hasFold && !hasStyle) {+ const tabsAttrs = ["tabs-active-id", "tabs-position", "tabs-task"]+ .filter(name => Object.prototype.hasOwnProperty.call(attrs, name));+ if (!hasFold && !hasStyle && tabsAttrs.length === 0) {return;}element.querySelectorAll(`[data-node-id="${operation.id}"]`).forEach(item => {@@ -164,6 +166,14 @@ const syncFoldAndStyleAttrs = (element: Element, operation: Extract<IOperation,item.removeAttribute("style");}}+ // 撤销恢复的页签属性同步到 DOM,后续拖动据此捕获有效的选择和布局状态。+ tabsAttrs.forEach(name => {+ if (attrs[name]) {+ item.setAttribute(name, attrs[name]);+ } else {+ item.removeAttribute(name);+ }+ });});};@@ -506,7 +516,7 @@ const promiseTransaction = (options: {return;}if (operation.action === "setAttrs") {- syncFoldAndStyleAttrs(protyle.wysiwyg.element, operation);+ syncBlockAttrs(protyle.wysiwyg.element, operation);const gutterFoldElement = protyle.gutter.element.querySelector('[data-type="fold"]');if (gutterFoldElement) {gutterFoldElement.removeAttribute("disabled");@@ -596,7 +606,7 @@ const promiseTransaction = (options: {return;}if (operation.action === "setAttrs") {- syncFoldAndStyleAttrs(protyle.wysiwyg.element, operation);+ syncBlockAttrs(protyle.wysiwyg.element, operation);}// 冻结范围依赖列的 DOM 分组,新增列事务落盘后使用完整数据重建分组。if (operation.action === "addAttrViewCol" &&@@ -821,7 +831,7 @@ export const onTransaction = (protyle: IProtyle, operations: IOperation[], isUndupdateElements.push(item);});if (operation.action === "setAttrs") {- syncFoldAndStyleAttrs(protyle.wysiwyg.element, operation);+ syncBlockAttrs(protyle.wysiwyg.element, operation);return;}if (operation.action === "unfoldHeading") {diff --git a/app/tests/tabsDrag.test.js b/app/tests/tabsDrag.test.jsindex 875aafd9d4..6b3fee37bf 100644--- a/app/tests/tabsDrag.test.js+++ b/app/tests/tabsDrag.test.js@@ -19,9 +19,18 @@ const sources = () => {return [extract("protyle/util/editorCommonEvent.ts", ["getDragSourceParentID", "moveTo"]),extract("protyle/wysiwyg/getBlock.ts", ["getParentBlock", "getPreviousBlockSibling", "getTopAloneElement"]),- extract("protyle/render/tabsRender.ts", ["getTabItems", "getTabTask"]),+ (() => {+ const file = ts.createSourceFile("tabsRender.ts", readFileSync(path.join(__dirname,+ "../src/protyle/render/tabsRender.ts"), "utf8"), ts.ScriptTarget.Latest, true);+ return ts.transpileModule(file.statements.filter(statement => !ts.isImportDeclaration(statement))+ .map(statement => statement.getText(file)).join("\n").replace(/^export /gm, ""), {+ compilerOptions: {target: ts.ScriptTarget.ES2021},+ }).outputText;+ })(),extract("protyle/wysiwyg/tabsRemoval.ts", ["repairActiveTab"]),- extract("protyle/render/tabsState.ts", ["adjacentTabID"]),+ extract("protyle/render/tabsState.ts", ["adjacentTabID", "resolveTabID", "tabKeyboardTarget"]),+ extract("util/escape.ts", ["escapeHtml"]),+ extract("protyle/wysiwyg/transaction.ts", ["syncBlockAttrs"]),ts.transpileModule(readFileSync(path.join(__dirname, "../src/protyle/render/tabsDrag.ts"), "utf8").replace(/export const /g, "const "), {compilerOptions: {target: ts.ScriptTarget.ES2021}}).outputText,].join("\n");@@ -50,8 +59,8 @@ const cases = async (source) => {const constants = {ZWSP: "\u200b", SIYUAN_DROP_BLOCK: "application/siyuan-block", SIYUAN_DROP_GUTTER: "application/siyuan-gutter"};const protyle = {lute, wysiwyg: {element: root}, notebookId: "notebook", block: {rootID: "doc"}};window.siyuan = {config: {system: {workspaceDir: "workspace"}}};- const {moveTo, bindTabsDrag, isDraggingTabs} = new Function("Constants", "genEmptyElement", "root", "protyle",- source + "; return {moveTo, bindTabsDrag, isDraggingTabs};")(+ const {moveTo, bindTabsDrag, isDraggingTabs, syncBlockAttrs, tabsRender, destroyTabsRender} = new Function("Constants", "genEmptyElement", "root", "protyle",+ source + "; return {moveTo, bindTabsDrag, isDraggingTabs, syncBlockAttrs, tabsRender, destroyTabsRender};")(constants, genEmptyElement, root, protyle);const list = document.createElement("div");const button = document.createElement("button");@@ -100,7 +109,7 @@ const cases = async (source) => {check.ok(Array.from(node.children).some(child => child.dataset.nodeId === attrs["tabs-active-id"]),JSON.stringify(operation));}- Object.entries(attrs).forEach(([name, value]) => node.setAttribute(name, value));+ syncBlockAttrs(stored, operation);} else if (operation.action === "delete") {node?.remove();} else if (operation.action === "update") {@@ -121,10 +130,43 @@ const cases = async (source) => {});replay(result.doOperations);replay(result.undoOperations);+ root.innerHTML = stored.innerHTML;+ const secondItem = root.querySelector(`[data-node-id="${originalID}"]`);+ const secondResult = await moveTo(protyle, [secondItem], root.lastElementChild, true, "afterend", false);+ replay(secondResult.doOperations);+ replay(secondResult.undoOperations);+ check.equal(find(originalID).parentElement.getAttribute("tabs-active-id"), originalID);check.equal(find(originalID).parentElement.dataset.nodeId, tabs.dataset.nodeId);check.equal(find(tabs.dataset.nodeId).getAttribute("tabs-position"), "left");- replay(result.doOperations);- replay(result.undoOperations);+ replay(secondResult.doOperations);+ replay(secondResult.undoOperations);+ check.equal(find(originalID).parentElement.getAttribute("tabs-active-id"), originalID);+ const restoredTabs = find(tabs.dataset.nodeId);+ const syncAttrs = attrs => syncBlockAttrs(stored, {+ action: "setAttrs", id: tabs.dataset.nodeId, data: JSON.stringify(attrs),+ });+ syncAttrs({"tabs-position": "top", "tabs-task": "true", fold: "1", style: "color: red"});+ check.equal(restoredTabs.getAttribute("tabs-position"), "top");+ check.equal(restoredTabs.getAttribute("tabs-task"), "true");+ check.equal(restoredTabs.getAttribute("fold"), "1");+ check.equal(restoredTabs.style.color, "red");+ check.equal(restoredTabs.getAttribute("tabs-active-id"), originalID);+ syncAttrs({"tabs-position": "", "tabs-task": "", fold: "0", style: ""});+ for (const name of ["tabs-position", "tabs-task", "fold", "style"]) {+ check.equal(restoredTabs.hasAttribute(name), false);+ }+ check.equal(restoredTabs.getAttribute("tabs-active-id"), originalID);+ root.innerHTML = lute.Md2BlockDOM("::: tabs\n@tab First\n\nFirst body\n@tab Second\n\nSecond body\n:::\n");+ tabsRender(root);+ const layoutTabs = root.querySelector(".tabs");+ const header = layoutTabs.querySelector(".tabs-header");+ const firstPanel = layoutTabs.querySelector(".tab-item");+ // 撤销将首个页签项插回容器前部,复用的导航栏必须恢复到正文之前。+ layoutTabs.prepend(firstPanel);+ tabsRender(root);+ check.equal(layoutTabs.firstElementChild, header);+ check.equal(layoutTabs.querySelectorAll(":scope > .tabs-header").length, 1);+ destroyTabsRender(root);root.remove();return "Tabs drag cases passed";};@@ -159,7 +201,7 @@ const run = async () => {if (process.versions.electron && process.type === "browser") {run().catch(error => { console.error(error); require("electron").app.exit(1); });} else {- require("node:test").test("tab drag transactions preserve valid active IDs through undo and redo", {+ require("node:test").test("repeated tab drags preserve active IDs through attribute replay, undo and redo", {skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,timeout: 45000,}, async () => {