Skip to content

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

:sparkles: Align mind map summary connectors and previews https://github.com/siyuan-note/siyuan/issues/20732

Daniel4a1e01e7d5e2
diff --git a/app/src/protyle/render/listMindmap/connectors.test.ts b/app/src/protyle/render/listMindmap/connectors.test.ts
index 3415e96ccc..17ddcc25bb 100644
--- a/app/src/protyle/render/listMindmap/connectors.test.ts
+++ b/app/src/protyle/render/listMindmap/connectors.test.ts
@@ -1,6 +1,6 @@
import * as assert from "node:assert/strict";
import test from "node:test";
-import {buildListMindmapConnectors, LIST_MINDMAP_CONNECTOR_STYLES} from "./connectors";
+import {buildListMindmapConnectors, buildListMindmapSummaryConnector, LIST_MINDMAP_CONNECTOR_STYLES} from "./connectors";
import {getListMindmapConnectorMenuItem} from "./connectorMenu";
import type {ListMindmapPosition} from "./model";
@@ -85,3 +85,30 @@ test("the connector submenu checks the saved preset and selects exactly one whol
assert.deepEqual(selected, [...LIST_MINDMAP_CONNECTOR_STYLES]);
}
});
+
+test("summary connectors follow all four styles while retaining their range and label endpoints", () => {
+ const position = {id: "summary", x: 200, top: 10, bottom: 110, labelX: 224, labelY: 40, width: 100, height: 40};
+ const shapes = LIST_MINDMAP_CONNECTOR_STYLES.map(style =>
+ buildListMindmapSummaryConnector(position, style, () => new PathFixture()).commands);
+ assert.equal(new Set(shapes.map(commands => JSON.stringify(commands))).size, 4);
+ shapes.forEach(commands => {
+ const points = commands.flatMap(command => {
+ const values = command.slice(1) as number[];
+ return values.filter((_value, index) => index % 2 === 0).map((x, index) => [x, values[index * 2 + 1]]);
+ });
+ for (const point of [[190, 10], [190, 110], [218, 60]]) {
+ assert.ok(points.some(candidate => candidate[0] === point[0] && candidate[1] === point[1]));
+ }
+ assert.ok(points.every(([x, y]) => x >= 190 && x <= 218 && y >= 10 && y <= 110));
+ });
+ assert.ok(shapes[0].some(command => command[0] === "bezier"));
+ assert.ok(shapes[1].every(command => command[0] === "move" || command[0] === "line"));
+ assert.ok(shapes[2].some(command => command[0] === "bezier"));
+ assert.ok(shapes[3].some(command => command[0] === "quadratic"));
+ for (const height of [0, 1, 8]) {
+ const commands = buildListMindmapSummaryConnector({...position, bottom: position.top + height},
+ "rounded", () => new PathFixture()).commands;
+ const ys = commands.flatMap(command => command.slice(2).filter((_value, index) => index % 2 === 0)) as number[];
+ assert.ok(ys.every(y => y >= position.top && y <= position.top + height));
+ }
+});
diff --git a/app/src/protyle/render/listMindmap/connectors.ts b/app/src/protyle/render/listMindmap/connectors.ts
index 8ee6c6cba9..120fd998ca 100644
--- a/app/src/protyle/render/listMindmap/connectors.ts
+++ b/app/src/protyle/render/listMindmap/connectors.ts
@@ -1,10 +1,44 @@
import type {ListMindmapPosition} from "./model";
+import type {ListMindmapSummaryPosition} from "./summary";
export const LIST_MINDMAP_CONNECTOR_STYLES = ["spline", "orthogonal", "bezier", "rounded"] as const;
export type ListMindmapConnectorStyle = typeof LIST_MINDMAP_CONNECTOR_STYLES[number];
type ConnectorPath = Pick<CanvasPath, "moveTo" | "lineTo" | "bezierCurveTo" | "quadraticCurveTo">;
+// 概要范围与标签之间的连线使用导图样式,预览、绘制和命中检测共用路径。
+export const buildListMindmapSummaryConnector = <T extends ConnectorPath>(
+ position: ListMindmapSummaryPosition, style: ListMindmapConnectorStyle, makePath: () => T,
+) => {
+ const {x, top, bottom, labelX} = position;
+ const startX = x - 10;
+ const endX = labelX - 6;
+ const centerY = (top + bottom) / 2;
+ const path = makePath();
+ if (style === "spline" || style === "bezier") {
+ const centerX = (startX + endX) / 2;
+ [top, bottom].forEach(y => {
+ path.moveTo(endX, centerY);
+ path.bezierCurveTo(style === "spline" ? endX : centerX, centerY, centerX, y, startX, y);
+ });
+ } else {
+ const radius = style === "rounded" ? Math.min(6, Math.max(0, bottom - top) / 2) : 0;
+ path.moveTo(startX, top);
+ path.lineTo(x - radius, top);
+ if (radius) {
+ path.quadraticCurveTo(x, top, x, top + radius);
+ }
+ path.lineTo(x, bottom - radius);
+ if (radius) {
+ path.quadraticCurveTo(x, bottom, x - radius, bottom);
+ }
+ path.lineTo(startX, bottom);
+ path.moveTo(x, centerY);
+ path.lineTo(endX, centerY);
+ }
+ return path;
+};
+
// 连线路径只使用已完成的节点布局,共享主干不进入分支的命中路径。
export const buildListMindmapConnectors = <T extends ConnectorPath>(
rootID: string, positions: Map<string, ListMindmapPosition>, edges: {from: string, to: string}[],
diff --git a/app/src/protyle/render/listMindmap/summaryView.test.ts b/app/src/protyle/render/listMindmap/summaryView.test.ts
new file mode 100644
index 0000000000..fdffb2c666
--- /dev/null
+++ b/app/src/protyle/render/listMindmap/summaryView.test.ts
@@ -0,0 +1,63 @@
+import * as assert from "node:assert/strict";
+import {readFileSync} from "node:fs";
+import test from "node:test";
+import {runInNewContext} from "node:vm";
+import {createSourceFile, isClassDeclaration, ScriptTarget, transpileModule} from "typescript";
+import {buildListMindmapSummaryConnector, LIST_MINDMAP_CONNECTOR_STYLES} from "./connectors";
+import {layoutListMindmapSummaries} from "./summary";
+import type {ListMindmapModel, ListMindmapNode} from "./model";
+
+const source = createSourceFile("view.ts", readFileSync("src/protyle/render/listMindmap/view.ts", "utf8"),
+ ScriptTarget.ES2021, true);
+const declaration = source.statements.find(item => isClassDeclaration(item) && item.name?.text === "ListMindmapView");
+assert.ok(declaration && isClassDeclaration(declaration));
+const methods = declaration.members.filter(item => ["drawSummaries", "drawSummaryPreview"].includes(item.name?.getText(source)));
+assert.equal(methods.length, 2);
+const compiled = transpileModule(`class View { ${methods.map(item => item.getText(source)).join("\n")} }
+globalThis.View = View;`, {compilerOptions: {target: ScriptTarget.ES2021}}).outputText;
+
+class PathFixture {
+ commands: (string | number)[][] = [];
+ moveTo(...values: number[]) { this.commands.push(["move", ...values]); }
+ lineTo(...values: number[]) { this.commands.push(["line", ...values]); }
+ bezierCurveTo(...values: number[]) { this.commands.push(["bezier", ...values]); }
+ quadraticCurveTo(...values: number[]) { this.commands.push(["quadratic", ...values]); }
+}
+
+test("summary previews and committed paths match across styles, zoom levels and the legacy default", () => {
+ for (const connectorStyle of [undefined, ...LIST_MINDMAP_CONNECTOR_STYLES]) {
+ for (const scale of [.5, 1, 2]) {
+ const sandbox: any = {Path2D: PathFixture, buildListMindmapSummaryConnector, layoutListMindmapSummaries};
+ runInNewContext(compiled, sandbox);
+ const view = new sandbox.View();
+ const child: ListMindmapNode = {id: "a", parentId: "root", children: [], contentBlocks: [], virtual: false, collapsed: false};
+ const root: ListMindmapNode = {id: "root", children: [child], contentBlocks: [], virtual: true, collapsed: false};
+ const range = {parentId: "root", nodeIds: ["a"]};
+ const model: ListMindmapModel = {list: undefined, root, nodes: new Map([["root", root], ["a", child]]),
+ metadata: {version: 1, nodes: {}, relations: [], connectorStyle}};
+ const positions = new Map([["a", {id: "a", x: 100, y: 40, width: 120, height: 50}]]);
+ Object.assign(view, {model, positions, summaryPositions: new Map(), summaryRanges: [range],
+ summarySelecting: true, options: {}, scale, linePaths: [], summaryElements: new Map()});
+ const strokes: PathFixture[] = [];
+ const context = {save: () => {}, restore: () => {}, setLineDash: () => {},
+ stroke: (path: PathFixture) => strokes.push(path)};
+ view.drawSummaryPreview(context, "blue");
+ assert.equal(strokes.length, 1);
+ const preview = strokes.pop();
+ Object.assign(model.metadata, {summaries: [{...range, id: "summary", label: "Summary"}]});
+ view.summaryPositions = layoutListMindmapSummaries(model, positions,
+ new Map([["summary", {width: 100, height: 40}]]));
+ view.summaryElements.set("summary", {style: {}, classList: {toggle: () => {}}, setAttribute: () => {}});
+ view.drawSummaries(context, () => ({color: "blue", width: 1.5}));
+ assert.equal(strokes.length, 1);
+ assert.deepEqual(strokes[0].commands, preview.commands);
+ assert.equal(view.linePaths[0].path, strokes[0], "hit testing uses the rendered shape");
+ assert.deepEqual(strokes[0].commands, buildListMindmapSummaryConnector(
+ view.summaryPositions.get("summary"), connectorStyle || "bezier", () => new PathFixture()).commands);
+ strokes.length = 0;
+ view.options.printLayout = true;
+ view.drawSummaryPreview(context, "blue");
+ assert.equal(strokes.length, 0, "creation previews are excluded from exports");
+ }
+ }
+});
diff --git a/app/src/protyle/render/listMindmap/view.ts b/app/src/protyle/render/listMindmap/view.ts
index 5ef6544f69..4a49a66c8e 100644
--- a/app/src/protyle/render/listMindmap/view.ts
+++ b/app/src/protyle/render/listMindmap/view.ts
@@ -21,7 +21,7 @@ import {clampMindmapPanOffset, clampMindmapPanPosition, MindmapPanRect} from "./
import {getListMindmapSummaryParentID, getListMindmapSummaryRange, getListMindmapSummarySelection, getListMindmapSummarySnapshot,
layoutListMindmapSummaries} from "./summary";
import type {ListMindmapSummary, ListMindmapSummaryPosition, ListMindmapSummaryRange} from "./summary";
-import {buildListMindmapConnectors} from "./connectors";
+import {buildListMindmapConnectors, buildListMindmapSummaryConnector} from "./connectors";
export interface ListMindmapViewOptions {
host: HTMLElement;
@@ -1394,19 +1394,9 @@ export class ListMindmapView {
if (!position) {
return;
}
- const {x, top, bottom, labelX, labelY} = position;
- const radius = Math.min(6, (bottom - top) / 2);
- const path = new Path2D();
- path.moveTo(x - 10, top);
- if (bottom > top) {
- path.lineTo(x - radius, top);
- path.quadraticCurveTo(x, top, x, top + radius);
- path.lineTo(x, bottom - radius);
- path.quadraticCurveTo(x, bottom, x - radius, bottom);
- path.lineTo(x - 10, bottom);
- path.moveTo(x, (top + bottom) / 2);
- }
- path.lineTo(labelX - 6, (top + bottom) / 2);
+ const {labelX, labelY} = position;
+ const path = buildListMindmapSummaryConnector(position,
+ this.model.metadata.connectorStyle || "bezier", () => new Path2D());
const selected = this.selectedSummary === summary.id && !this.printTransform && !this.options.printLayout;
const hovered = this.hoveredLine === `summary:${summary.id}` && !this.printTransform && !this.options.printLayout;
const style = resolveStyle(summary.color, selected, hovered);
@@ -1424,6 +1414,31 @@ export class ListMindmapView {
});
}
+ private drawSummaryPreview(context: CanvasRenderingContext2D, color: string) {
+ if (!this.summarySelecting || this.printTransform || this.options.printLayout || !this.summaryRanges.length) {
+ return;
+ }
+ const previews = this.summaryRanges.map((range, index) => ({...range, id: `preview:${index}`, label: ""}));
+ const sizes = new Map([...this.summaryPositions].map(([id, position]) =>
+ [id, {width: position.width, height: position.height}]));
+ previews.forEach(preview => sizes.set(preview.id, {width: 0, height: 0}));
+ const model = {...this.model, metadata: {...this.model.metadata,
+ summaries: [...(this.model.metadata.summaries || []), ...previews]}};
+ const positions = layoutListMindmapSummaries(model, this.positions, sizes);
+ context.save();
+ context.strokeStyle = color;
+ context.lineWidth = 1.5 / this.scale;
+ context.setLineDash([4 / this.scale, 4 / this.scale]);
+ previews.forEach(preview => {
+ const position = positions.get(preview.id);
+ if (position) {
+ context.stroke(buildListMindmapSummaryConnector(position,
+ this.model.metadata.connectorStyle || "bezier", () => new Path2D()));
+ }
+ });
+ context.restore();
+ }
+
private updateRelations() {
this.relationElements.forEach((element, id) => {
if (!this.model.metadata.relations.some(relation => relation.id === id)) {
@@ -1926,31 +1941,7 @@ export class ListMindmapView {
this.drawRelationPreview(context, resolveColor(relationStyles[0].color, primary), relationStyles[0].width || 1.5);
this.drawSummaries(context, (color, selected, hovered) =>
resolveLineStyle(summaryStyles, color, undefined, defaultLine, selected, hovered));
- if (this.summarySelecting && !this.printTransform && !this.options.printLayout && this.summaryRanges.length) {
- const previews = this.summaryRanges.map((range, index) => ({...range, id: `preview:${index}`, label: ""}));
- const sizes = new Map([...this.summaryPositions].map(([id, position]) =>
- [id, {width: position.width, height: position.height}]));
- previews.forEach(preview => sizes.set(preview.id, {width: 0, height: 0}));
- const model = {...this.model, metadata: {...this.model.metadata,
- summaries: [...(this.model.metadata.summaries || []), ...previews]}};
- const positions = layoutListMindmapSummaries(model, this.positions, sizes);
- context.save();
- context.strokeStyle = primary;
- context.lineWidth = 1.5 / this.scale;
- context.setLineDash([4 / this.scale, 4 / this.scale]);
- previews.forEach(preview => {
- const position = positions.get(preview.id);
- if (position) {
- context.beginPath();
- context.moveTo(position.x - 10, position.top);
- context.lineTo(position.x, position.top);
- context.lineTo(position.x, position.bottom);
- context.lineTo(position.x - 10, position.bottom);
- context.stroke();
- }
- });
- context.restore();
- }
+ this.drawSummaryPreview(context, primary);
this.renderRouteControls();
}