Skip to content

Build your personal knowledge base with Trilium Notes

fix(mind-map): measure the branches once a node's icon is drawn

Elian Dorand6818d73c49f
diff --git a/apps/client/src/widgets/type_widgets/MindMap.tsx b/apps/client/src/widgets/type_widgets/MindMap.tsx
index 52433d03b7..2d060d7c73 100644
--- a/apps/client/src/widgets/type_widgets/MindMap.tsx
+++ b/apps/client/src/widgets/type_widgets/MindMap.tsx
@@ -281,7 +281,15 @@ function MindElixir({ children, containerRef: externalContainerRef, containerPro
const mind = apiRef.current;
if (!mind) return;
- const renderIcons = () => renderIconClasses(mind.nodes);
+ const renderIcons = () => {
+ // An icon takes far less room than the class it arrived as, so the node it sits on
+ // narrows the moment it is dressed — after the branches have been measured against the
+ // wider one, leaving them reaching past it. Measure again, but only where something
+ // was dressed: the second pass finds nothing to do and the redrawing stops there.
+ if (renderIconClasses(mind.nodes)) {
+ mind.linkDiv();
+ }
+ };
renderIcons();
mind.bus.addListener("linkDiv", renderIcons);
diff --git a/apps/client/src/widgets/type_widgets/helpers/mind_map_icons.spec.ts b/apps/client/src/widgets/type_widgets/helpers/mind_map_icons.spec.ts
index 27d1c71515..cde67d8ade 100644
--- a/apps/client/src/widgets/type_widgets/helpers/mind_map_icons.spec.ts
+++ b/apps/client/src/widgets/type_widgets/helpers/mind_map_icons.spec.ts
@@ -23,7 +23,7 @@ describe("renderIconClasses", () => {
it("dresses a Trilium icon class, whichever pack it belongs to", () => {
const container = buildNode("bx bx-star", "mdi mdi-cube");
- renderIconClasses(container);
+ expect(renderIconClasses(container)).toBe(true);
const [ boxicon, mdi ] = container.querySelectorAll(".icons > span");
expect(boxicon.className).toBe("bx bx-star");
@@ -36,7 +36,7 @@ describe("renderIconClasses", () => {
// could be drawn; and a sentence is a sentence.
const container = buildNode("⭐", "phosphor ph-cube", "bx");
- renderIconClasses(container);
+ expect(renderIconClasses(container)).toBe(false);
expect([ ...container.querySelectorAll(".icons > span") ].map((icon) => [ icon.className, icon.textContent ]))
.toEqual([ [ "", "⭐" ], [ "", "phosphor ph-cube" ], [ "", "bx" ] ]);
@@ -54,12 +54,14 @@ describe("renderIconClasses", () => {
expect(icon?.childNodes[0].textContent).toBe("");
});
- it("can be run again over what it has already dressed", () => {
- // It runs after every layout, and a layout does not always rebuild the node.
+ it("can be run again over what it has already dressed, reporting that it changed nothing", () => {
+ // It runs after every layout, and a layout does not always rebuild the node. Saying that
+ // nothing was dressed is what stops the caller measuring the map over and over: it measures
+ // again only where a node has just narrowed.
const container = buildNode("bx bx-star");
- renderIconClasses(container);
- renderIconClasses(container);
+ expect(renderIconClasses(container)).toBe(true);
+ expect(renderIconClasses(container)).toBe(false);
const icon = container.querySelector(".icons > span");
expect(icon?.className).toBe("bx bx-star");
diff --git a/apps/client/src/widgets/type_widgets/helpers/mind_map_icons.ts b/apps/client/src/widgets/type_widgets/helpers/mind_map_icons.ts
index ba5084600c..0c5d72fd62 100644
--- a/apps/client/src/widgets/type_widgets/helpers/mind_map_icons.ts
+++ b/apps/client/src/widgets/type_widgets/helpers/mind_map_icons.ts
@@ -9,8 +9,13 @@
* Called for the whole map after every layout, which is what follows any change to a node.
*
* @param container the element holding the rendered nodes.
+ * @returns whether anything was dressed, and with it the width of the node it sits on — the class
+ * takes far more room as words than as the icon it becomes, so a caller that has measured
+ * the map has to measure it again.
*/
export function renderIconClasses(container: HTMLElement) {
+ let dressed = false;
+
for (const iconEl of container.querySelectorAll<HTMLElement>(".icons > span")) {
const iconClass = iconEl.textContent?.trim() ?? "";
if (!isIconClass(iconClass)) continue;
@@ -21,7 +26,10 @@ export function renderIconClasses(container: HTMLElement) {
// span holding no children at all throws — which failed the save of any map with an icon,
// the preview being rendered through that exporter.
iconEl.replaceChildren(document.createTextNode(""));
+ dressed = true;
}
+
+ return dressed;
}
/**