Skip to content

Build your personal knowledge base with Trilium Notes

feat(mind map): name arrows and summaries in the user's language

Elian Dorane618e6186e16
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 361c0636f9..da0359a49e 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -3525,6 +3525,8 @@
"summary": "Summary",
"new-topic": "New topic",
"new-node": "New node",
+ "new-summary": "Summary",
+ "custom-link": "Custom link",
"text-color": "Text",
"background-color": "Background",
"branch-color": "Branch",
@@ -3550,7 +3552,6 @@
"node-link": "Link",
"add-link": "Add a link",
"choose-link": "Link this node to a note or a page",
- "link-placeholder": "Search notes or paste a URL",
"clear-link": "Remove the link",
"link-mixed": "Different links",
"links-differ": "The selected nodes don't carry the same link.",
diff --git a/apps/client/src/translations/ro/translation.json b/apps/client/src/translations/ro/translation.json
index 5b5940ebf7..7a8314c18a 100644
--- a/apps/client/src/translations/ro/translation.json
+++ b/apps/client/src/translations/ro/translation.json
@@ -2610,7 +2610,9 @@
"clickTips": "Vă rugăm să faceți clic pe nodul țintă",
"summary": "Rezumat",
"new-topic": "Subiect nou",
- "new-node": "Nod nou"
+ "new-node": "Nod nou",
+ "new-summary": "Rezumat",
+ "custom-link": "Legătură personalizată"
},
"llm": {
"feature_not_enabled": "Activați funcția experimentală LLM din Setări → Avansat → Funcții experimentale pentru a folosi integrările AI.",
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 78e9288500..82c75c8833 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,10 +2,13 @@
// 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 i18next from "i18next";
+import { DARK_THEME, MindElixirInstance, THEME as LIGHT_THEME } from "mind-elixir";
+import { beforeAll, describe, expect, it } from "vitest";
-import { buildTheme, sanitizeMindMapData } from "./MindMap.js";
+import englishTranslation from "../../../translations/en/translation.json";
+
+import { buildTheme, localizeBuiltLabels, renameOverlayLabel, sanitizeMindMapData } from "./MindMap.js";
describe("sanitizeMindMapData", () => {
it("strips XSS/RCE vectors from dangerouslySetInnerHTML (GHSA-rj57-j38v-3577)", () => {
@@ -105,3 +108,98 @@ describe("buildTheme", () => {
expect(buildTheme("dark", container())).toBe(DARK_THEME);
});
});
+
+describe("localizeBuiltLabels", () => {
+ // The names under test are read through i18next, which nothing else in the suite sets up.
+ beforeAll(() => i18next.init({ lng: "en", resources: { en: { translation: englishTranslation } } }));
+
+ type Topic = Parameters<MindElixirInstance["createArrow"]>[0];
+ type SummarySvg = Parameters<MindElixirInstance["editSummary"]>[0];
+
+ /** An instance carrying only the methods a case drives, since only those are taken over. */
+ function fakeMind(methods: Partial<MindElixirInstance>) {
+ return { ...methods } as MindElixirInstance;
+ }
+
+ function fakeSummary(label: string) {
+ const labelEl = document.createElement("div");
+ labelEl.textContent = label;
+ return { summaryObj: { label }, labelEl } as SummarySvg;
+ }
+
+ const NODE = {} as Topic;
+
+ it("names a new arrow, letting one it is given a name for through", () => {
+ const given: unknown[] = [];
+ const mind = fakeMind({ createArrow: (_from, _to, options) => void given.push(options) });
+ localizeBuiltLabels(mind);
+
+ mind.createArrow(NODE, NODE);
+ mind.createArrow(NODE, NODE, { bidirectional: true });
+ mind.createArrow(NODE, NODE, { label: "Mine" } as Parameters<MindElixirInstance["createArrow"]>[2]);
+
+ expect(given).toEqual([
+ { label: "Custom link" },
+ { label: "Custom link", bidirectional: true },
+ { label: "Mine" }
+ ]);
+ });
+
+ it("names a summary as it is built, and leaves one opened for editing alone", () => {
+ const built = fakeSummary("summary");
+ const existing = fakeSummary("summary");
+ const edited: SummarySvg[] = [];
+ const mind = fakeMind({
+ // The library hands a summary it has just built straight to the editor, through the
+ // instance — which is the moment the name is replaced in.
+ createSummary: function () { this.editSummary(built); },
+ editSummary: (summary) => void edited.push(summary)
+ });
+ localizeBuiltLabels(mind);
+
+ mind.createSummary();
+ mind.editSummary(existing);
+
+ expect(built.summaryObj.label).toBe("Summary");
+ expect(built.labelEl?.textContent).toBe("Summary");
+ // Named "summary" by whoever wrote it, so it stays that way.
+ expect(existing.summaryObj.label).toBe("summary");
+ expect(existing.labelEl?.textContent).toBe("summary");
+ // Both still reach the editor, renamed or not.
+ expect(edited).toEqual([ built, existing ]);
+ });
+});
+
+describe("renameOverlayLabel", () => {
+ function overlayLabel(anchor: string, width: number, height: number) {
+ const label = document.createElement("div");
+ Object.assign(label.dataset, { x: "100", y: "50", anchor });
+ // jsdom lays nothing out, so the size the label is placed against is stated outright.
+ Object.defineProperty(label, "clientWidth", { value: width });
+ Object.defineProperty(label, "clientHeight", { value: height });
+ return label;
+ }
+
+ it("places a renamed label against the point and the side it was built for", () => {
+ const trailing = overlayLabel("start", 40, 20);
+ renameOverlayLabel(trailing, "Rezumat");
+ expect(trailing.textContent).toBe("Rezumat");
+ expect([ trailing.style.left, trailing.style.top ]).toEqual([ "100px", "40px" ]);
+
+ // The ones that grow leftwards move by what they gained, so the point they mark stays put.
+ const leading = overlayLabel("end", 40, 20);
+ renameOverlayLabel(leading, "Rezumat");
+ expect([ leading.style.left, leading.style.top ]).toEqual([ "60px", "40px" ]);
+
+ const centered = overlayLabel("middle", 40, 20);
+ renameOverlayLabel(centered, "Rezumat");
+ expect([ centered.style.left, centered.style.top ]).toEqual([ "80px", "40px" ]);
+ });
+
+ it("renames a label that records no point of its own without placing it anywhere", () => {
+ const label = document.createElement("div");
+ renameOverlayLabel(label, "Rezumat");
+ expect(label.textContent).toBe("Rezumat");
+ expect([ label.style.left, label.style.top ]).toEqual([ "", "" ]);
+ });
+});
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 9ce970e35b..b4b372a18f 100644
--- a/apps/client/src/widgets/type_widgets/mind_map/MindMap.tsx
+++ b/apps/client/src/widgets/type_widgets/mind_map/MindMap.tsx
@@ -222,6 +222,8 @@ function MindElixir({ children, containerRef: externalContainerRef, containerPro
theme: buildTheme(defaultColorScheme.current, containerRef.current)
});
+ localizeBuiltLabels(mind);
+
apiRef.current = mind;
if (externalApiRef) {
externalApiRef.current = mind;
@@ -368,6 +370,73 @@ export function buildTheme(colorScheme: "light" | "dark", container: HTMLElement
};
}
+/**
+ * Names the arrows and summaries Mind Elixir builds in the user's language.
+ *
+ * Neither name is covered by the library's own translations — its lang pack carries the twelve
+ * context-menu entries and nothing else — and neither is offered as an option, so each is taken
+ * where the library leaves an opening:
+ *
+ * - An arrow is built from `{ label: "Custom Link", ...options }`, so a label carried among its
+ * options stands in place of the English one. Its options are not described as carrying a label,
+ * hence the cast; were the library to stop passing what it is not expecting through, the arrow
+ * would be named in English again and nothing else about it would change.
+ * - A summary's name is written into it and cannot be given, so it is replaced in the moment
+ * between the summary being built and being handed to the editor that opens over it — which the
+ * library reaches through the instance. The editor reads both the text it opens with and the text
+ * it falls back to, should the summary be left empty, from the summary itself, so both follow.
+ *
+ * @param mind the instance to name after, taken over for as long as it lives.
+ */
+export function localizeBuiltLabels(mind: MindElixirInstance) {
+ const { createArrow, createSummary, editSummary } = mind;
+
+ mind.createArrow = function (from, to, options) {
+ const named = { label: t("mind-map.custom-link"), ...options };
+ createArrow.call(this, from, to, named as Parameters<typeof createArrow>[2]);
+ };
+
+ // Raised only around a summary the map is building, so that opening an existing one for editing
+ // leaves whatever it is called alone — including a summary someone did call "summary".
+ let building = false;
+
+ mind.createSummary = function (options) {
+ building = true;
+ try {
+ createSummary.call(this, options);
+ } finally {
+ building = false;
+ }
+ };
+
+ mind.editSummary = function (summary) {
+ if (building && summary?.summaryObj) {
+ summary.summaryObj.label = t("mind-map.new-summary");
+ renameOverlayLabel(summary.labelEl, summary.summaryObj.label);
+ }
+ editSummary.call(this, summary);
+ };
+}
+
+/**
+ * Writes a text into one of the labels Mind Elixir lays over the map, which are placed by hand
+ * against the point they belong to rather than by the browser — so one that is no longer the size it
+ * was measured at has to be placed again, from the point and the side it recorded when it was built.
+ */
+export function renameOverlayLabel(label: HTMLElement | undefined, text: string) {
+ if (!label) return;
+
+ label.textContent = text;
+
+ const x = Number(label.dataset.x);
+ const y = Number(label.dataset.y);
+ if (!Number.isFinite(x) || !Number.isFinite(y)) return;
+
+ const width = label.clientWidth;
+ label.style.left = `${label.dataset.anchor === "end" ? x - width : label.dataset.anchor === "middle" ? x - width / 2 : x}px`;
+ label.style.top = `${y - label.clientHeight / 2}px`;
+}
+
function createOverlayElement() {
const overlayEl = document.createElement("div");
overlayEl.className = "mind-map-overlay";