Skip to content

Build your personal knowledge base with Trilium Notes

feat(mind-map): mark the nodes there is a memo about

Elian Doran781991abbd22
diff --git a/apps/client/src/widgets/type_widgets/mind_map/MindMap.css b/apps/client/src/widgets/type_widgets/mind_map/MindMap.css
index cad5201345..0a6e742863 100644
--- a/apps/client/src/widgets/type_widgets/mind_map/MindMap.css
+++ b/apps/client/src/widgets/type_widgets/mind_map/MindMap.css
@@ -55,6 +55,63 @@
background-color: var(--bgcolor);
}
+/*
+ * A node the map holds writing about wears a dot in its corner (see memos.ts): a memo is the one
+ * thing a node carries that the map draws nothing of, so without it there is nothing to tell a node
+ * written about from a node not.
+ *
+ * Drawn in the node's own color at a fraction of it rather than in a color of its own, so that it
+ * sits well on a node of any color and on either map theme, and quietly enough that a map full of
+ * memos still reads as a map.
+ *
+ * It follows the text on the line the text is written on, rather than sitting in a corner of the
+ * node: a node has no corner to speak of. The map draws a pill at the top — where the corner of the
+ * box is empty space beside the curve, and a dot left there hangs off the node — and further down
+ * draws no box at all, where a corner is nowhere and a dot in one is adrift in the canvas. What
+ * every node does have is a line of text and an end to it, so the dot follows the text as a mark
+ * upon it.
+ */
+.mind-map-container me-tpc[data-has-memo] {
+ position: relative;
+}
+
+.mind-map-container me-tpc[data-has-memo]::after {
+ content: "";
+ position: absolute;
+ top: calc(50% - 2.5px);
+ inset-inline-end: var(--memo-dot-end);
+ width: 5px;
+ height: 5px;
+ border-radius: 50%;
+ background-color: currentColor;
+ opacity: .45;
+
+ /*
+ * Where the line ends, which is a matter of what the node is drawn as: a pill keeps a cap the
+ * width of its own curve beyond the text, and the dot sits in the middle of that cap — a share
+ * of the curve rather than a set number of pixels, so that it holds wherever a theme puts the
+ * corner. A branch is what most of a map is made of, so its share is the one kept here.
+ */
+ --memo-dot-end: calc(var(--main-radius, 20px) * .5);
+}
+
+.mind-map-container me-root > me-tpc[data-has-memo]::after {
+ --memo-dot-end: calc(var(--root-radius, 30px) * .5);
+}
+
+/*
+ * A child keeps three pixels between its text and its edge and is drawn with no box at all, so it
+ * has no cap for the dot to sit in: there the dot follows the text just past where the node ends.
+ * No further than that — the expander of a node that has children begins seven pixels out.
+ *
+ * Everything below a branch is a child — `me-children` nests within itself all the way down — and
+ * it is nested within `me-main` as well, so this has to be said after the branch's own share above
+ * rather than left to a selector to tell them apart.
+ */
+.mind-map-container me-children me-tpc[data-has-memo]::after {
+ --memo-dot-end: -5px;
+}
+
/*
* The first icon of a node leads its text, as a note's icon leads its title (see icons.ts).
* The ones after it follow the text a little smaller: the first says what the node is, the rest are
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 59bb0109e0..d907cc8756 100644
--- a/apps/client/src/widgets/type_widgets/mind_map/MindMap.tsx
+++ b/apps/client/src/widgets/type_widgets/mind_map/MindMap.tsx
@@ -19,6 +19,7 @@ import { renderIconClasses } from "./icons";
import { renderNodeLinks } from "./links";
import MapContextMenu from "./MapContextMenu";
import MapToolbar, { DirectionToolbar } from "./MapToolbar";
+import { renderMemoMarkers } from "./NodeMemo";
import NodePanel from "./NodePanel";
/**
@@ -298,6 +299,8 @@ function MindElixir({ children, containerRef: externalContainerRef, containerPro
const dressNodes = () => {
renderNodeLinks(mind.nodes);
+ // Beside the node rather than within it, so it changes no box and asks for no measuring.
+ renderMemoMarkers(mind.nodes);
// 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
diff --git a/apps/client/src/widgets/type_widgets/mind_map/NodeMemo.spec.ts b/apps/client/src/widgets/type_widgets/mind_map/NodeMemo.spec.ts
index 0e8802d3bd..a3606edd27 100644
--- a/apps/client/src/widgets/type_widgets/mind_map/NodeMemo.spec.ts
+++ b/apps/client/src/widgets/type_widgets/mind_map/NodeMemo.spec.ts
@@ -1,12 +1,34 @@
-import type { NodeObj } from "mind-elixir";
+import type { NodeObj, Topic } from "mind-elixir";
import { describe, expect, it } from "vitest";
-import { getNodeMemo, toMemoHtml } from "./NodeMemo";
+import { getNodeMemo, MEMO_MARKER_ATTRIBUTE, renderMemoMarkers, toMemoHtml } from "./NodeMemo";
function buildNode(node: Partial<NodeObj> = {}): NodeObj {
return { id: "n1", topic: "Node", ...node };
}
+/**
+ * A map of nodes as Mind Elixir renders them: an element each, carrying the node it stands for —
+ * which is where the pass reads a memo from, the memo being nowhere on the page.
+ */
+function buildMap(...nodes: Partial<NodeObj>[]) {
+ const container = document.createElement("div");
+
+ for (const [ index, node ] of nodes.entries()) {
+ const topic = document.createElement("me-tpc") as Topic;
+ topic.nodeObj = { id: `n${index}`, topic: "Node", ...node } as NodeObj;
+ container.appendChild(topic);
+ }
+
+ return container;
+}
+
+function markedNodes(container: HTMLElement) {
+ return [ ...container.querySelectorAll<Topic>("me-tpc") ]
+ .filter((topic) => topic.hasAttribute(MEMO_MARKER_ATTRIBUTE))
+ .map((topic) => topic.nodeObj.id);
+}
+
describe("the memo of a node", () => {
it("is read from the field the node menu wrote, which Mind Elixir has no say over", () => {
// Its own `note` field is a different one, never written here: reading that instead would
@@ -27,3 +49,59 @@ describe("the memo of a node", () => {
expect(toMemoHtml("")).toBe("");
});
});
+
+describe("renderMemoMarkers", () => {
+ it("marks the nodes there is something written about, and only those", () => {
+ const container = buildMap(
+ { memo: "<p>About the first</p>" } as Partial<NodeObj>,
+ {},
+ // Written through the textarea this replaces, a memo is plain text rather than markup.
+ { memo: "Ask about this" } as Partial<NodeObj>,
+ // Emptied down to nothing is carrying none: the editor hands back an empty string, and
+ // the panel's own tab reads that the same way.
+ { memo: "" } as Partial<NodeObj>
+ );
+
+ renderMemoMarkers(container);
+
+ expect(markedNodes(container)).toEqual([ "n0", "n2" ]);
+ });
+
+ it("takes the mark off a node whose memo is taken away", () => {
+ // The pass runs after every layout, over nodes it has already been over: what it puts on it
+ // must also take off, or a memo cleared would leave the map saying there is one to read.
+ const container = buildMap({ memo: "<p>About it</p>" } as Partial<NodeObj>);
+ renderMemoMarkers(container);
+ expect(markedNodes(container)).toEqual([ "n0" ]);
+
+ const [ topic ] = container.querySelectorAll<Topic>("me-tpc");
+ delete (topic.nodeObj as NodeObj & { memo?: string }).memo;
+ renderMemoMarkers(container);
+
+ expect(markedNodes(container)).toEqual([]);
+ });
+
+ it("stays on a node through being selected, which rewrites what the node wears", () => {
+ // Selecting a node sets its `className` outright — Mind Elixir writes "selected" over
+ // whatever was there — and unselecting takes only that word off again. A mark held as a
+ // class went with the first click and stayed gone until the map was next laid out, which is
+ // to say the dot vanished the moment anyone touched the node it was telling them about.
+ const container = buildMap({ memo: "<p>About it</p>" } as Partial<NodeObj>);
+ renderMemoMarkers(container);
+
+ const [ topic ] = container.querySelectorAll<Topic>("me-tpc");
+ topic.className = "selected";
+ expect(markedNodes(container)).toEqual([ "n0" ]);
+
+ topic.classList.remove("selected");
+ expect(markedNodes(container)).toEqual([ "n0" ]);
+ });
+
+ it("passes over a node that has yet to be given the one it stands for", () => {
+ const container = document.createElement("div");
+ container.appendChild(document.createElement("me-tpc"));
+
+ expect(() => renderMemoMarkers(container)).not.toThrow();
+ expect(container.querySelector("me-tpc")?.hasAttribute(MEMO_MARKER_ATTRIBUTE)).toBe(false);
+ });
+});
diff --git a/apps/client/src/widgets/type_widgets/mind_map/NodeMemo.tsx b/apps/client/src/widgets/type_widgets/mind_map/NodeMemo.tsx
index c65181168b..cd59ed0331 100644
--- a/apps/client/src/widgets/type_widgets/mind_map/NodeMemo.tsx
+++ b/apps/client/src/widgets/type_widgets/mind_map/NodeMemo.tsx
@@ -2,7 +2,7 @@ import "./NodeMemo.css";
import { AttributeEditor, type CKTextEditor, type EditorConfig, MEMO_PLUGINS } from "@triliumnext/ckeditor5";
import type { DISPLAYABLE_LOCALE_IDS } from "@triliumnext/commons";
-import type { NodeObj } from "mind-elixir";
+import type { NodeObj, Topic } from "mind-elixir";
import { useEffect, useLayoutEffect, useRef } from "preact/hooks";
import { t } from "../../../services/i18n";
@@ -165,3 +165,39 @@ export function toMemoHtml(memo: string | null | undefined) {
return memo.split(/\r?\n/).map((line) => `<p>${escapeHtml(line)}</p>`).join("");
}
+
+/**
+ * Worn on the map by a node there is a memo about, which is what draws the mark. See MindMap.css.
+ *
+ * An attribute rather than a class: selecting a node sets `className` outright — Mind Elixir writes
+ * `"selected"` over whatever the node wore — and unselecting only takes that word off again, so a
+ * class of ours would leave with the first click and stay gone until the map was next laid out.
+ */
+export const MEMO_MARKER_ATTRIBUTE = "data-has-memo";
+
+/**
+ * Marks every node on the map carrying a memo, and unmarks every node that no longer carries one.
+ *
+ * A memo is the one thing a node holds that the map draws nothing of — its colors, its icons, its
+ * picture, its link and its tags are all on the node itself — so without a mark there is no telling
+ * a node written about from a node not, short of selecting it and opening the tab. That matters most
+ * on a map that can only be read, where a reader has no reason to click anything at all.
+ *
+ * The mark is left to CSS, this putting only the attribute on: drawn beside the text rather than in
+ * the flow of it, it changes no box, so — unlike the icons, which are dressed in the same pass — it
+ * costs the map no second measuring. It is out of an exported picture for the same reason, which is
+ * where it belongs: a mark saying there is writing here to go and read means nothing in a picture
+ * there is no reading it from.
+ *
+ * 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.
+ */
+export function renderMemoMarkers(container: HTMLElement) {
+ for (const topic of container.querySelectorAll<Topic>("me-tpc")) {
+ // Anything written at all, as the panel's own tab reads it (see `getCommonValue`), so that
+ // the two never disagree over a memo one of them counts and the other does not.
+ const memo = topic.nodeObj && getNodeMemo(topic.nodeObj);
+ topic.toggleAttribute(MEMO_MARKER_ATTRIBUTE, !!memo);
+ }
+}