Skip to content

Build your personal knowledge base with Trilium Notes

feat(mind map): draw the map on Trilium's own background

Elian Doranb3f2132e01fd
diff --git a/apps/client/src/widgets/type_widgets/mind_map/MindMap.spec.ts b/apps/client/src/widgets/type_widgets/mind_map/MindMap.spec.ts
index 9724c2b253..78e9288500 100644
--- a/apps/client/src/widgets/type_widgets/mind_map/MindMap.spec.ts
+++ b/apps/client/src/widgets/type_widgets/mind_map/MindMap.spec.ts
@@ -2,9 +2,10 @@
// DOMPurify relies on browser-faithful DOM traversal (NodeIterator); happy-dom
// mishandles it and strips valid markup (surfaced by dompurify 3.4.8). Run the
// sanitization-dependent specs under jsdom, which matches real-browser behavior.
+import { DARK_THEME, THEME as LIGHT_THEME } from "mind-elixir";
import { describe, expect, it } from "vitest";
-import { sanitizeMindMapData } from "./MindMap.js";
+import { buildTheme, sanitizeMindMapData } from "./MindMap.js";
describe("sanitizeMindMapData", () => {
it("strips XSS/RCE vectors from dangerouslySetInnerHTML (GHSA-rj57-j38v-3577)", () => {
@@ -78,3 +79,29 @@ describe("sanitizeMindMapData", () => {
expect(data.nodeData.dangerouslySetInnerHTML).toBe(123);
});
});
+
+describe("buildTheme", () => {
+ function container(background?: string) {
+ const el = document.createElement("div");
+ if (background) {
+ el.style.setProperty("--main-background-color", background);
+ }
+ return el;
+ }
+
+ it("draws the map on Trilium's background, keeping the rest of the scheme's theme", () => {
+ const light = buildTheme("light", container("#fafafa"));
+ expect(light.name).toBe(LIGHT_THEME.name);
+ expect(light.cssVar?.["--bgcolor"]).toBe("#fafafa");
+ expect(light.cssVar?.["--main-bgcolor"]).toBe(LIGHT_THEME.cssVar["--main-bgcolor"]);
+ expect(light.palette).toEqual(LIGHT_THEME.palette);
+
+ const dark = buildTheme("dark", container("#242424"));
+ expect(dark.name).toBe(DARK_THEME.name);
+ expect(dark.cssVar?.["--bgcolor"]).toBe("#242424");
+ });
+
+ it("leaves the theme as it is when Trilium's background cannot be resolved", () => {
+ expect(buildTheme("dark", container())).toBe(DARK_THEME);
+ });
+});
diff --git a/apps/client/src/widgets/type_widgets/mind_map/MindMap.tsx b/apps/client/src/widgets/type_widgets/mind_map/MindMap.tsx
index 36928b6617..a5ebf494c4 100644
--- a/apps/client/src/widgets/type_widgets/mind_map/MindMap.tsx
+++ b/apps/client/src/widgets/type_widgets/mind_map/MindMap.tsx
@@ -3,7 +3,7 @@ import "./MindMap.css";
import { NOTE_TYPE_IMAGE_ATTACHMENTS } from "@triliumnext/commons";
import { t } from "i18next";
-import { DARK_THEME, default as VanillaMindElixir, MindElixirData, MindElixirInstance, NodeObj, Operation, THEME as LIGHT_THEME } from "mind-elixir";
+import { DARK_THEME, default as VanillaMindElixir, MindElixirData, MindElixirInstance, NodeObj, Operation, Theme, THEME as LIGHT_THEME } from "mind-elixir";
import type { LangPack } from "mind-elixir/i18n";
import { ComponentChildren, HTMLAttributes, RefObject } from "preact";
import { createPortal } from "preact/compat";
@@ -213,7 +213,7 @@ function MindElixir({ children, containerRef: externalContainerRef, containerPro
el: containerRef.current,
editable,
contextMenu: { locale: buildMindElixirLangPack() },
- theme: defaultColorScheme.current === "dark" ? DARK_THEME : LIGHT_THEME
+ theme: buildTheme(defaultColorScheme.current, containerRef.current)
});
apiRef.current = mind;
@@ -238,9 +238,11 @@ function MindElixir({ children, containerRef: externalContainerRef, containerPro
// React to theme changes.
useEffect(() => {
- if (!apiRef.current) return;
- const newTheme = colorScheme === "dark" ? DARK_THEME : LIGHT_THEME;
- if (apiRef.current.theme === newTheme) return; // Avoid unnecessary theme changes, which can be expensive to render.
+ if (!apiRef.current || !containerRef.current) return;
+ const newTheme = buildTheme(colorScheme, containerRef.current);
+ // Avoid unnecessary theme changes, which can be expensive to render. Compared by name, since
+ // buildTheme hands out a fresh object every time.
+ if (apiRef.current.theme.name === newTheme.name) return;
try {
apiRef.current.changeTheme(newTheme);
} catch (e) {
@@ -332,6 +334,34 @@ function MindElixir({ children, containerRef: externalContainerRef, containerPro
);
}
+/**
+ * The Mind Elixir theme to draw the map with, on a canvas of Trilium's own.
+ *
+ * The map would otherwise sit on a background the library picked for itself — a grey of its own in
+ * either color scheme — which reads as a panel laid over the note rather than as the note. The
+ * canvas is the only surface taken over: the nodes and the branches keep the library's palette.
+ *
+ * The color is resolved against the container rather than handed over as `var(--main-background-color)`,
+ * because the theme is not only written to the container as custom properties: it is also read
+ * straight out of the instance to fill the backdrop of the exported SVG (see export.ts), which is
+ * carried far from any document that could resolve a variable.
+ *
+ * @param colorScheme the color scheme in effect.
+ * @param container the element the map is drawn in, to resolve Trilium's background against.
+ * @returns the library's theme for that scheme, with the canvas replaced; unchanged if Trilium's
+ * background cannot be resolved.
+ */
+export function buildTheme(colorScheme: "light" | "dark", container: HTMLElement): Theme {
+ const baseTheme = (colorScheme === "dark" ? DARK_THEME : LIGHT_THEME);
+ const background = getComputedStyle(container).getPropertyValue("--main-background-color").trim();
+ if (!background) return baseTheme;
+
+ return {
+ ...baseTheme,
+ cssVar: { ...baseTheme.cssVar, "--bgcolor": background }
+ };
+}
+
function createOverlayElement() {
const overlayEl = document.createElement("div");
overlayEl.className = "mind-map-overlay";