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): add a tag editor to the node panel
Elian Doranf6c4f9c79b2b
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex e2c6c4f6e3..b7beb8bf11 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -3530,7 +3530,10 @@"font-size-small": "S","font-size-medium": "M","font-size-large": "L",- "font-size-extra-large": "XL"+ "font-size-extra-large": "XL",+ "tags": "Tags",+ "add-tag": "Add tag",+ "remove-tag": "Remove tag"},"llm": {"disabled_placeholder": "AI features are turned off. Enable them to configure providers and MCP.",diff --git a/apps/client/src/widgets/type_widgets/MindMapNodePanel.spec.tsx b/apps/client/src/widgets/type_widgets/MindMapNodePanel.spec.tsxindex b36ae031e2..a5057da273 100644--- a/apps/client/src/widgets/type_widgets/MindMapNodePanel.spec.tsx+++ b/apps/client/src/widgets/type_widgets/MindMapNodePanel.spec.tsx@@ -2,7 +2,7 @@ import type { MindElixirInstance, NodeObj } from "mind-elixir";import { describe, expect, it, vi } from "vitest";import { renderInto } from "../../test/render";-import MindMapNodePanel, { DEFAULT_FONT_SIZE, getCommonValue, MIXED, NODE_BACKGROUND_COLORS, NODE_COLORS } from "./MindMapNodePanel";+import MindMapNodePanel, { applyTagTexts, DEFAULT_FONT_SIZE, getCommonValue, MIXED, NODE_BACKGROUND_COLORS, NODE_COLORS } from "./MindMapNodePanel";function buildNode(node: Partial<NodeObj> = {}): NodeObj {return { id: "n1", topic: "Node", ...node };@@ -26,6 +26,7 @@ const SIZE = 0;const TEXT = 1;const BACKGROUND = 2;const BRANCH = 3;+const TAGS = 4;function section(container: HTMLElement, index: number) {const sections = container.querySelectorAll<HTMLElement>(".mind-map-node-panel-section");@@ -79,6 +80,18 @@ describe("getCommonValue", () => {});});+describe("applyTagTexts", () => {+ it("keeps a styled tag while its text stands, and plain text for the rest", () => {+ const styled = { text: "urgent", style: { color: "red" } };++ expect(applyTagTexts([styled, "later"], ["urgent", "done"])).toEqual([styled, "done"]);+ // Dropped from the texts, the styling goes with it rather than following the tag back.+ expect(applyTagTexts([styled], ["done"])).toEqual(["done"]);+ expect(applyTagTexts(undefined, ["done"])).toEqual(["done"]);+ expect(applyTagTexts([styled], [])).toEqual([]);+ });+});+describe("MindMapNodePanel", () => {it("offers every row on a single line, backgrounds as translucent variants of the same hues", () => {const nodes = [buildNode()];@@ -182,6 +195,36 @@ describe("MindMapNodePanel", () => {expect(reshapeNode).toHaveBeenNthCalledWith(1, mind.currentNodes[0], { branchColor: NODE_COLORS[1] });});+ it("edits the tags of a single node, keeping the styling of the ones that stay", () => {+ const styled = { text: "urgent", style: { color: "red" } };+ const nodes = [buildNode({ tags: [styled, "later"] })];+ const { mind, reshapeNode } = buildMind(nodes);++ const container = renderInto(<MindMapNodePanel mind={mind} nodes={nodes} />);+ const tags = section(container, TAGS);++ expect([...tags.querySelectorAll(".tn-chip")].map((chip) => chip.textContent)).toEqual(+ expect.arrayContaining([expect.stringContaining("urgent"), expect.stringContaining("later")]));++ const input = tags.querySelector<HTMLInputElement>("input");+ if (!input) throw new Error("the tag field has no box to type in");+ input.value = "done";+ input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true }));++ expect(reshapeNode).toHaveBeenCalledWith(mind.currentNodes[0], { tags: [styled, "later", "done"] });+ });++ it("leaves the tags out for a selection of several nodes", () => {+ // Every node carries its own, so a field standing for all of them could only overwrite.+ const nodes = [buildNode({ id: "a", tags: ["one"] }), buildNode({ id: "b", tags: ["two"] })];+ const { mind } = buildMind(nodes);++ const container = renderInto(<MindMapNodePanel mind={mind} nodes={nodes} />);++ expect(container.querySelectorAll(".mind-map-node-panel-section")).toHaveLength(TAGS);+ expect(container.querySelector(".tn-field")).toBeNull();+ });+it("keeps clicks and key presses from reaching the map underneath", () => {const nodes = [buildNode()];const { mind } = buildMind(nodes);diff --git a/apps/client/src/widgets/type_widgets/MindMapNodePanel.tsx b/apps/client/src/widgets/type_widgets/MindMapNodePanel.tsxindex 8e2bbfe7e7..d63fc9d654 100644--- a/apps/client/src/widgets/type_widgets/MindMapNodePanel.tsx+++ b/apps/client/src/widgets/type_widgets/MindMapNodePanel.tsx@@ -1,9 +1,10 @@import "./MindMapNodePanel.css";-import type { MindElixirInstance, NodeObj } from "mind-elixir";+import type { MindElixirInstance, NodeObj, TagObj } from "mind-elixir";import { ComponentChildren } from "preact";import { t } from "../../services/i18n";+import ValuesInput from "../attribute_widgets/values_input";import ColorPicker, { DEFAULT_COLOR_PALETTE } from "../react/ColorPicker";import SegmentedChoice from "../react/SegmentedChoice";@@ -35,6 +36,9 @@ export default function MindMapNodePanel({ mind, nodes }: MindMapNodePanelProps)const textColor = getCommonValue(nodes, (node) => node.style?.color);const backgroundColor = getCommonValue(nodes, (node) => node.style?.background);const branchColor = getCommonValue(nodes, (node) => node.branchColor);+ // Tags belong to one node rather than to a shape shared by the selection: offering them for+ // several would mean overwriting each node's own with whatever is typed.+ const taggedNode = (nodes.length === 1 ? nodes[0] : null);/*** Applies a patch to every selected node. The selection is read back from the instance rather@@ -86,6 +90,19 @@ export default function MindMapNodePanel({ mind, nodes }: MindMapNodePanelProps)onChange={(color) => patchSelectedNodes({ branchColor: color ?? "" })}/></PanelSection>++ {taggedNode && (+ <PanelSection label={t("mind-map.tags")}>+ <ValuesInput+ labelType="text"+ values={(taggedNode.tags ?? []).map(getTagText)}+ placeholder={t("mind-map.add-tag")}+ addButtonText={t("mind-map.add-tag")}+ removeButtonText={t("mind-map.remove-tag")}+ onCommit={(texts) => patchSelectedNodes({ tags: applyTagTexts(taggedNode.tags, texts) })}+ />+ </PanelSection>+ )}</div>);}@@ -110,6 +127,20 @@ function buildFontSizeOptions() {];}+/** The text a tag reads as, whether it carries a style of its own or is only the text. */+export function getTagText(tag: string | TagObj) {+ return typeof tag === "string" ? tag : tag.text;+}++/**+ * Turns the texts a node is now tagged with back into tags. A tag that carries a style of its own+ * is kept as it was for as long as its text is, so that a map made elsewhere doesn't lose the+ * styling of its tags to one being added beside them.+ */+export function applyTagTexts(tags: NodeObj["tags"], texts: string[]): (string | TagObj)[] {+ return texts.map((text) => tags?.find((tag) => getTagText(tag) === text) ?? text);+}+/** Turns the outcome of {@link getCommonValue} into the matching {@link ColorPicker} state. */function toPickerValue(value: string | null | typeof MIXED) {return {