Skip to content

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

:bug: Reserve space for mind map connection labels and compact PDF previews https://github.com/siyuan-note/siyuan/issues/18831

Vanessaa7ed0c3b3f85
diff --git a/app/src/protyle/export/index.ts b/app/src/protyle/export/index.ts
index 35fc99d403..0823925c32 100644
--- a/app/src/protyle/export/index.ts
+++ b/app/src/protyle/export/index.ts
@@ -325,6 +325,15 @@ const renderPDF = async (id: string) => {
max-width: 100%;
}
+ #preview .list-mindmap__toolbar {
+ display: none !important;
+ }
+
+ #preview .list-mindmap {
+ height: var(--list-mindmap-print-height, 420px);
+ min-height: 0;
+ }
+
#preview a.pdf-embedded-asset {
position: relative;
padding-right: 1em !important;
@@ -348,7 +357,7 @@ const renderPDF = async (id: string) => {
</style>
${getSnippetCSS()}
</head>
-<body style="-webkit-print-color-adjust: exact;">
+<body data-export-pdf="true" style="-webkit-print-color-adjust: exact;">
<div id="action">
<div style="flex: 1;overflow-y:auto;overflow-x:hidden">
<div class="b3-label">
diff --git a/app/src/protyle/render/listMindmap/model.test.ts b/app/src/protyle/render/listMindmap/model.test.ts
index 4f252b1c6b..d306a7131d 100644
--- a/app/src/protyle/render/listMindmap/model.test.ts
+++ b/app/src/protyle/render/listMindmap/model.test.ts
@@ -23,6 +23,19 @@ buildGlobals.forEach(({name, descriptor}) => {
const measured = (id: string, width = 100, height = 40,
children: ListMindmapLayoutNode[] = []): ListMindmapLayoutNode => ({id, width, height, children});
+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);
+ const normal = layoutListMindmap(root).nodes;
+ assert.equal(normal.get("b").y - normal.get("a").y - 40, 24);
+ 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("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}';
@@ -774,6 +787,40 @@ const browserCases = async (sourceCode: string, css: string) => {
check.equal(host.querySelector(".list-mindmap__relation--selected"), null);
const savedRelations = model.metadata.relations;
+ // 短连线文字撑开节点间距,文字仍位于所属连线上。
+ model.metadata.relations = [];
+ view.update(model);
+ await settle();
+ const siblingGap = () => view.positions.get(beta).y - view.positions.get(alpha).y - view.positions.get(alpha).height;
+ const originalGap = siblingGap();
+ model.metadata.relations = [{id: "short-label", from: alpha, to: beta, label: "Connection label"}];
+ view.update(model);
+ await settle();
+ const shortLabel = view.relationElements.get("short-label");
+ const fromBox = view.positions.get(alpha);
+ const toBox = view.positions.get(beta);
+ check.ok(siblingGap() >= shortLabel.offsetHeight + 32);
+ const labelRoute = view.relationPath("short-label", fromBox, toBox, shortLabel);
+ check.ok(labelRoute.labelPoint, "short relations always retain their text");
+ check.notEqual(shortLabel.style.visibility, "hidden");
+ for (const box of [fromBox, toBox]) {
+ const p = labelRoute.labelPoint;
+ check.equal(p.x + shortLabel.offsetWidth / 2 > box.x && p.x - shortLabel.offsetWidth / 2 < box.x + box.width &&
+ p.y + shortLabel.offsetHeight / 2 > box.y && p.y - shortLabel.offsetHeight / 2 < box.y + box.height, false);
+ }
+ const routePoints = view.relationRoutes.get("short-label");
+ check.ok(routePoints.slice(1).some((point: any, index: number) => {
+ const previous = routePoints[index];
+ const p = labelRoute.labelPoint;
+ return (point.x === previous.x && p.x === point.x && p.y >= Math.min(point.y, previous.y) &&
+ p.y <= Math.max(point.y, previous.y)) ||
+ (point.y === previous.y && p.y === point.y && p.x >= Math.min(point.x, previous.x) &&
+ p.x <= Math.max(point.x, previous.x));
+ }), "the label stays on its own connection");
+ model.metadata.relations[0].label = "";
+ view.update(model);
+ await settle();
+ check.equal(siblingGap(), originalGap, "removing the label restores the default 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 36e99c232e..00c1c0ef3c 100644
--- a/app/src/protyle/render/listMindmap/model.ts
+++ b/app/src/protyle/render/listMindmap/model.ts
@@ -249,10 +249,13 @@ export const layoutListMindmap = (root: ListMindmapLayoutNode, options: {
horizontalGap?: number;
verticalGap?: number;
padding?: number;
+ horizontalGaps?: Map<string, number>;
+ verticalGaps?: Map<string, number>;
} = {}) => {
const horizontalGap = options.horizontalGap ?? 40;
const verticalGap = options.verticalGap ?? 24;
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)) {
throw new Error("Invalid list mindmap layout spacing");
}
@@ -277,8 +280,8 @@ export const layoutListMindmap = (root: ListMindmapLayoutNode, options: {
const heights = new Map<string, number>();
[...ordered].reverse().forEach(({node}) => {
const children = node.collapsed ? [] : node.children;
- const childHeight = children.reduce((sum, child) => sum + heights.get(child.id), 0) +
- Math.max(0, children.length - 1) * verticalGap;
+ const childHeight = children.reduce((sum, child, index) => sum + heights.get(child.id) +
+ (index ? gapBefore(child.id) : 0), 0);
heights.set(node.id, Math.max(node.height, childHeight));
});
const tops = new Map([[root.id, padding]]);
@@ -292,7 +295,7 @@ export const layoutListMindmap = (root: ListMindmapLayoutNode, options: {
const position = {
id: node.id,
parentId,
- x: parent ? parent.x + parent.width + horizontalGap : padding,
+ x: parent ? parent.x + parent.width + Math.max(horizontalGap, options.horizontalGaps?.get(node.id) || 0) : padding,
y: top + (height - node.height) / 2,
width: node.width,
height: node.height,
@@ -303,12 +306,15 @@ export const layoutListMindmap = (root: ListMindmapLayoutNode, options: {
edges.push({from: parentId, to: node.id});
}
const children = node.collapsed ? [] : node.children;
- const childHeight = children.reduce((sum, child) => sum + heights.get(child.id), 0) +
- Math.max(0, children.length - 1) * verticalGap;
+ const childHeight = children.reduce((sum, child, index) => sum + heights.get(child.id) +
+ (index ? gapBefore(child.id) : 0), 0);
let childTop = top + (height - childHeight) / 2;
- children.forEach(child => {
+ children.forEach((child, index) => {
+ if (index) {
+ childTop += gapBefore(child.id);
+ }
tops.set(child.id, childTop);
- childTop += heights.get(child.id) + verticalGap;
+ childTop += heights.get(child.id);
});
});
return {nodes, edges, width: width + padding, height: heights.get(root.id) + padding * 2};
diff --git a/app/src/protyle/render/listMindmap/render.ts b/app/src/protyle/render/listMindmap/render.ts
index 4c72e48947..651b05dd77 100644
--- a/app/src/protyle/render/listMindmap/render.ts
+++ b/app/src/protyle/render/listMindmap/render.ts
@@ -43,6 +43,7 @@ export const listMindmapRender = (root: Element, cdn?: string) => {
list.appendChild(host);
const view = new ListMindmapView({
host, model, readOnly: true, labels: window.siyuan?.languages || {}, cdn,
+ printLayout: !!list.closest("[data-export-pdf]"),
onExit: () => {
view.destroy();
host.remove();
diff --git a/app/src/protyle/render/listMindmap/view.ts b/app/src/protyle/render/listMindmap/view.ts
index d42bc89b79..fceeba794a 100644
--- a/app/src/protyle/render/listMindmap/view.ts
+++ b/app/src/protyle/render/listMindmap/view.ts
@@ -16,6 +16,7 @@ export interface ListMindmapViewOptions {
host: HTMLElement;
model: ListMindmapModel;
readOnly?: boolean;
+ printLayout?: boolean;
labels?: Record<string, string>;
cdn?: string;
onOpenLink?: (href: string, event: MouseEvent) => void;
@@ -183,8 +184,12 @@ export class ListMindmapView {
if (this.printTransform) {
Object.assign(this, this.printTransform);
this.printTransform = undefined;
- this.options.host.style.removeProperty("--list-mindmap-print-height");
- this.draw();
+ if (this.options.printLayout) {
+ this.fitPrint();
+ } else {
+ this.options.host.style.removeProperty("--list-mindmap-print-height");
+ this.draw();
+ }
}
});
this.resizeObserver = new ResizeObserver(() => this.refreshLayout());
@@ -495,7 +500,50 @@ export class ListMindmapView {
};
const anchorId = this.editingId || this.foldAnchor?.id;
const previous = anchorId ? this.positions.get(anchorId) : undefined;
- const result = layoutListMindmap(makeLayoutNode(this.model.root.id));
+ const layoutRoot = makeLayoutNode(this.model.root.id);
+ let result = layoutListMindmap(layoutRoot);
+ const horizontalGaps = new Map<string, number>();
+ const verticalGaps = new Map<string, number>();
+ const reserve = (gaps: Map<string, number>, id: string, size: number) => {
+ gaps.set(id, Math.max(gaps.get(id) || 0, size));
+ };
+ const ancestors = (id: string) => {
+ const path: string[] = [];
+ while (id) {
+ path.unshift(id);
+ id = result.nodes.get(id)?.parentId;
+ }
+ return path;
+ };
+ // 只撑开关系线两端之间的分支边界,其他节点保留默认间距。
+ this.model.metadata.relations.forEach(relation => {
+ if (!relation.label?.trim() || !result.nodes.has(relation.from) || !result.nodes.has(relation.to)) {
+ return;
+ }
+ const label = this.relationElements.get(relation.id);
+ 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);
+ }
+ }
+ });
+ if (horizontalGaps.size || verticalGaps.size) {
+ result = layoutListMindmap(layoutRoot, {horizontalGaps, verticalGaps});
+ }
this.positions = result.nodes;
this.relationRoutes.clear();
this.edges = result.edges;
@@ -547,7 +595,7 @@ export class ListMindmapView {
this.model.nodes.get(this.foldAnchor.id)?.collapsed) === this.foldAnchor.collapsed) {
this.foldAnchor = undefined;
}
- if (this.printTransform) {
+ if (this.printTransform || this.options.printLayout) {
this.fitPrint();
} else if (this.initialFit) {
this.initialFit = false;
@@ -636,15 +684,17 @@ export class ListMindmapView {
const height = label.offsetHeight;
const segments = points.slice(1).map((p, i) => ({a: points[i], b: p,
length: Math.hypot(p.x - points[i].x, p.y - points[i].y)})).sort((a, b) => b.length - a.length);
+ const nodes = [...this.positions.values()];
+ const available = (p: MindmapRoutePoint) => !nodes.some(node =>
+ p.x + width / 2 > node.x - 4 && p.x - width / 2 < node.x + node.width + 4 &&
+ p.y + height / 2 > node.y - 4 && p.y - height / 2 < node.y + node.height + 4);
let labelPoint: MindmapRoutePoint;
for (const segment of segments) {
- const center = {x: (segment.a.x + segment.b.x) / 2, y: (segment.a.y + segment.b.y) / 2};
- const candidates = [center, {x: center.x + width / 2 + 6, y: center.y},
- {x: center.x - width / 2 - 6, y: center.y},
- {x: center.x, y: center.y - height / 2 - 6}, {x: center.x, y: center.y + height / 2 + 6}];
- labelPoint = candidates.find(p => ![...this.positions.values()].some(node =>
- p.x + width / 2 > node.x - 4 && p.x - width / 2 < node.x + node.width + 40 &&
- p.y + height / 2 > node.y - 4 && p.y - height / 2 < node.y + node.height + 4));
+ const candidates = [0.5, 0.25, 0.75].map(ratio => ({
+ x: segment.a.x + (segment.b.x - segment.a.x) * ratio,
+ y: segment.a.y + (segment.b.y - segment.a.y) * ratio,
+ }));
+ labelPoint = candidates.find(available);
if (labelPoint) {
break;
}