Skip to content

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

:bug: Synchronize mind map views across panes and standardize attributes https://github.com/siyuan-note/siyuan/issues/18831

Vanessa2c19522e0694
diff --git a/AGENTS.md b/AGENTS.md
index e75cfe623c..db5860b417 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -109,6 +109,8 @@ SiYuan repository guide. Module path `github.com/siyuan-note/siyuan`, license AG
6. **CSS:** Do not use the `:has()` selector because of its performance impact
7. **CSS positioning and scrolling:** When changing `position`, `transform`, `contain`, or `overflow` on a shared container, check the effects on descendant positioning reference frames, overlay coverage, and clipping. Prefer a dedicated container when a local control needs a positioning reference. For settings dialog changes, verify detail overlays, the top drag area, and scrollbar placement at different window widths
+8. **Built-in custom attributes:** Use the `custom-sy-` prefix for custom attributes owned by built-in features
+
---
## 4. Required toolchain
diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.ts
index 38526b458d..50b56de494 100644
--- a/app/src/protyle/gutter/index.ts
+++ b/app/src/protyle/gutter/index.ts
@@ -2193,8 +2193,8 @@ export class Gutter {
if (type === "NodeList") {
submenu.push({
id: "listMindmap",
- icon: nodeElement.getAttribute("custom-list-mindmap") === "1" ? "iconList" : "iconMindmap",
- label: nodeElement.getAttribute("custom-list-mindmap") === "1" ?
+ icon: nodeElement.getAttribute("custom-sy-list-mindmap") === "1" ? "iconList" : "iconMindmap",
+ label: nodeElement.getAttribute("custom-sy-list-mindmap") === "1" ?
window.siyuan.languages.listMindmapToList : window.siyuan.languages.listMindmapToMindmap,
click() {
hideElements(["select"], protyle);
@@ -3745,7 +3745,7 @@ export class Gutter {
let html = "";
let nodeElement = selectedElement || element;
const mindmapElement = nodeElement.getAttribute("data-type") === "NodeList" &&
- nodeElement.getAttribute("custom-list-mindmap") === "1" ?
+ nodeElement.getAttribute("custom-sy-list-mindmap") === "1" ?
nodeElement.querySelector(":scope > .list-mindmap") : null;
const tabsHeader = !isMultiSelect && nodeElement.getAttribute("data-type") === "NodeTabs" ?
nodeElement.querySelector(":scope > .tabs-header") : null;
diff --git a/app/src/protyle/render/listMindmap/model.test.ts b/app/src/protyle/render/listMindmap/model.test.ts
index 87b470ec3e..b6bf94830d 100644
--- a/app/src/protyle/render/listMindmap/model.test.ts
+++ b/app/src/protyle/render/listMindmap/model.test.ts
@@ -153,7 +153,7 @@ const browserCases = async (sourceCode: string, css: string) => {
item.getAttribute("data-marker")), ["0.", "1."]);
// 删除节点同步清理已删除子树的关系线和样式,其他关系及最后一个实际节点保留。
- list.setAttribute("custom-list-mindmap-data", JSON.stringify({version: 1,
+ list.setAttribute("custom-sy-list-mindmap-data", JSON.stringify({version: 1,
nodes: {[second.id]: {bold: true}, [third.id]: {textColor: "red"}, [first.id]: {italic: true}},
relations: [{id: "r1", from: second.id, to: first.id, label: "remove"},
{id: "r2", from: third.id, to: first.id, label: "remove child"},
@@ -172,7 +172,7 @@ const browserCases = async (sourceCode: string, css: string) => {
check.equal(inserted.dataset.marker, "1.");
// 未知版本或损坏配置在任何编辑之前报错,原内容保持不变。
- list.setAttribute("custom-list-mindmap-data", '{"version":2,"nodes":{},"relations":[]}');
+ list.setAttribute("custom-sy-list-mindmap-data", '{"version":2,"nodes":{},"relations":[]}');
const corrupt = list.outerHTML;
check.throws(() => api.moveListMindmapNode(list, inserted.dataset.nodeId, first.id, "after"), /metadata/);
check.throws(() => api.deleteListMindmapNode(list, inserted.dataset.nodeId), /metadata/);
@@ -232,7 +232,7 @@ const browserCases = async (sourceCode: string, css: string) => {
check.equal(childList.outerHTML, childHTML);
// 渲染和编辑标记不会进入持久化 DOM,列表视图属性及关系线配置仍然保留。
- list.setAttribute("custom-list-mindmap", "1");
+ list.setAttribute("custom-sy-list-mindmap", "1");
list.setAttribute("data-list-mindmap-rendered", "true");
list.setAttribute("data-list-mindmap-editing", "true");
const derived = document.createElement("div");
@@ -247,23 +247,23 @@ const browserCases = async (sourceCode: string, css: string) => {
check.equal(cleaned.includes("data-list-mindmap-editing"), false);
check.equal(cleaned.includes("<!--list-mindmap-->"), false);
check.equal(cleaned.includes("<!--unrelated list-mindmap comment-->"), true);
- check.equal(cleaned.includes('custom-list-mindmap="1"'), true);
+ check.equal(cleaned.includes('custom-sy-list-mindmap="1"'), true);
check.ok(list.contains(derived));
check.equal(api.cleanListMindmapHTML("<div>unrelated</div>"), "<div>unrelated</div>");
check.ok(lute.BlockDOM2Md(cleaned).includes("New child"));
// 复制后关系线与节点样式引用新块 ID,遇到任一损坏配置时不进行部分迁移。
- list.setAttribute("custom-list-mindmap-data", JSON.stringify({version: 1, extension: "keep",
+ list.setAttribute("custom-sy-list-mindmap-data", JSON.stringify({version: 1, extension: "keep",
nodes: {a: {bold: true}, b: {italic: true}},
relations: [{id: "relation", from: "a", to: "b", label: "keep"}]}));
const child = list.querySelector('[data-type="NodeList"]');
- child.setAttribute("custom-list-mindmap-data", '{"version":2,"nodes":{},"relations":[]}');
+ child.setAttribute("custom-sy-list-mindmap-data", '{"version":2,"nodes":{},"relations":[]}');
const beforeRemap = list.outerHTML;
check.throws(() => api.remapListMindmapIDs(list, new Map([["a", "new-a"], ["b", "new-b"]])), /metadata/);
check.equal(list.outerHTML, beforeRemap);
- child.removeAttribute("custom-list-mindmap-data");
+ child.removeAttribute("custom-sy-list-mindmap-data");
api.remapListMindmapIDs(list, new Map([["a", "new-a"], ["b", "new-b"]]));
- const remapped = JSON.parse(list.getAttribute("custom-list-mindmap-data"));
+ const remapped = JSON.parse(list.getAttribute("custom-sy-list-mindmap-data"));
check.deepEqual(remapped.nodes, {"new-a": {bold: true}, "new-b": {italic: true}});
check.deepEqual(remapped.relations, [{id: "relation", from: "new-a", to: "new-b", label: "keep"}]);
check.equal(remapped.extension, "keep");
diff --git a/app/src/protyle/render/listMindmap/model.ts b/app/src/protyle/render/listMindmap/model.ts
index 749c9c6227..ea99a45f0c 100644
--- a/app/src/protyle/render/listMindmap/model.ts
+++ b/app/src/protyle/render/listMindmap/model.ts
@@ -1,7 +1,7 @@
import {getOrderedListMarkerUpdates} from "../../wysiwyg/listContext";
-export const LIST_MINDMAP_VIEW_ATTRIBUTE = "custom-list-mindmap";
-export const LIST_MINDMAP_META_ATTRIBUTE = "custom-list-mindmap-data";
+export const LIST_MINDMAP_VIEW_ATTRIBUTE = "custom-sy-list-mindmap";
+export const LIST_MINDMAP_META_ATTRIBUTE = "custom-sy-list-mindmap-data";
export interface ListMindmapNodeStyle {
textColor?: string;
diff --git a/app/src/protyle/wysiwyg/transaction.ts b/app/src/protyle/wysiwyg/transaction.ts
index f362cd2064..7f7c8cd598 100644
--- a/app/src/protyle/wysiwyg/transaction.ts
+++ b/app/src/protyle/wysiwyg/transaction.ts
@@ -74,7 +74,7 @@ import {
} from "./blockSelection";
import {isEmptyParagraph} from "./emptyTextBlock";
import {cleanTableCellRichHTML, retainTableCellRichMetadata} from "../util/tableCellRich";
-import {cleanListMindmapHTML} from "../render/listMindmap/model";
+import {cleanListMindmapHTML, LIST_MINDMAP_VIEW_ATTRIBUTE} from "../render/listMindmap/model";
import {completeTabsListSource, convertTabsList, isTabsListConversion} from "./tabsList";
import {waitForPendingTransactions} from "../util/transactionQueue";
import {
@@ -147,12 +147,21 @@ const syncBlockAttrs = (element: Element, operation: Extract<IOperation, {action
const attrs = JSON.parse(operation.data);
const hasFold = Object.prototype.hasOwnProperty.call(attrs, "fold");
const hasStyle = Object.prototype.hasOwnProperty.call(attrs, "style");
+ const hasMindmapView = Object.prototype.hasOwnProperty.call(attrs, LIST_MINDMAP_VIEW_ATTRIBUTE);
const tabsAttrs = ["tabs-active-id", "tabs-position", "tabs-task"]
.filter(name => Object.prototype.hasOwnProperty.call(attrs, name));
- if (!hasFold && !hasStyle && tabsAttrs.length === 0) {
+ if (!hasFold && !hasStyle && !hasMindmapView && tabsAttrs.length === 0) {
return;
}
element.querySelectorAll(`[data-node-id="${operation.id}"]`).forEach(item => {
+ // 同步脑图视图属性,由列表监听器刷新各分屏中的视图。
+ if (hasMindmapView) {
+ if (attrs[LIST_MINDMAP_VIEW_ATTRIBUTE]) {
+ item.setAttribute(LIST_MINDMAP_VIEW_ATTRIBUTE, attrs[LIST_MINDMAP_VIEW_ATTRIBUTE]);
+ } else {
+ item.removeAttribute(LIST_MINDMAP_VIEW_ATTRIBUTE);
+ }
+ }
if (hasFold) {
if (attrs.fold === "1") {
item.setAttribute("fold", "1");
diff --git a/app/src/protyle/wysiwyg/transactionAttrs.test.ts b/app/src/protyle/wysiwyg/transactionAttrs.test.ts
new file mode 100644
index 0000000000..92adf12648
--- /dev/null
+++ b/app/src/protyle/wysiwyg/transactionAttrs.test.ts
@@ -0,0 +1,44 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import {test} from "node:test";
+import {runInNewContext} from "node:vm";
+import {createSourceFile, isVariableStatement, ScriptTarget, transpileModule} from "typescript";
+
+const source = createSourceFile("transaction.ts", readFileSync("src/protyle/wysiwyg/transaction.ts", "utf8"),
+ ScriptTarget.ES2021, true);
+const declaration = source.statements.find(statement => isVariableStatement(statement) &&
+ statement.declarationList.declarations.some(item => item.name.getText(source) === "syncBlockAttrs"));
+const attribute = "custom-sy-list-mindmap";
+const context = {LIST_MINDMAP_VIEW_ATTRIBUTE: attribute, sync: undefined as any};
+runInNewContext(transpileModule(declaration.getText(source) + "\nglobalThis.sync = syncBlockAttrs;", {
+ compilerOptions: {target: ScriptTarget.ES2021},
+}).outputText, context);
+
+test("mind map view transactions synchronize matching blocks when toggling, undoing and redoing", () => {
+ const copies = [new Map<string, string>(), new Map<string, string>()];
+ const root = {
+ querySelectorAll(selector: string) {
+ assert.equal(selector, '[data-node-id="list-id"]');
+ return copies.map(attrs => ({
+ setAttribute: (name: string, value: string) => attrs.set(name, value),
+ removeAttribute: (name: string) => attrs.delete(name),
+ }));
+ },
+ };
+ for (const value of ["1", "", "1", ""]) {
+ context.sync(root, {action: "setAttrs", id: "list-id", data: JSON.stringify({[attribute]: value})});
+ copies.forEach(attrs => assert.equal(attrs.get(attribute), value || undefined));
+ }
+});
+
+test("unrelated attribute transactions preserve the mind map view", () => {
+ const attrs = new Map([[attribute, "1"]]);
+ const root = {querySelectorAll: () => [{
+ setAttribute: (name: string, value: string) => attrs.set(name, value),
+ removeAttribute: (name: string) => attrs.delete(name),
+ }]};
+ context.sync(root, {action: "setAttrs", id: "list-id", data: JSON.stringify({style: "color: red", fold: "1"})});
+ assert.equal(attrs.get(attribute), "1");
+ assert.equal(attrs.get("style"), "color: red");
+ assert.equal(attrs.get("fold"), "1");
+});
diff --git a/docs/LIST-MINDMAP.md b/docs/LIST-MINDMAP.md
index 1a3a16f633..bf801e7dcd 100644
--- a/docs/LIST-MINDMAP.md
+++ b/docs/LIST-MINDMAP.md
@@ -24,7 +24,7 @@
正文仍存放在原列表和原列表项中,搜索、块引用和反链继续使用原块 ID。单个顶层列表项直接作为根节点;多个顶层项使用可自定义标题的虚拟中心节点,它不新增实际块,标题存放在脑图属性的可选 `rootTitle` 字段中。
-列表块使用 `custom-list-mindmap="1"` 启用脑图视图。`custom-list-mindmap-data` 保存版本为 `1` 的节点样式和关系线配置。关闭脑图视图保留该配置;折叠使用列表项的已有 `fold` 属性。未知版本或损坏的配置不会被自动覆盖,原列表仍可查看。
+列表块使用 `custom-sy-list-mindmap="1"` 启用脑图视图。`custom-sy-list-mindmap-data` 保存版本为 `1` 的节点样式和关系线配置。关闭脑图视图保留该配置;折叠使用列表项的已有 `fold` 属性。未知版本或损坏的配置不会被自动覆盖,原列表仍可查看。
修改前补齐折叠隐藏的列表内容。结构、正文和配置修改通过已有文档事务保存,并进入文档撤销记录。复制列表时,样式所关联的节点 ID 和关系线的端点随复制后的块 ID 一起更新。