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
feat(mind-map): set the first icon of a node ahead of its text
Elian Dorand14ae5ce1487
diff --git a/apps/client/src/widgets/type_widgets/MindMap.css b/apps/client/src/widgets/type_widgets/MindMap.cssindex 4a68d8d945..512844e49f 100644--- a/apps/client/src/widgets/type_widgets/MindMap.css+++ b/apps/client/src/widgets/type_widgets/MindMap.css@@ -40,6 +40,25 @@background-color: var(--bgcolor);}+/*+ * The first icon of a node leads its text, as a note's icon leads its title (see mind_map_icons).+ * The ones after it follow the text a little smaller: the first says what the node is, the rest are+ * marks upon it.+ */+.mind-map-container .mind-map-lead-icon {+ margin-inline-end: .3em;+ vertical-align: middle;+}++.mind-map-container .icons {+ font-size: .8em;+}++/* Emptied by the move, the wrapper would otherwise hold on to the gap it keeps before itself. */+.mind-map-container .icons:empty {+ display: none;+}+.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_icons.spec.ts b/apps/client/src/widgets/type_widgets/helpers/mind_map_icons.spec.tsindex cde67d8ade..1f2a583764 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@@ -1,16 +1,25 @@import { afterEach, beforeEach, describe, expect, it } from "vitest";-import { renderIconClasses } from "./mind_map_icons";+import { LEAD_ICON_CLASS, renderIconClasses } from "./mind_map_icons";const uncheckedWindow = window as unknown as { glob: { iconRegistry?: unknown } };-/** Renders what Mind Elixir makes of a node's icons: the text it was given, one span each. */+/**+ * Renders what Mind Elixir makes of a node: its text, and after it the icons it was given as the+ * text they arrived as, one span each.+ */function buildNode(...icons: string[]) {const container = document.createElement("div");- container.innerHTML = `<me-tpc><span class="icons">${icons.map((icon) => `<span>${icon}</span>`).join("")}</span></me-tpc>`;+ container.innerHTML = `<me-tpc><span class="text">Topic</span>`+ + `<span class="icons">${icons.map((icon) => `<span>${icon}</span>`).join("")}</span></me-tpc>`;return container;}+/** Every icon of the node, in the order they are worn — the one moved ahead of the text included. */+function icons(container: HTMLElement) {+ return [ ...container.querySelectorAll<HTMLElement>(`me-tpc > .${LEAD_ICON_CLASS}, me-tpc > .icons > span`) ];+}+describe("renderIconClasses", () => {beforeEach(() => {uncheckedWindow.glob.iconRegistry = { sources: [ { prefix: "bx" }, { prefix: "mdi" } ] };@@ -25,21 +34,22 @@ describe("renderIconClasses", () => {expect(renderIconClasses(container)).toBe(true);- const [ boxicon, mdi ] = container.querySelectorAll(".icons > span");- expect(boxicon.className).toBe("bx bx-star");+ const [ boxicon, mdi ] = icons(container);+ expect(boxicon.classList).toContain("bx-star");expect(boxicon.textContent).toBe("");expect(mdi.className).toBe("mdi mdi-cube");});it("leaves alone whatever is not one, so that a map made elsewhere keeps its icons", () => {// An emoji is an icon in its own right; a pack that is not installed names nothing that- // could be drawn; and a sentence is a sentence.+ // could be drawn; and a sentence is a sentence. The first still leads the text, being the+ // node's first icon whether or not Trilium is the one drawing it.const container = buildNode("⭐", "phosphor ph-cube", "bx");- expect(renderIconClasses(container)).toBe(false);+ renderIconClasses(container);- expect([ ...container.querySelectorAll(".icons > span") ].map((icon) => [ icon.className, icon.textContent ]))- .toEqual([ [ "", "⭐" ], [ "", "phosphor ph-cube" ], [ "", "bx" ] ]);+ expect(icons(container).map((icon) => [ icon.textContent, icon.classList.contains(LEAD_ICON_CLASS) ]))+ .toEqual([ [ "⭐", true ], [ "phosphor ph-cube", false ], [ "bx", false ] ]);});it("leaves a dressed icon something to read, for the exporter's sake", () => {@@ -49,7 +59,7 @@ describe("renderIconClasses", () => {renderIconClasses(container);- const icon = container.querySelector(".icons > span");+ const [ icon ] = icons(container);expect(icon?.childNodes).toHaveLength(1);expect(icon?.childNodes[0].textContent).toBe("");});@@ -63,17 +73,52 @@ describe("renderIconClasses", () => {expect(renderIconClasses(container)).toBe(true);expect(renderIconClasses(container)).toBe(false);- const icon = container.querySelector(".icons > span");- expect(icon?.className).toBe("bx bx-star");+ const [ icon ] = icons(container);+ expect(icon?.className).toBe(`bx bx-star ${LEAD_ICON_CLASS}`);expect(icon?.textContent).toBe("");});+ it("sets the first icon ahead of the node's text, leaving the rest after it", () => {+ const container = buildNode("bx bx-star", "mdi mdi-cube", "⭐");++ renderIconClasses(container);++ // The first is a child of the node itself now, standing before its text; the others stay in+ // the wrapper Mind Elixir put them in, which is where its exporter looks for them.+ expect([ ...(container.querySelector("me-tpc")?.children ?? []) ].map((child) => child.className))+ .toEqual([ `bx bx-star ${LEAD_ICON_CLASS}`, "text", "icons" ]);+ expect(container.querySelectorAll(".icons > span")).toHaveLength(2);+ });++ it("moves the first icon once, however often it is run", () => {+ // Run after every layout, and a layout does not always rebuild the node — moving again+ // would take the second icon to the front along with the first.+ const container = buildNode("bx bx-star", "mdi mdi-cube");++ expect(renderIconClasses(container)).toBe(true);+ expect(renderIconClasses(container)).toBe(false);++ expect(container.querySelectorAll(`.${LEAD_ICON_CLASS}`)).toHaveLength(1);+ expect(icons(container).map((icon) => icon.className.replace(` ${LEAD_ICON_CLASS}`, "")))+ .toEqual([ "bx bx-star", "mdi mdi-cube" ]);+ });++ it("leaves a node with no text of its own — nothing to lead — as it is", () => {+ const container = document.createElement("div");+ container.innerHTML = `<me-tpc><span class="icons"><span>bx bx-star</span></span></me-tpc>`;++ renderIconClasses(container);++ expect(container.querySelector(`.${LEAD_ICON_CLASS}`)).toBeNull();+ expect(container.querySelector(".icons > span")?.className).toBe("bx bx-star");+ });+it("holds off until the icon packs are known", () => {delete uncheckedWindow.glob.iconRegistry;const container = buildNode("bx bx-star");renderIconClasses(container);- expect(container.querySelector(".icons > span")?.textContent).toBe("bx bx-star");+ expect(icons(container)[0]?.textContent).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.tsindex 0c5d72fd62..cb9f32852b 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@@ -1,5 +1,9 @@+/** Marks the icon moved ahead of a node's text, both to dress it and to know it has been moved. */+export const LEAD_ICON_CLASS = "mind-map-lead-icon";+/**- * Dresses the node icons a map carries as Trilium icon classes.+ * Dresses the node icons a map carries as Trilium icon classes, and sets the first of them ahead of+ * the node's text.** Mind Elixir renders an icon as text — its topic renderer escapes what it is given and drops it* into a `<span>` — so a class such as `bx bx-star` arrives as those very words. Putting the class@@ -9,12 +13,12 @@* 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+ * @returns whether anything was changed, 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;+ let changed = false;for (const iconEl of container.querySelectorAll<HTMLElement>(".icons > span")) {const iconClass = iconEl.textContent?.trim() ?? "";@@ -26,10 +30,41 @@ 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;+ changed = true;+ }++ return leadWithFirstIcon(container) || changed;+}++/**+ * Moves the first icon of every node ahead of its text, where a note wears its own.+ *+ * Mind Elixir builds a topic in an order of its own — the icons after the text — and the parts of+ * it are laid inline, so there is no reordering them but by moving one. It is moved back on every+ * render of the node, and this runs after every one of those.+ *+ * The ones that follow stay where they are, which is also what keeps the exporter reading them: it+ * looks for the icons inside their wrapper, and what leaves it is not drawn there. That costs+ * nothing today, an exported map carrying no icon font to draw any of them with.+ */+function leadWithFirstIcon(container: HTMLElement) {+ let moved = false;++ for (const topic of container.querySelectorAll<HTMLElement>("me-tpc")) {+ // One already at the front is this same pass having run before, the node not having been+ // rendered since; moving again would take the second icon along with it.+ if (topic.querySelector(`:scope > .${LEAD_ICON_CLASS}`)) continue;++ const firstIcon = topic.querySelector<HTMLElement>(":scope > .icons > span");+ const text = topic.querySelector(":scope > .text");+ if (!firstIcon || !text) continue;++ firstIcon.classList.add(LEAD_ICON_CLASS);+ topic.insertBefore(firstIcon, text);+ moved = true;}- return dressed;+ return moved;}/**