Skip to content

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

:bug: Persist embedded mind map view changes to source lists https://github.com/siyuan-note/siyuan/issues/18831

Vanessa8e3d8a0f0bc1
diff --git a/app/src/protyle/render/listMindmap/index.ts b/app/src/protyle/render/listMindmap/index.ts
index 06daea5342..520b974a5b 100644
--- a/app/src/protyle/render/listMindmap/index.ts
+++ b/app/src/protyle/render/listMindmap/index.ts
@@ -30,12 +30,15 @@ import {openListMindmapEditor} from "./editor";
const roots = new WeakMap<IProtyle, {refresh: () => void, destroy: () => void}>();
-const canEdit = (owner: IProtyle, list: HTMLElement) => !owner.disabled && !owner.lite &&
+const canToggleView = (owner: IProtyle, list: HTMLElement) => !owner.disabled && !owner.lite &&
!owner.options.action.includes(Constants.CB_GET_HISTORY) &&
- !list.closest(".protyle-wysiwyg__embed") && list.closest(".protyle-wysiwyg") === owner.wysiwyg.element;
+ list.closest(".protyle-wysiwyg") === owner.wysiwyg.element;
+
+const canEdit = (owner: IProtyle, list: HTMLElement) => canToggleView(owner, list) &&
+ !list.closest(".protyle-wysiwyg__embed");
export const toggleListMindmap = (owner: IProtyle, list: HTMLElement) => {
- if (list.dataset.type !== "NodeList" || !canEdit(owner, list)) {
+ if (list.dataset.type !== "NodeList" || !canToggleView(owner, list)) {
return;
}
const previous = list.getAttribute(Constants.CUSTOM_SY_LIST_MINDMAP) || "";
@@ -164,7 +167,7 @@ class ListMindmapController {
if (this.activeEditor && !await this.activeEditor.finish()) {
return;
}
- if (canEdit(owner, list)) {
+ if (canToggleView(owner, list)) {
toggleListMindmap(owner, list);
} else {
this.destroy();
diff --git a/app/src/protyle/render/listMindmap/toggle.test.ts b/app/src/protyle/render/listMindmap/toggle.test.ts
new file mode 100644
index 0000000000..4f4ca64b1e
--- /dev/null
+++ b/app/src/protyle/render/listMindmap/toggle.test.ts
@@ -0,0 +1,66 @@
+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("index.ts", readFileSync("src/protyle/render/listMindmap/index.ts", "utf8"),
+ ScriptTarget.ES2021, true);
+const declarations = source.statements.filter(statement => isVariableStatement(statement) &&
+ statement.declarationList.declarations.some(item => ["canToggleView", "canEdit", "toggleListMindmap"].includes(item.name.getText(source))));
+const compiled = transpileModule(declarations.map(item => item.getText(source).replace(/^export /, "")).join("\n") +
+ "\nglobalThis.api = {canEdit, toggleListMindmap};", {compilerOptions: {target: ScriptTarget.ES2021}}).outputText;
+
+const setup = () => {
+ const root = {};
+ const embed = {};
+ const owner = {disabled: false, lite: false, options: {action: [] as string[]}, wysiwyg: {element: root}};
+ const attribute = "custom-sy-list-mindmap";
+ const attrs = new Map([[attribute, "1"]]);
+ const list = {
+ dataset: {type: "NodeList", nodeId: "source-list"},
+ closest: (selector: string) => selector === ".protyle-wysiwyg" ? root : embed,
+ getAttribute: (name: string) => attrs.get(name),
+ setAttribute: (name: string, value: string) => attrs.set(name, value),
+ };
+ const operations: {doOperations: any[], undoOperations: any[]}[] = [];
+ const context: any = {
+ Constants: {CB_GET_HISTORY: "history", CUSTOM_SY_LIST_MINDMAP: attribute},
+ roots: new WeakMap(), hideElements: () => {}, readListMindmap: () => {},
+ transaction: (actualOwner: unknown, doOperations: any[], undoOperations: any[]) => {
+ assert.equal(actualOwner, owner);
+ operations.push({doOperations, undoOperations});
+ },
+ };
+ runInNewContext(compiled, context);
+ return {owner, list, attrs, attribute, operations, api: context.api};
+};
+
+test("embedded mind maps persist view changes against the source list with undo", () => {
+ const {owner, list, attrs, attribute, operations, api} = setup();
+ assert.equal(api.canEdit(owner, list), false, "embedded node content remains read-only");
+ for (const next of ["", "1"]) {
+ const previous = attrs.get(attribute);
+ api.toggleListMindmap(owner, list);
+ const operation = operations[operations.length - 1];
+ assert.equal(operation.doOperations[0].action, "setAttrs");
+ assert.equal(operation.doOperations[0].id, "source-list");
+ assert.deepEqual(JSON.parse(operation.doOperations[0].data), {[attribute]: next});
+ assert.deepEqual(JSON.parse(operation.undoOperations[0].data), {[attribute]: previous});
+ assert.equal(attrs.get(attribute), next);
+ }
+ assert.equal(operations.length, 2);
+});
+
+test("read-only, history and lightweight editors cannot persist embedded view changes", () => {
+ for (const mode of ["disabled", "lite", "history"]) {
+ const {owner, list, operations, api} = setup();
+ if (mode === "history") {
+ owner.options.action.push("history");
+ } else {
+ owner[mode as "disabled" | "lite"] = true;
+ }
+ api.toggleListMindmap(owner, list);
+ assert.equal(operations.length, 0);
+ }
+});