Skip to content

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

:lipstick: Improve mind map layout density https://github.com/siyuan-note/siyuan/issues/20676

Daniel839f1c6ec1a2
diff --git a/app/src/protyle/render/listMindmap/model.test.ts b/app/src/protyle/render/listMindmap/model.test.ts
index 2ec0df584e..3f4520681d 100644
--- a/app/src/protyle/render/listMindmap/model.test.ts
+++ b/app/src/protyle/render/listMindmap/model.test.ts
@@ -5,8 +5,11 @@ import {tmpdir} from "node:os";
import * as path from "node:path";
import {execFile} from "node:child_process";
import {promisify} from "node:util";
+import {runInNewContext} from "node:vm";
import type {ListMindmapLayoutNode, ListMindmapNode} from "./model";
import type {ListMindmapFoldTarget} from "./fold";
+import {routeMindmapRelation} from "./routing";
+import {layoutListMindmapSummaries} from "./summary";
import {getListConversionType, type TListSubtype} from "../../wysiwyg/listContext";
const buildGlobals = ["SIYUAN_VERSION", "NODE_ENV"].map(name => ({
@@ -49,15 +52,91 @@ test("relation label spacing only expands the requested branch boundary", () =>
const root = measured("root", 100, 40, [measured("a"), measured("b"), measured("c")]);
const expanded = layoutListMindmap(root, {verticalGaps: new Map([["b", 64]])}).nodes;
assert.equal(expanded.get("b").y - expanded.get("a").y - 40, 64);
- assert.equal(expanded.get("c").y - expanded.get("b").y - 40, 24);
+ assert.equal(expanded.get("c").y - expanded.get("b").y - 40, 16);
const normal = layoutListMindmap(root).nodes;
- assert.equal(normal.get("b").y - normal.get("a").y - 40, 24);
+ assert.equal(normal.get("b").y - normal.get("a").y - 40, 16);
const horizontal = layoutListMindmap(root, {horizontalGaps: new Map([["b", 150]])}).nodes;
assert.equal(horizontal.get("b").x - horizontal.get("root").x - 100, 150);
assert.equal(horizontal.get("a").x - horizontal.get("root").x - 100, 40);
assert.equal(horizontal.get("c").x - horizontal.get("root").x - 100, 40);
});
+test("default sibling packing reduces height while preserving node sizes and control clearance", () => {
+ const root = measured("root", 100, 40, [measured("a", 80, 32), measured("b", 160, 80),
+ measured("c", 120, 160), measured("d", 60, 32)]);
+ const snapshot = JSON.stringify(root);
+ const previous = layoutListMindmap(root, {verticalGap: 24});
+ const compact = layoutListMindmap(root);
+ assert.ok(compact.height < previous.height);
+ assert.equal(compact.width, previous.width);
+ root.children.forEach((child, index) => {
+ const position = compact.nodes.get(child.id);
+ assert.equal(position.width, child.width);
+ assert.equal(position.height, child.height);
+ if (index) {
+ const above = compact.nodes.get(root.children[index - 1].id);
+ assert.ok(position.y > above.y + above.height + 14, "the lower hover corridor stays clear of its sibling");
+ }
+ assert.ok(position.x >= compact.nodes.get(root.id).x + root.width + 39,
+ "fold and add-child buttons retain their horizontal space");
+ });
+ assert.equal(JSON.stringify(root), snapshot);
+});
+
+test("compact view spacing retains draggable relations without expanding unrelated siblings", () => {
+ const typescript = require("typescript") as typeof import("typescript");
+ const source = typescript.createSourceFile("view.ts", readFileSync(path.join(__dirname, "view.ts"), "utf8"),
+ typescript.ScriptTarget.ES2021, true);
+ const declaration = source.statements.find(typescript.isClassDeclaration);
+ const refresh = declaration.members.find(member => member.name?.getText(source) === "refreshLayout");
+ const compiled = typescript.transpileModule(`class View {${refresh.getText(source)}} globalThis.View = View;`, {
+ compilerOptions: {target: typescript.ScriptTarget.ES2021},
+ }).outputText;
+ let frame: () => void;
+ const context: any = {layoutListMindmap, layoutListMindmapSummaries,
+ requestAnimationFrame: (callback: () => void) => { frame = callback; return 1; }};
+ runInNewContext(compiled, context);
+ const view = new context.View();
+ const root = measured("root", 100, 32, [measured("a", 100, 32), measured("b", 100, 32), measured("c", 100, 32)]);
+ const elements = new Map([root, ...root.children].map(node => [node.id,
+ {offsetWidth: node.width, offsetHeight: node.height, hidden: false, style: {}}]));
+ const model = {root, nodes: new Map([root, ...root.children].map(node => [node.id, node])),
+ metadata: {version: 1, nodes: {}, relations: [] as {id: string, from: string, to: string, label: string}[]}};
+ Object.assign(view, {model, options: {}, frame: 0, positions: new Map(), folded: new Map(),
+ nodeElements: elements, relationElements: new Map(), summaryElements: new Map(), relationRoutes: new Map(),
+ fallbackRoutes: new Set(), anchorFallbackRoutes: new Set(), offsetX: 0, offsetY: 0, scale: 1,
+ viewport: {clientWidth: 760, clientHeight: 460},
+ calculateRelationRoute: (_relation: unknown, from: Parameters<typeof routeMindmapRelation>[0],
+ to: Parameters<typeof routeMindmapRelation>[1]) =>
+ routeMindmapRelation(from, to, [...view.positions.values()]),
+ boundedPan: (x: number, y: number) => ({x, y}), draw: () => {},
+ });
+ const layout = () => { view.refreshLayout(); frame(); };
+ const gap = (from: string, to: string) => view.positions.get(to).y - view.positions.get(from).y -
+ view.positions.get(from).height;
+ layout();
+ const compactGap = gap("a", "b");
+ model.metadata.relations.push({id: "relation", from: "a", to: "b", label: ""});
+ const snapshot = JSON.stringify(model);
+ layout();
+ const points = view.relationRoutes.get("relation") as {x: number, y: number}[];
+ assert.ok(points.slice(1).some((point, index) =>
+ Math.hypot(point.x - points[index].x, point.y - points[index].y) >= 12),
+ "an unlabeled relation retains a segment long enough for its drag handle");
+ assert.ok(gap("a", "b") > compactGap);
+ assert.equal(gap("b", "c"), compactGap);
+ assert.equal(JSON.stringify(model), snapshot);
+ assert.equal(view.scale, 1);
+ model.metadata.relations[0].label = "Connection";
+ view.relationElements.set("relation", {offsetWidth: 120, offsetHeight: 20, hidden: true});
+ layout();
+ assert.ok(gap("a", "b") >= 20 + 32, "relation text reserves its measured height and clearance");
+ assert.equal(gap("b", "c"), compactGap);
+ model.metadata.relations = [];
+ layout();
+ assert.equal(gap("a", "b"), compactGap);
+});
+
test("mindmap metadata rejects unknown formats and invalid values without replacing original data", () => {
const value = '{"version":1,"nodes":{"a":{"bold":true,"fontSize":18,"extension":"retained"}},' +
'"relations":[{"id":"r","from":"a","to":"b","label":"label","dash":true}],"extension":42}';
@@ -2254,7 +2333,12 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,
model.metadata.relations[0].label = "";
view.update(model);
await settle();
- check.equal(siblingGap(), originalGap, "removing the label restores the default spacing");
+ check.ok(siblingGap() > originalGap, "an unlabeled relation keeps space for its drag handle");
+ check.ok(siblingGap() < beforeHandleGap, "removing the label releases its extra text spacing");
+ model.metadata.relations = [];
+ view.update(model);
+ await settle();
+ check.equal(siblingGap(), originalGap, "removing the relation restores the compact sibling spacing");
model.metadata.relations = [
{id: "forward", from: alpha, to: beta, label: ""},
{id: "reverse", from: beta, to: alpha, label: ""},
diff --git a/app/src/protyle/render/listMindmap/model.ts b/app/src/protyle/render/listMindmap/model.ts
index ce7ef515f9..919d7e3195 100644
--- a/app/src/protyle/render/listMindmap/model.ts
+++ b/app/src/protyle/render/listMindmap/model.ts
@@ -479,7 +479,7 @@ export const convertListMindmapToList = (element: Element, type: string, lute: L
return materializeListMindmapRoot(list, lute) ? lute.SpinBlockDOM(list.outerHTML) : converted;
};
-// 按每层最大宽度对齐节点,并为每个分支保留完整的垂直空间,避免富文本节点相互遮挡。
+// 按父节点宽度排列分支,保留节点下方控件的操作空间,避免富文本节点相互遮挡。
export const layoutListMindmap = (root: ListMindmapLayoutNode, options: {
horizontalGap?: number;
verticalGap?: number;
@@ -488,8 +488,9 @@ export const layoutListMindmap = (root: ListMindmapLayoutNode, options: {
verticalGaps?: Map<string, number>;
summaries?: {nodeIds: string[], height: number}[];
} = {}) => {
+ // 横向间距容纳右侧两枚按钮,纵向间距避开节点下方 14px 的悬停区域。
const horizontalGap = options.horizontalGap ?? 40;
- const verticalGap = options.verticalGap ?? 24;
+ const verticalGap = options.verticalGap ?? 16;
const padding = options.padding ?? 32;
const gapBefore = (id: string) => Math.max(verticalGap, options.verticalGaps?.get(id) || 0);
if ([horizontalGap, verticalGap, padding].some(value => !Number.isFinite(value) || value < 0)) {
diff --git a/app/src/protyle/render/listMindmap/view.ts b/app/src/protyle/render/listMindmap/view.ts
index 7bfd13a4b7..f228a0264e 100644
--- a/app/src/protyle/render/listMindmap/view.ts
+++ b/app/src/protyle/render/listMindmap/view.ts
@@ -1059,30 +1059,30 @@ export class ListMindmapView {
}
return path;
};
- // 只撑开关系线两端之间的分支边界,其他节点保留默认间距。
+ // 关系线两端保留拖拽线段和文字的空间,只撑开所跨分支,其他节点保留紧凑间距。
this.model.metadata.relations.forEach(relation => {
- if (!relation.label?.trim() || !result.nodes.has(relation.from) || !result.nodes.has(relation.to)) {
+ if (!result.nodes.has(relation.from) || !result.nodes.has(relation.to)) {
return;
}
- const label = this.relationElements.get(relation.id);
+ const label = relation.label?.trim() ? this.relationElements.get(relation.id) : undefined;
if (label) {
label.hidden = false;
- const from = result.nodes.get(relation.from);
- const to = result.nodes.get(relation.to);
- const fromPath = ancestors(from.id);
- const toPath = ancestors(to.id);
- let common = 0;
- while (common < Math.min(fromPath.length, toPath.length) && fromPath[common] === toPath[common]) {
- common++;
- }
- if (common < fromPath.length && common < toPath.length) {
- const lower = from.y < to.y ? toPath[common] : fromPath[common];
- reserve(verticalGaps, lower, label.offsetHeight + 32);
- }
- if (from.x + from.width <= to.x || to.x + to.width <= from.x) {
- const right = from.x < to.x ? to : from;
- reserve(horizontalGaps, right.id, label.offsetWidth + 32);
- }
+ }
+ const from = result.nodes.get(relation.from);
+ const to = result.nodes.get(relation.to);
+ const fromPath = ancestors(from.id);
+ const toPath = ancestors(to.id);
+ let common = 0;
+ while (common < Math.min(fromPath.length, toPath.length) && fromPath[common] === toPath[common]) {
+ common++;
+ }
+ if (common < fromPath.length && common < toPath.length) {
+ const lower = from.y < to.y ? toPath[common] : fromPath[common];
+ reserve(verticalGaps, lower, label ? label.offsetHeight + 32 : 24);
+ }
+ if (label && (from.x + from.width <= to.x || to.x + to.width <= from.x)) {
+ const right = from.x < to.x ? to : from;
+ reserve(horizontalGaps, right.id, label.offsetWidth + 32);
}
});
if (horizontalGaps.size || verticalGaps.size) {