Skip to content

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

:art: Unify mindmap line states and align summary endpoints https://github.com/siyuan-note/siyuan/issues/19747

Daniele6db1d2ee46a
diff --git a/app/appearance/themes/daylight/theme.css b/app/appearance/themes/daylight/theme.css
index 9f87306ffc..7e2d383e5a 100644
--- a/app/appearance/themes/daylight/theme.css
+++ b/app/appearance/themes/daylight/theme.css
@@ -77,10 +77,12 @@
--b3-mindmap-relation-selected-color: initial;
--b3-mindmap-relation-selected-width: auto;
- /* 思维导图概要线:线宽使用正数或 px,选中线宽为 auto 时在普通线宽上增加 1,打印使用普通样式 */
+ /* 思维导图概要线:显式颜色始终优先,状态规则与父子连线一致,打印使用普通样式 */
--b3-mindmap-summary-color: var(--b3-border-color);
--b3-mindmap-summary-width: 1.5;
- --b3-mindmap-summary-selected-color: var(--b3-theme-primary);
+ --b3-mindmap-summary-hover-color: initial;
+ --b3-mindmap-summary-hover-width: auto;
+ --b3-mindmap-summary-selected-color: initial;
--b3-mindmap-summary-selected-width: auto;
/* 菜单 */
diff --git a/app/appearance/themes/midnight/theme.css b/app/appearance/themes/midnight/theme.css
index 4448b13ea5..a8a3e97bb5 100644
--- a/app/appearance/themes/midnight/theme.css
+++ b/app/appearance/themes/midnight/theme.css
@@ -76,10 +76,12 @@
--b3-mindmap-relation-selected-color: initial;
--b3-mindmap-relation-selected-width: auto;
- /* 思维导图概要线:线宽使用正数或 px,选中线宽为 auto 时在普通线宽上增加 1,打印使用普通样式 */
+ /* 思维导图概要线:显式颜色始终优先,状态规则与父子连线一致,打印使用普通样式 */
--b3-mindmap-summary-color: var(--b3-border-color);
--b3-mindmap-summary-width: 1.5;
- --b3-mindmap-summary-selected-color: var(--b3-theme-primary);
+ --b3-mindmap-summary-hover-color: initial;
+ --b3-mindmap-summary-hover-width: auto;
+ --b3-mindmap-summary-selected-color: initial;
--b3-mindmap-summary-selected-width: auto;
/* 菜单 */
diff --git a/app/src/protyle/render/listMindmap/model.test.ts b/app/src/protyle/render/listMindmap/model.test.ts
index c7c99cfdca..c6e82294e8 100644
--- a/app/src/protyle/render/listMindmap/model.test.ts
+++ b/app/src/protyle/render/listMindmap/model.test.ts
@@ -1331,7 +1331,10 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,
model.metadata.nodes[themedEdge] = {lineColor: "#112233", lineWidth: 5};
checkLine("#112233", 5);
view.setHoveredLine(`edge:${themedEdge}`);
- checkLine("#445566", 4);
+ checkLine("#112233", 4);
+ view.selectedEdge = themedEdge;
+ checkLine("#112233", 6);
+ view.selectedEdge = undefined;
setLineTheme("hover-color", "invalid-color");
for (const width of ["auto", "0", "-2", "Infinity", "2em", "3px-invalid"]) {
setLineTheme("hover-width", width);
@@ -1451,7 +1454,10 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,
themedRelation.width = 3;
checkRelation("#aabbcc", 3);
view.setHoveredLine("relation:relation-test");
- checkRelation("#445566", 4);
+ checkRelation("#aabbcc", 4);
+ view.selectedRelation = "relation-test";
+ checkRelation("#aabbcc", 6);
+ view.selectedRelation = undefined;
setRelationTheme("hover-color", "invalid-color");
for (const width of ["auto", "0", "-2", "Infinity", "2em", "3px-invalid"]) {
setRelationTheme("hover-width", width);
@@ -2974,10 +2980,15 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,
const bracketCenter = summaryViewport.getBoundingClientRect().top + summaryView.offsetY +
(summaryPosition.top + summaryPosition.bottom) / 2 * summaryView.scale;
check.ok(Math.abs((summaryBounds.top + summaryBounds.bottom) / 2 - bracketCenter) < 1);
- const visibleBounds = Array.from(summaryHost.querySelectorAll<HTMLElement>("[data-mindmap-id]"))
- .filter(element => element.dataset.mindmapId !== summaryView.model.root.id).map(element => element.getBoundingClientRect());
- check.ok(Math.abs((Math.min(...visibleBounds.map(rect => rect.top)) + Math.max(...visibleBounds.map(rect => rect.bottom))) / 2 -
- bracketCenter) < 1, "summary brackets center on the displayed member nodes");
+ const memberCenters = [summaryIds[0], summaryIds[2]].map(id => {
+ const rect = summaryHost.querySelector(`[data-mindmap-id="${id}"]`).getBoundingClientRect();
+ return (rect.top + rect.bottom) / 2;
+ });
+ const summaryOriginY = summaryViewport.getBoundingClientRect().top + summaryView.offsetY;
+ check.ok(Math.abs(summaryOriginY + summaryPosition.top * summaryView.scale - memberCenters[0]) < 1);
+ check.ok(Math.abs(summaryOriginY + summaryPosition.bottom * summaryView.scale - memberCenters[1]) < 1);
+ check.ok(Math.abs((memberCenters[0] + memberCenters[1]) / 2 - bracketCenter) < 1,
+ "summary brackets connect the displayed first and last member centers");
summaryView.clearSelection();
summaryLabel.focus();
check.equal(document.activeElement, summaryLabel);
@@ -3015,7 +3026,7 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,
summaryView.selectedSummary = undefined;
checkSummaryStyle("#123456", 1.5);
summaryView.selectedSummary = "summary";
- checkSummaryStyle("#abcdef", 2.5);
+ checkSummaryStyle("#123456", 2.5);
setSummaryTheme("color", "var(--b3-theme-primary)");
setSummaryTheme("width", "3px");
summaryView.selectedSummary = undefined;
@@ -3023,10 +3034,10 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,
summaryView.model.metadata.summaries[0].color = "#445566";
checkSummaryStyle("#445566", 3);
summaryView.selectedSummary = "summary";
- checkSummaryStyle("#abcdef", 4);
+ checkSummaryStyle("#445566", 4);
setSummaryTheme("selected-color", "#778899");
setSummaryTheme("selected-width", "6");
- checkSummaryStyle("#778899", 6);
+ checkSummaryStyle("#445566", 6);
summaryView.options.printLayout = true;
checkSummaryStyle("#445566", 3);
summaryView.options.printLayout = false;
@@ -3036,14 +3047,51 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,
setSummaryTheme("selected-color", "invalid-color");
for (const width of ["auto", "0", "-2", "Infinity", "2em", "3px-invalid"]) {
setSummaryTheme("selected-width", width);
- checkSummaryStyle("#abcdef", 4);
+ checkSummaryStyle("#445566", 4);
}
delete summaryView.model.metadata.summaries[0].color;
summaryView.selectedSummary = undefined;
setSummaryTheme("color", "invalid-color");
setSummaryTheme("width", "-1");
checkSummaryStyle("#123456", 1.5);
- ["color", "width", "selected-color", "selected-width"].forEach(property =>
+ setSummaryTheme("color", "#112233");
+ setSummaryTheme("width", "2");
+ setSummaryTheme("hover-color", "#556677");
+ setSummaryTheme("hover-width", "4px");
+ setSummaryTheme("selected-color", "#778899");
+ setSummaryTheme("selected-width", "6");
+ summaryView.setHoveredLine("summary:summary");
+ checkSummaryStyle("#556677", 4);
+ summaryView.selectedSummary = "summary";
+ checkSummaryStyle("#778899", 6);
+ for (const value of ["initial", "unset", "invalid-color"]) {
+ setSummaryTheme("selected-color", value);
+ checkSummaryStyle("#112233", 6);
+ }
+ summaryView.model.metadata.summaries[0].color = "#445566";
+ setSummaryTheme("selected-color", "#778899");
+ checkSummaryStyle("#445566", 6);
+ summaryView.selectedSummary = undefined;
+ checkSummaryStyle("#445566", 4);
+ summaryView.options.printLayout = true;
+ checkSummaryStyle("#445566", 2);
+ summaryView.options.printLayout = false;
+ summaryView.printTransform = {scale: 1, offsetX: 0, offsetY: 0};
+ checkSummaryStyle("#445566", 2);
+ summaryView.printTransform = undefined;
+ delete summaryView.model.metadata.summaries[0].color;
+ setSummaryTheme("hover-color", "initial");
+ setSummaryTheme("hover-width", "auto");
+ const summaryScale = summaryView.scale;
+ summaryView.scale = 0.5;
+ checkSummaryStyle("#112233", 5);
+ summaryView.scale = summaryScale;
+ summaryView.setHoveredLine();
+ sendPointer(summaryLabel, "pointermove", summaryBounds.left + 1, summaryBounds.top + 1);
+ check.equal(summaryView.hoveredLine, "summary:summary");
+ summaryViewport.dispatchEvent(new PointerEvent("pointerleave"));
+ check.equal(summaryView.hoveredLine, undefined);
+ ["color", "width", "hover-color", "hover-width", "selected-color", "selected-width"].forEach(property =>
summaryHost.style.removeProperty(`--b3-mindmap-summary-${property}`));
summaryHost.style.removeProperty("--b3-border-color");
summaryHost.style.removeProperty("--b3-theme-primary");
diff --git a/app/src/protyle/render/listMindmap/summary.test.ts b/app/src/protyle/render/listMindmap/summary.test.ts
index 7b289e5244..297652b1ec 100644
--- a/app/src/protyle/render/listMindmap/summary.test.ts
+++ b/app/src/protyle/render/listMindmap/summary.test.ts
@@ -46,7 +46,7 @@ test("summary creation accepts sibling ranges and rejects cross-level, overlappi
assert.deepEqual(getListMindmapSummaryRange(model, "c", "c"), ["c"]);
});
-test("summary brackets cover visible descendants and disappear with their members", () => {
+test("summary brackets align member centers, avoid descendants and disappear with their members", () => {
const model = makeModel();
model.metadata.summaries = [summary(["a", "b"])];
const positions = new Map([
@@ -57,9 +57,20 @@ test("summary brackets cover visible descendants and disappear with their member
const sizes = new Map([["s", {width: 150, height: 60}]]);
const expanded = layoutListMindmapSummaries(model, positions, sizes).get("s");
assert.equal(expanded.x, 476);
- assert.equal(expanded.top, 9);
- assert.equal(expanded.bottom, 215);
+ assert.equal(expanded.top, 57);
+ assert.equal(expanded.bottom, 182);
assert.equal(expanded.labelY + expanded.height / 2, (expanded.top + expanded.bottom) / 2);
+ sizes.set("s", {width: 150, height: 400});
+ const tall = layoutListMindmapSummaries(model, positions, sizes).get("s");
+ assert.equal(tall.top, expanded.top);
+ assert.equal(tall.bottom, expanded.bottom);
+ assert.equal(tall.labelY + tall.height / 2, (expanded.top + expanded.bottom) / 2);
+ model.metadata.summaries = [summary(["a"])];
+ const single = layoutListMindmapSummaries(model, positions, sizes).get("s");
+ assert.equal(single.top, 57);
+ assert.equal(single.bottom, 57);
+ assert.equal(single.x, 476);
+ model.metadata.summaries = [summary(["a", "b"])];
positions.delete("nested");
assert.equal(layoutListMindmapSummaries(model, positions, sizes).get("s").x, 256);
positions.delete("a");
diff --git a/app/src/protyle/render/listMindmap/summary.ts b/app/src/protyle/render/listMindmap/summary.ts
index 25254e800c..ee8585377b 100644
--- a/app/src/protyle/render/listMindmap/summary.ts
+++ b/app/src/protyle/render/listMindmap/summary.ts
@@ -89,7 +89,7 @@ export const getListMindmapSummaryRange = (model: ListMindmapModel, from: string
return nodeIds;
};
-// 括号包围成员的可见子树,文字尺寸参与边界计算,折叠隐藏的成员不产生独立概要。
+// 括号连接首尾成员的垂直中心,横向避让可见子树,标签高度独立参与画布边界计算。
export const layoutListMindmapSummaries = (model: ListMindmapModel, positions: Map<string, ListMindmapPosition>,
sizes: Map<string, {width: number, height: number}>) => {
const result = new Map<string, ListMindmapSummaryPosition>();
@@ -105,8 +105,6 @@ export const layoutListMindmapSummaries = (model: ListMindmapModel, positions: M
return;
}
let right = 0;
- let top = Infinity;
- let bottom = -Infinity;
const pending = [...summary.nodeIds];
while (pending.length) {
const id = pending.pop();
@@ -115,16 +113,16 @@ export const layoutListMindmapSummaries = (model: ListMindmapModel, positions: M
continue;
}
right = Math.max(right, position.x + position.width);
- // 节点向上避让下划线,括号和标签按实际显示边界居中。
- const nodeTop = position.y - (id === model.root.id ? 1 : 3);
- top = Math.min(top, nodeTop);
- bottom = Math.max(bottom, nodeTop + position.height);
pending.push(...(model.nodes.get(id)?.children.map(node => node.id) || []));
}
+ const nodeCenter = (id: string) => {
+ const position = positions.get(id);
+ return position.y - (id === model.root.id ? 1 : 3) + position.height / 2;
+ };
+ const top = nodeCenter(summary.nodeIds[0]);
+ const bottom = nodeCenter(summary.nodeIds[summary.nodeIds.length - 1]);
const center = (top + bottom) / 2;
- result.set(summary.id, {id: summary.id, x: right + 36,
- top: Math.min(top - 8, center - size.height / 2),
- bottom: Math.max(bottom + 8, center + size.height / 2),
+ result.set(summary.id, {id: summary.id, x: right + 36, top, bottom,
labelX: right + 60, labelY: center - size.height / 2, ...size});
});
return result;
diff --git a/app/src/protyle/render/listMindmap/view.ts b/app/src/protyle/render/listMindmap/view.ts
index 5e68d28fe4..1e6fd7da51 100644
--- a/app/src/protyle/render/listMindmap/view.ts
+++ b/app/src/protyle/render/listMindmap/view.ts
@@ -842,9 +842,9 @@ export class ListMindmapView {
let top = 0;
let left = 0;
this.summaryPositions.forEach(position => {
- top = Math.min(top, position.top - 32);
+ top = Math.min(top, position.top - 32, position.labelY - 32);
this.bounds.width = Math.max(this.bounds.width, position.labelX + position.width + 32);
- this.bounds.height = Math.max(this.bounds.height, position.bottom + 32);
+ this.bounds.height = Math.max(this.bounds.height, position.bottom + 32, position.labelY + position.height + 32);
});
// 每条连接独立避让节点和按钮,已有关系线不影响路径选择。
this.model.metadata.relations.forEach((relation) => {
@@ -1033,7 +1033,7 @@ export class ListMindmapView {
}
private drawSummaries(context: CanvasRenderingContext2D,
- resolveStyle: (color: string, selected: boolean) => {color: string, width: number}) {
+ resolveStyle: (color: string, selected: boolean, hovered: boolean) => {color: string, width: number}) {
(this.model.metadata.summaries || []).forEach(summary => {
const position = this.summaryPositions.get(summary.id);
const element = this.summaryElements.get(summary.id);
@@ -1048,15 +1048,18 @@ export class ListMindmapView {
const radius = Math.min(6, (bottom - top) / 2);
const path = new Path2D();
path.moveTo(x - 10, 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);
+ 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 selected = this.selectedSummary === summary.id && !this.printTransform && !this.options.printLayout;
- const style = resolveStyle(summary.color, selected);
+ const hovered = this.hoveredLine === `summary:${summary.id}` && !this.printTransform && !this.options.printLayout;
+ const style = resolveStyle(summary.color, selected, hovered);
context.strokeStyle = style.color;
context.lineWidth = style.width;
context.setLineDash([]);
@@ -1113,8 +1116,9 @@ export class ListMindmapView {
return [...this.positions.values()].map(node => ({...node,
controlY: node.y + (node.id === this.model.root.id ? node.height / 2 : node.height),
})).concat([...this.summaryPositions.values()].map(summary => ({
- id: `summary:${summary.id}`, x: summary.x - 10, y: summary.top,
- width: summary.labelX + summary.width - summary.x + 10, height: summary.bottom - summary.top,
+ id: `summary:${summary.id}`, x: summary.x - 10, y: Math.min(summary.top, summary.labelY),
+ width: summary.labelX + summary.width - summary.x + 10,
+ height: Math.max(summary.bottom, summary.labelY + summary.height) - Math.min(summary.top, summary.labelY),
controlY: summary.top,
})));
}
@@ -1398,7 +1402,7 @@ export class ListMindmapView {
const theme = getComputedStyle(this.options.host);
const defaultLine = theme.getPropertyValue("--b3-border-color").trim() || "#a8adb5";
const primary = theme.getPropertyValue("--b3-theme-primary").trim() || "#3574f0";
- const readLineStyles = (type: "line" | "relation" | "summary", states = ["", "hover-", "selected-"]) => states.map(state => {
+ const readLineStyles = (type: "line" | "relation" | "summary") => ["", "hover-", "selected-"].map(state => {
const prefix = `--b3-mindmap-${type}-${state}`;
const width = Number(theme.getPropertyValue(`${prefix}width`).trim().replace(/px$/, ""));
return {
@@ -1408,7 +1412,7 @@ export class ListMindmapView {
});
const lineStyles = readLineStyles("line");
const relationStyles = readLineStyles("relation");
- const summaryStyles = readLineStyles("summary", ["", "selected-"]);
+ const summaryStyles = readLineStyles("summary");
const colors = new Map<string, string>();
const resolveColor = (value: string, fallback: string) => {
const key = `${fallback}\n${value || ""}`;
@@ -1421,6 +1425,18 @@ export class ListMindmapView {
}
return colors.get(key);
};
+ const resolveLineStyle = (styles: typeof lineStyles, color: string, width: number, fallback: string,
+ selected: boolean, hovered: boolean) => {
+ const baseColor = resolveColor(styles[0].color, fallback);
+ const baseWidth = width || styles[0].width || 1.5;
+ const stateStyle = selected ? styles[2] : hovered ? styles[1] : undefined;
+ return {
+ // 显式颜色始终优先,状态颜色缺省时沿用普通主题色。
+ color: resolveColor(color, resolveColor(stateStyle?.color, baseColor)),
+ width: stateStyle?.width ?? baseWidth + (selected ? 1 : 0) + (hovered ? 1.5 / this.scale : 0),
+ baseWidth,
+ };
+ };
this.edges.forEach((edge) => {
const from = this.positions.get(edge.from);
const to = this.positions.get(edge.to);
@@ -1441,12 +1457,9 @@ export class ListMindmapView {
context.beginPath();
const selected = this.selectedEdge === edge.to;
const hovered = this.hoveredLine === `edge:${edge.to}`;
- const baseColor = resolveColor(style.lineColor, resolveColor(lineStyles[0].color, defaultLine));
- const baseWidth = style.lineWidth || lineStyles[0].width || 1.5;
- // 选中状态优先于悬停状态,未配置状态样式时保留节点配色和随缩放调整的加粗效果。
- const stateStyle = selected ? lineStyles[2] : hovered ? lineStyles[1] : undefined;
- context.strokeStyle = resolveColor(stateStyle?.color, baseColor);
- context.lineWidth = stateStyle?.width ?? baseWidth + (selected ? 1 : 0) + (hovered ? 1.5 / this.scale : 0);
+ const lineStyle = resolveLineStyle(lineStyles, style.lineColor, style.lineWidth, defaultLine, selected, hovered);
+ context.strokeStyle = lineStyle.color;
+ context.lineWidth = lineStyle.width;
context.setLineDash(style.lineDash ? [6, 4] : []);
context.stroke(path);
});
@@ -1468,11 +1481,10 @@ export class ListMindmapView {
context.beginPath();
const selected = relation.id === this.selectedRelation;
const hovered = this.hoveredLine === `relation:${relation.id}`;
- const baseColor = resolveColor(relation.color, resolveColor(relationStyles[0].color, primary));
- const baseWidth = relation.width || relationStyles[0].width || 1.5;
- const stateStyle = selected ? relationStyles[2] : hovered ? relationStyles[1] : undefined;
- context.strokeStyle = resolveColor(stateStyle?.color, baseColor);
- context.lineWidth = stateStyle?.width ?? baseWidth + (selected ? 1 : 0) + (hovered ? 1.5 / this.scale : 0);
+ const lineStyle = resolveLineStyle(relationStyles, relation.color, relation.width, primary, selected, hovered);
+ const baseWidth = lineStyle.baseWidth;
+ context.strokeStyle = lineStyle.color;
+ context.lineWidth = lineStyle.width;
const emphasis = Math.max(0, context.lineWidth - baseWidth);
element.classList.toggle("mindmap-view__relation--hover", hovered);
context.setLineDash(relation.dash === false ? [] : [5, 4]);
@@ -1498,14 +1510,8 @@ export class ListMindmapView {
}
});
this.drawRelationPreview(context, resolveColor(relationStyles[0].color, primary), relationStyles[0].width || 1.5);
- this.drawSummaries(context, (color, selected) => {
- const baseWidth = summaryStyles[0].width || 1.5;
- return {
- color: selected ? resolveColor(summaryStyles[1].color, primary) :
- resolveColor(color, resolveColor(summaryStyles[0].color, defaultLine)),
- width: selected ? summaryStyles[1].width ?? baseWidth + 1 : baseWidth,
- };
- });
+ this.drawSummaries(context, (color, selected, hovered) =>
+ resolveLineStyle(summaryStyles, color, undefined, defaultLine, selected, hovered));
this.renderRouteControls();
}
@@ -1862,8 +1868,9 @@ export class ListMindmapView {
}
const target = event.target as Element;
const relation = target.closest<HTMLElement>(".mindmap-view__relation[data-relation-id]");
+ const summary = target.closest<HTMLElement>(".mindmap-view__summary[data-summary-id]");
const line = target.closest(".mindmap-view__node, input, button") ? undefined : this.findLine(event);
- this.setHoveredLine(relation ? `relation:${relation.dataset.relationId}` :
+ this.setHoveredLine(summary ? `summary:${summary.dataset.summaryId}` : relation ? `relation:${relation.dataset.relationId}` :
line ? `${line.summary ? "summary" : line.relation ? "relation" : "edge"}:${line.id}` : undefined);
return;
}
@@ -2356,6 +2363,7 @@ export class ListMindmapView {
this.relationRoutes.forEach(points => points.forEach(point => include(point.x, point.y)));
this.summaryPositions.forEach(summary => {
include(summary.x - 10, summary.top, summary.labelX + summary.width - summary.x + 10, summary.bottom - summary.top);
+ include(summary.labelX, summary.labelY, summary.width, summary.height);
});
this.relationElements.forEach(element => {
if (!element.hidden && element.style.visibility !== "hidden") {