Skip to content

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

:sparkles: Support CSS variables for list mindmap relation styles https://github.com/siyuan-note/siyuan/issues/19747

Daniel41558d57709e
diff --git a/app/appearance/themes/daylight/theme.css b/app/appearance/themes/daylight/theme.css
index e5c5963ab2..fb141c0b67 100644
--- a/app/appearance/themes/daylight/theme.css
+++ b/app/appearance/themes/daylight/theme.css
@@ -69,6 +69,14 @@
--b3-list-mindmap-line-selected-color: initial;
--b3-list-mindmap-line-selected-width: auto;
+ /* 列表脑图关系线:状态规则与父子连线一致,新建预览使用普通状态的颜色和线宽 */
+ --b3-list-mindmap-relation-color: var(--b3-theme-primary);
+ --b3-list-mindmap-relation-width: 1.5;
+ --b3-list-mindmap-relation-hover-color: initial;
+ --b3-list-mindmap-relation-hover-width: auto;
+ --b3-list-mindmap-relation-selected-color: initial;
+ --b3-list-mindmap-relation-selected-width: auto;
+
/* 菜单 */
--b3-menu-background: var(--b3-theme-background);
--b3-menu-icon-color: rgba(0, 0, 0, .6);
diff --git a/app/appearance/themes/midnight/theme.css b/app/appearance/themes/midnight/theme.css
index 7d40914a3c..3a43d4f3f2 100644
--- a/app/appearance/themes/midnight/theme.css
+++ b/app/appearance/themes/midnight/theme.css
@@ -68,6 +68,14 @@
--b3-list-mindmap-line-selected-color: initial;
--b3-list-mindmap-line-selected-width: auto;
+ /* 列表脑图关系线:状态规则与父子连线一致,新建预览使用普通状态的颜色和线宽 */
+ --b3-list-mindmap-relation-color: var(--b3-theme-primary);
+ --b3-list-mindmap-relation-width: 1.5;
+ --b3-list-mindmap-relation-hover-color: initial;
+ --b3-list-mindmap-relation-hover-width: auto;
+ --b3-list-mindmap-relation-selected-color: initial;
+ --b3-list-mindmap-relation-selected-width: auto;
+
/* 菜单 */
--b3-menu-background: var(--b3-theme-background);
--b3-menu-icon-color: rgba(255, 255, 255, .7);
diff --git a/app/src/protyle/render/listMindmap/model.test.ts b/app/src/protyle/render/listMindmap/model.test.ts
index 1952033893..fe4fd949d9 100644
--- a/app/src/protyle/render/listMindmap/model.test.ts
+++ b/app/src/protyle/render/listMindmap/model.test.ts
@@ -1263,6 +1263,78 @@ const browserCases = async (sourceCode: string, css: string, taskSource: string,
check.deepEqual(relationAdditions, [[beta, alpha]]);
model.metadata.relations.push({id: "relation-test", from: alpha, to: beta, label: "Relation"});
view.update(model);
+ await settle();
+ view.clearSelection();
+ view.setHoveredLine();
+ const relationScale = view.scale;
+ const primaryColor = host.style.getPropertyValue("--b3-theme-primary");
+ host.style.setProperty("--b3-theme-primary", "#123abc");
+ const setRelationTheme = (property: string, value: string) =>
+ host.style.setProperty(`--b3-list-mindmap-relation-${property}`, value);
+ const checkRelation = (color: string, width: number) => {
+ checkLine(color, width);
+ check.equal(lineContext.fillStyle, color, "relation arrows share the connector color");
+ };
+ view.scale = 0.5;
+ setLineTheme("color", "#abcdef");
+ setLineTheme("width", "9");
+ checkRelation("#123abc", 1.5);
+ view.setHoveredLine("relation:relation-test");
+ checkRelation("#123abc", 4.5);
+ view.selectedRelation = "relation-test";
+ checkRelation("#123abc", 5.5);
+ view.setHoveredLine();
+ checkRelation("#123abc", 2.5);
+ view.clearSelection();
+ setRelationTheme("color", "var(--b3-font-background1)");
+ setRelationTheme("width", "2px");
+ checkRelation("#123456", 2);
+ setRelationTheme("hover-color", "#445566");
+ setRelationTheme("hover-width", "4");
+ setRelationTheme("selected-color", "#778899");
+ setRelationTheme("selected-width", "6px");
+ view.setHoveredLine("relation:relation-test");
+ checkRelation("#445566", 4);
+ view.selectedRelation = "relation-test";
+ checkRelation("#778899", 6);
+ view.setHoveredLine();
+ checkRelation("#778899", 6);
+ view.clearSelection();
+ const themedRelation = model.metadata.relations[0];
+ themedRelation.color = "#aabbcc";
+ themedRelation.width = 3;
+ checkRelation("#aabbcc", 3);
+ view.setHoveredLine("relation:relation-test");
+ checkRelation("#445566", 4);
+ setRelationTheme("hover-color", "invalid-color");
+ for (const width of ["auto", "0", "-2", "Infinity", "2em", "3px-invalid"]) {
+ setRelationTheme("hover-width", width);
+ checkRelation("#aabbcc", 6);
+ }
+ view.selectedRelation = "relation-test";
+ setRelationTheme("selected-color", "initial");
+ setRelationTheme("selected-width", "auto");
+ checkRelation("#aabbcc", 7);
+ view.clearSelection();
+ view.setHoveredLine();
+ view.relationFrom = alpha;
+ view.relationPreview = {targetId: beta};
+ checkRelation("#123456", 2);
+ setRelationTheme("color", "invalid-color");
+ setRelationTheme("width", "-1");
+ checkRelation("#123abc", 1.5);
+ view.relationFrom = undefined;
+ view.relationPreview = undefined;
+ delete themedRelation.color;
+ delete themedRelation.width;
+ checkRelation("#123abc", 1.5);
+ view.scale = relationScale;
+ themedProperties.forEach(property => {
+ host.style.removeProperty(`--b3-list-mindmap-line-${property}`);
+ host.style.removeProperty(`--b3-list-mindmap-relation-${property}`);
+ });
+ host.style.setProperty("--b3-theme-primary", primaryColor);
+ view.draw();
host.querySelector<HTMLButtonElement>(".list-mindmap__relation").click();
const relationElement = host.querySelector<HTMLElement>(".list-mindmap__relation");
relationElement.dispatchEvent(new PointerEvent("pointerover", {bubbles: true}));
diff --git a/app/src/protyle/render/listMindmap/view.ts b/app/src/protyle/render/listMindmap/view.ts
index 478179785e..65a1096984 100644
--- a/app/src/protyle/render/listMindmap/view.ts
+++ b/app/src/protyle/render/listMindmap/view.ts
@@ -1102,13 +1102,16 @@ 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 lineStyles = ["", "hover-", "selected-"].map(state => {
- const width = Number(theme.getPropertyValue(`--b3-list-mindmap-line-${state}width`).trim().replace(/px$/, ""));
+ const readLineStyles = (type: "line" | "relation") => ["", "hover-", "selected-"].map(state => {
+ const prefix = `--b3-list-mindmap-${type}-${state}`;
+ const width = Number(theme.getPropertyValue(`${prefix}width`).trim().replace(/px$/, ""));
return {
- color: theme.getPropertyValue(`--b3-list-mindmap-line-${state}color`).trim(),
+ color: theme.getPropertyValue(`${prefix}color`).trim(),
width: Number.isFinite(width) && width > 0 ? width : undefined,
};
});
+ const lineStyles = readLineStyles("line");
+ const relationStyles = readLineStyles("relation");
const colors = new Map<string, string>();
const resolveColor = (value: string, fallback: string) => {
const key = `${fallback}\n${value || ""}`;
@@ -1166,18 +1169,22 @@ export class ListMindmapView {
const {path, end, previous, arrowSizeLimit, labelPoint} = route;
this.linePaths.push({id: relation.id, relation: true, path, end});
context.beginPath();
- context.strokeStyle = resolveColor(relation.color, primary);
- const emphasis = (relation.id === this.selectedRelation ? 1 : 0) +
- (this.hoveredLine === `relation:${relation.id}` ? 1.5 / this.scale : 0);
- context.lineWidth = (relation.width || 1.5) + emphasis;
- element.classList.toggle("list-mindmap__relation--hover", this.hoveredLine === `relation:${relation.id}`);
+ 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 emphasis = Math.max(0, context.lineWidth - baseWidth);
+ element.classList.toggle("list-mindmap__relation--hover", hovered);
context.setLineDash(relation.dash === false ? [] : [5, 4]);
context.stroke(path);
context.setLineDash([]);
context.beginPath();
const direction = Math.atan2(end.y - previous.y, end.x - previous.x);
// 短线上的双向箭头预留间隙,避免合并成菱形。
- const arrowSize = Math.min(Math.max(7 / this.scale, (relation.width || 1.5) * 2), arrowSizeLimit);
+ const arrowSize = Math.min(Math.max(7 / this.scale, baseWidth * 2), arrowSizeLimit);
// 箭头随线条状态加宽,保持长度不变以保留双向箭头之间的间隙。
const halfWidth = arrowSize / 2 + emphasis / 2;
const baseX = end.x - arrowSize * Math.cos(Math.PI / 6) * Math.cos(direction);
@@ -1193,11 +1200,11 @@ export class ListMindmapView {
element.style.top = `${labelPoint.y}px`;
}
});
- this.drawRelationPreview(context, primary);
+ this.drawRelationPreview(context, resolveColor(relationStyles[0].color, primary), relationStyles[0].width || 1.5);
this.renderRouteControls();
}
- private drawRelationPreview(context: CanvasRenderingContext2D, color: string) {
+ private drawRelationPreview(context: CanvasRenderingContext2D, color: string, width: number) {
const from = this.positions.get(this.relationFrom);
if (!from || !this.relationPreview) {
return;
@@ -1212,12 +1219,12 @@ export class ListMindmapView {
const {path, end, previous, arrowSizeLimit} = route;
context.beginPath();
context.strokeStyle = color;
- context.lineWidth = 1.5;
+ context.lineWidth = width;
context.setLineDash([5, 4]);
context.stroke(path);
context.setLineDash([]);
context.beginPath();
- const size = Math.min(Math.max(7 / this.scale, 3), arrowSizeLimit);
+ const size = Math.min(Math.max(7 / this.scale, width * 2), arrowSizeLimit);
const direction = Math.atan2(end.y - previous.y, end.x - previous.x);
context.fillStyle = color;
context.moveTo(end.x - size * Math.cos(direction - Math.PI / 6), end.y - size * Math.sin(direction - Math.PI / 6));