Skip to content

Build your personal knowledge base with Trilium Notes

feat(mind-map): offer the tags of a whole selection where they agree

Elian Doran9117e069a9ef
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index fc6a0c0b26..101ed508c4 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -3533,6 +3533,7 @@
"font-size-extra-large": "XL",
"tags": "Tags",
"tags-placeholder": "Type a tag and press Enter",
+ "tags-differ": "The selected nodes don't carry the same tags, so these can only be read.",
"add-tag": "Add this tag",
"remove-tag": "Remove this tag"
},
diff --git a/apps/client/src/widgets/type_widgets/MindMapNodePanel.spec.tsx b/apps/client/src/widgets/type_widgets/MindMapNodePanel.spec.tsx
index a5057da273..06df12186b 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, { applyTagTexts, DEFAULT_FONT_SIZE, getCommonValue, MIXED, NODE_BACKGROUND_COLORS, NODE_COLORS } from "./MindMapNodePanel";
+import MindMapNodePanel, { applyTagTexts, DEFAULT_FONT_SIZE, gatherTags, getCommonValue, MIXED, NODE_BACKGROUND_COLORS, NODE_COLORS } from "./MindMapNodePanel";
function buildNode(node: Partial<NodeObj> = {}): NodeObj {
return { id: "n1", topic: "Node", ...node };
@@ -33,6 +33,18 @@ function section(container: HTMLElement, index: number) {
return sections[index];
}
+function tagTexts(container: HTMLElement) {
+ return [ ...section(container, TAGS).querySelectorAll(".tn-chip > span") ].map((chip) => chip.textContent);
+}
+
+/** Types a tag into the field and settles it, the way Enter does for someone using the panel. */
+function typeTag(container: HTMLElement, text: string) {
+ const input = section(container, TAGS).querySelector<HTMLInputElement>("input");
+ if (!input) throw new Error("the tag field has no box to type in");
+ input.value = text;
+ input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true }));
+}
+
function sizeButtons(container: HTMLElement) {
return Array.from(section(container, SIZE).querySelectorAll<HTMLElement>(".btn"));
}
@@ -80,6 +92,29 @@ describe("getCommonValue", () => {
});
});
+describe("gatherTags", () => {
+ it("hands over the tags a selection agrees on, whatever order each holds them in", () => {
+ expect(gatherTags([buildNode()])).toEqual({ texts: [], readOnly: false });
+ expect(gatherTags([buildNode({ tags: ["one", { text: "two" }] })]))
+ .toEqual({ texts: ["one", "two"], readOnly: false });
+ expect(gatherTags([
+ buildNode({ id: "a", tags: ["one", "two"] }),
+ buildNode({ id: "b", tags: ["two", "one"] })
+ ])).toEqual({ texts: ["one", "two"], readOnly: false });
+ });
+
+ it("gathers everything the selection carries, for reading only, once they disagree", () => {
+ expect(gatherTags([
+ buildNode({ id: "a", tags: ["one", "shared"] }),
+ buildNode({ id: "b", tags: ["shared", "two"] })
+ ])).toEqual({ texts: ["one", "shared", "two"], readOnly: true });
+
+ // One node holding none of them is a disagreement like any other.
+ expect(gatherTags([buildNode({ id: "a", tags: ["one"] }), buildNode({ id: "b" })]))
+ .toEqual({ texts: ["one"], readOnly: true });
+ });
+});
+
describe("applyTagTexts", () => {
it("keeps a styled tag while its text stands, and plain text for the rest", () => {
const styled = { text: "urgent", style: { color: "red" } };
@@ -201,28 +236,50 @@ describe("MindMapNodePanel", () => {
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")]));
+ expect(tagTexts(container)).toEqual(["urgent", "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 }));
+ typeTag(container, "done");
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);
+ it("edits the tags of several nodes that carry the same ones, each keeping its own styling", () => {
+ // The same tag, dressed differently on each node: what is written back has to be read from
+ // the node it is written to, rather than from whichever node the field happened to show.
+ const nodes = [
+ buildNode({ id: "a", tags: [{ text: "urgent", style: { color: "red" } }] }),
+ buildNode({ id: "b", tags: [{ text: "urgent", style: { color: "blue" } }] })
+ ];
+ const { mind, reshapeNode } = buildMind(nodes);
+
+ const container = renderInto(<MindMapNodePanel mind={mind} nodes={nodes} />);
+
+ expect(tagTexts(container)).toEqual(["urgent"]);
+ expect(container.querySelector<HTMLInputElement>(".tn-field input")?.disabled).toBe(false);
+
+ typeTag(container, "done");
+
+ expect(reshapeNode).toHaveBeenNthCalledWith(1, mind.currentNodes[0], { tags: [nodes[0].tags?.[0], "done"] });
+ expect(reshapeNode).toHaveBeenNthCalledWith(2, mind.currentNodes[1], { tags: [nodes[1].tags?.[0], "done"] });
+ });
+
+ it("gathers the tags of a selection that disagrees, for reading only", () => {
+ const nodes = [
+ buildNode({ id: "a", tags: ["one", "shared"] }),
+ buildNode({ id: "b", tags: ["shared", "two"] })
+ ];
+ const { mind, reshapeNode } = 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();
+ expect(tagTexts(container)).toEqual(["one", "shared", "two"]);
+ expect(container.querySelector<HTMLInputElement>(".tn-field input")?.disabled).toBe(true);
+ // Nothing to press: a removal would take a tag off a node that never had it.
+ expect([...container.querySelectorAll<HTMLButtonElement>(".tn-chip-remove")].every((button) => button.disabled)).toBe(true);
+
+ typeTag(container, "done");
+ expect(reshapeNode).not.toHaveBeenCalled();
});
it("keeps clicks and key presses from reaching the map underneath", () => {
diff --git a/apps/client/src/widgets/type_widgets/MindMapNodePanel.tsx b/apps/client/src/widgets/type_widgets/MindMapNodePanel.tsx
index 079cd136db..d5589f7ac6 100644
--- a/apps/client/src/widgets/type_widgets/MindMapNodePanel.tsx
+++ b/apps/client/src/widgets/type_widgets/MindMapNodePanel.tsx
@@ -36,17 +36,16 @@ 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);
+ const tags = gatherTags(nodes);
/**
* Applies a patch to every selected node. The selection is read back from the instance rather
* than taken from the props, so that the elements the patch is applied to are the live ones.
+ * The patch may be derived per node, for what is written depending on what a node already has.
*/
- function patchSelectedNodes(patch: Partial<NodeObj>) {
+ function patchSelectedNodes(patch: Partial<NodeObj> | ((node: NodeObj) => Partial<NodeObj>)) {
for (const topic of mind.currentNodes) {
- mind.reshapeNode(topic, patch);
+ mind.reshapeNode(topic, typeof patch === "function" ? patch(topic.nodeObj) : patch);
}
}
@@ -91,18 +90,22 @@ export default function MindMapNodePanel({ mind, nodes }: MindMapNodePanelProps)
/>
</PanelSection>
- {taggedNode && (
- <PanelSection label={t("mind-map.tags")}>
- <ValuesInput
- labelType="text"
- values={(taggedNode.tags ?? []).map(getTagText)}
- placeholder={t("mind-map.tags-placeholder")}
- addButtonText={t("mind-map.add-tag")}
- removeButtonText={t("mind-map.remove-tag")}
- onCommit={(texts) => patchSelectedNodes({ tags: applyTagTexts(taggedNode.tags, texts) })}
- />
- </PanelSection>
- )}
+ <PanelSection
+ label={t("mind-map.tags")}
+ title={tags.readOnly ? t("mind-map.tags-differ") : undefined}
+ >
+ <ValuesInput
+ labelType="text"
+ values={tags.texts}
+ placeholder={t("mind-map.tags-placeholder")}
+ addButtonText={t("mind-map.add-tag")}
+ removeButtonText={t("mind-map.remove-tag")}
+ disabled={tags.readOnly}
+ // Derived per node: the nodes agree on the texts, but each may dress its own
+ // tags differently, and that is kept by reading from the node being written to.
+ onCommit={(texts) => patchSelectedNodes((node) => ({ tags: applyTagTexts(node.tags, texts) }))}
+ />
+ </PanelSection>
</div>
);
}
@@ -127,6 +130,26 @@ function buildFontSizeOptions() {
];
}
+/**
+ * What the tag field stands for, given the selection.
+ *
+ * Tags belong to a node rather than to a shape the selection shares, so a field standing for
+ * several of them can only be edited where they already agree — one node always does. Where they
+ * don't, the field shows everything the selection carries between them, for reading: taking a tag
+ * as read would mean writing one node's tags over another's.
+ */
+export function gatherTags(nodes: NodeObj[]): { texts: string[], readOnly: boolean } {
+ const perNode = nodes.map((node) => (node.tags ?? []).map(getTagText));
+ const [ first = [], ...rest ] = perNode;
+
+ if (rest.every((texts) => texts.length === first.length && texts.every((text) => first.includes(text)))) {
+ return { texts: first, readOnly: false };
+ }
+
+ // In the order they are first met, so that the tags of the node selected first lead.
+ return { texts: [ ...new Set(perNode.flat()) ], readOnly: true };
+}
+
/** 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;
@@ -149,9 +172,9 @@ function toPickerValue(value: string | null | typeof MIXED) {
};
}
-function PanelSection({ label, children }: { label: string, children: ComponentChildren }) {
+function PanelSection({ label, title, children }: { label: string, title?: string, children: ComponentChildren }) {
return (
- <div className="mind-map-node-panel-section">
+ <div className="mind-map-node-panel-section" title={title}>
<div className="mind-map-node-panel-section-label">{label}</div>
{children}
</div>