Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(mind-map): keep branch lines from showing through tinted nodes
Elian Doranb800b40bffef
diff --git a/apps/client/src/widgets/type_widgets/MindMap.css b/apps/client/src/widgets/type_widgets/MindMap.cssindex 80664e7f08..4a68d8d945 100644--- a/apps/client/src/widgets/type_widgets/MindMap.css+++ b/apps/client/src/widgets/type_widgets/MindMap.css@@ -18,6 +18,28 @@pointer-events: auto;}+/*+ * The node backgrounds the panel offers are tints (see MindMapNodePanel), which take on whatever+ * the canvas is. The branch lines are drawn underneath the nodes, so a tint on its own composites+ * against them and lets them show through the node — give a tinted node an opaque copy of the+ * canvas to sit on instead. Only nodes carrying a background of their own get one, so that the+ * lines keep running under plain nodes as they did.+ */+.mind-map-container me-tpc[style*="background"] {+ position: relative;+}++.mind-map-container me-tpc[style*="background"]::before {+ content: "";+ position: absolute;+ inset: 0;+ /* Below the node's own background, but still above the lines, which are outside the stacking+ context that the node's `me-parent` / `me-root` establishes. */+ z-index: -1;+ border-radius: inherit;+ background-color: var(--bgcolor);+}+.mind-map-container:fullscreen {.rb {bottom: calc(20px + env(safe-area-inset-bottom));diff --git a/apps/client/src/widgets/type_widgets/helpers/mind_map_export.spec.ts b/apps/client/src/widgets/type_widgets/helpers/mind_map_export.spec.tsindex 18d9dbb37b..7001b637a1 100644--- a/apps/client/src/widgets/type_widgets/helpers/mind_map_export.spec.ts+++ b/apps/client/src/widgets/type_widgets/helpers/mind_map_export.spec.ts@@ -37,7 +37,7 @@ function buildExportedSvg() {`</svg>`;}-function buildMind({ labels = [] as string[], exportedSvg = buildExportedSvg() } = {}) {+function buildMind({ labels = [] as string[], exportedSvg = buildExportedSvg(), canvasColor = "#252526" } = {}) {const nodes = document.createElement("me-nodes");for (const labelHtml of labels) {const label = document.createElement("div");@@ -49,6 +49,7 @@ function buildMind({ labels = [] as string[], exportedSvg = buildExportedSvg() }return {nodes,+ theme: { cssVar: { "--bgcolor": canvasColor } },exportSvg: () => new Blob([ exportedSvg ], { type: "image/svg+xml" })} as unknown as MindElixirInstance;}@@ -97,6 +98,31 @@ describe("postProcessExportedSvg", () => {expect(doc.querySelector("rect")?.getAttribute("width")).toBe("400");});+ it("backs only translucent node boxes with the canvas color, keeping their geometry", () => {+ // The exporter draws the lines first and the node boxes over them, so a tinted box would+ // otherwise show the branch lines through the node.+ const exportedSvg = `<svg xmlns="http://www.w3.org/2000/svg" width="400px" height="300px">` ++ `<rect x="0" y="0" width="400" height="300" fill="#fff"/>` ++ `<svg x="100" y="100" overflow="visible"><g class="lines"></g>` ++ `<rect x="1" y="2" width="30" height="10" rx="3px" fill="rgba(229, 230, 77, 0.251)"/>` ++ `<rect x="5" y="6" width="30" height="10" fill="rgb(45, 55, 72)"/>` ++ `<rect x="7" y="8" width="30" height="10" fill="rgba(0, 0, 0, 0)"/>` ++ `</svg></svg>`;+ const result = postProcessExportedSvg(buildMind({ canvasColor: "#252526" }), exportedSvg);++ const doc = new DOMParser().parseFromString(result, "image/svg+xml");+ const innerSvg = doc.documentElement.querySelector(":scope > svg");+ const boxes = Array.from(innerSvg?.querySelectorAll(":scope > rect") ?? []);+ expect(boxes.map((box) => box.getAttribute("fill"))).toEqual([+ // The backing sits right below the tinted box; the opaque and the fully transparent+ // boxes are left alone.+ "#252526", "rgba(229, 230, 77, 0.251)", "rgb(45, 55, 72)", "rgba(0, 0, 0, 0)"+ ]);+ for (const attribute of [ "x", "y", "width", "height", "rx" ]) {+ expect(boxes[0].getAttribute(attribute)).toBe(boxes[1].getAttribute(attribute));+ }+ });+it("adds no labels when the map has none, and returns unparseable input unchanged", () => {const noLabels = postProcessExportedSvg(buildMind(), buildExportedSvg());const doc = new DOMParser().parseFromString(noLabels, "image/svg+xml");diff --git a/apps/client/src/widgets/type_widgets/helpers/mind_map_export.ts b/apps/client/src/widgets/type_widgets/helpers/mind_map_export.tsindex 3700e5a9eb..7366737cf4 100644--- a/apps/client/src/widgets/type_widgets/helpers/mind_map_export.ts+++ b/apps/client/src/widgets/type_widgets/helpers/mind_map_export.ts@@ -51,6 +51,7 @@ const SIZE_SLACK_PX = 2;* replicating the label's box and text style.* - Adds slack to every `<foreignObject>`'s exact-fit size so text is not clipped when* the SVG is rasterized with slightly different font metrics (see the slack constants).+ * - Backs translucent node boxes with the canvas color (see {@link backTranslucentNodeBoxes}).** @param mind the live mind map instance the SVG was exported from.* @param svgText the output of `mind.exportSvg().text()`.@@ -65,6 +66,11 @@ export function postProcessExportedSvg(mind: MindElixirInstance, svgText: stringreturn svgText;}+ const canvasColor = mind.theme?.cssVar?.["--bgcolor"];+ if (canvasColor) {+ backTranslucentNodeBoxes(contentSvg, canvasColor);+ }+const labels = mind.nodes.querySelectorAll<HTMLElement>(".svg-label");for (const label of Array.from(labels)) {contentSvg.appendChild(doc.importNode(buildLabelForeignObject(label), true));@@ -78,6 +84,41 @@ export function postProcessExportedSvg(mind: MindElixirInstance, svgText: stringreturn new XMLSerializer().serializeToString(doc);}+/**+ * Paints an opaque copy of the map canvas behind every translucent node box.+ *+ * The node backgrounds the editor offers are tints (see MindMapNodePanel) meant to composite+ * against the canvas, but the exporter draws the branch lines first and the node boxes over them+ * (each box being a direct `<rect>` child of the map layers, filled with the node's computed+ * background), so a tint alone would show the lines through the node. Nodes with no background of+ * their own are fully transparent and are left as they are, so the lines keep running under them.+ *+ * @param contentSvg the exporter's inner svg, holding the map layers.+ * @param canvasColor the map background, as the exporter fills its own backdrop with.+ */+function backTranslucentNodeBoxes(contentSvg: Element, canvasColor: string) {+ for (const box of Array.from(contentSvg.querySelectorAll(":scope > rect"))) {+ const alpha = getFillAlpha(box.getAttribute("fill"));+ if (alpha === null || alpha <= 0 || alpha >= 1) {+ continue;+ }++ const backing = box.cloneNode(false) as Element;+ backing.setAttribute("fill", canvasColor);+ contentSvg.insertBefore(backing, box);+ }+}++/**+ * The alpha of a fill the exporter took from a computed style, or `null` when it carries none.+ * Computed sRGB colors are always serialized as `rgb()`/`rgba()`, so a translucent one arrives in+ * the `rgba()` form whichever notation the node's background was written in.+ */+function getFillAlpha(fill: string | null) {+ const alpha = /^rgba\(\s*[\d.]+\s*,\s*[\d.]+\s*,\s*[\d.]+\s*,\s*([\d.]+)\s*\)$/.exec(fill ?? "")?.[1];+ return (alpha !== undefined ? Number.parseFloat(alpha) : null);+}+function addSizeSlack(element: Element, attribute: "width" | "height") {const value = Number.parseFloat(element.getAttribute(attribute) ?? "");if (!Number.isFinite(value) || value <= 0) {