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): let a node wear several icons
Elian Doran818d2cbfb296
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 938fe7fca7..8309b91191 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -3531,9 +3531,11 @@"font-size-medium": "M","font-size-large": "L","font-size-extra-large": "XL",- "icon": "Icon",- "choose-icon": "Choose an icon",+ "icons": "Icons",+ "add-icon": "Add an icon",+ "change-icon": "Change this icon","clear-icon": "Remove the icon",+ "icons-differ": "The selected nodes don't carry the same icons, so these can only be read.","node-link": "Link","add-link": "Add a link","choose-link": "Link this node to a note or a page",diff --git a/apps/client/src/widgets/type_widgets/MindMapNodePanel.css b/apps/client/src/widgets/type_widgets/MindMapNodePanel.cssindex 599b17c97d..97a78e7e7d 100644--- a/apps/client/src/widgets/type_widgets/MindMapNodePanel.css+++ b/apps/client/src/widgets/type_widgets/MindMapNodePanel.css@@ -33,7 +33,15 @@opacity: .75;}-/* The icon stands at the size it will read at on a node, rather than the size a note wears. */+/* The icons of a node stand side by side, the button adding one more after them. */+.mind-map-node-panel .mind-map-node-icons {+ display: flex;+ flex-wrap: wrap;+ align-items: center;+ gap: 2px;+}++/* Each stands at the size it will read at on a node, rather than the size a note wears. */.mind-map-node-panel .mind-map-node-icon {--note-icon-size: 20px;--note-icon-container-padding-size: 4px;diff --git a/apps/client/src/widgets/type_widgets/MindMapNodePanel.spec.tsx b/apps/client/src/widgets/type_widgets/MindMapNodePanel.spec.tsxindex bab92f289b..effe6d8cb5 100644--- a/apps/client/src/widgets/type_widgets/MindMapNodePanel.spec.tsx+++ b/apps/client/src/widgets/type_widgets/MindMapNodePanel.spec.tsx@@ -3,7 +3,7 @@ import { describe, expect, it, vi } from "vitest";import { buildNotes } from "../../test/easy-froca";import { renderInto } from "../../test/render";-import MindMapNodePanel, { applyTagTexts, DEFAULT_FONT_SIZE, gatherTags, getCommonValue, MIXED, NODE_BACKGROUND_COLORS, NODE_COLORS } from "./MindMapNodePanel";+import MindMapNodePanel, { applyTagTexts, DEFAULT_FONT_SIZE, gatherTags, getCommonValue, MIXED, NODE_BACKGROUND_COLORS, NODE_COLORS, withIconAt } from "./MindMapNodePanel";function buildNode(node: Partial<NodeObj> = {}): NodeObj {return { id: "n1", topic: "Node", ...node };@@ -48,6 +48,16 @@ function typeTag(container: HTMLElement, text: string) {input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true }));}+function iconButtons(container: HTMLElement) {+ return [ ...section(container, ICON).querySelectorAll<HTMLButtonElement>(".mind-map-node-icon button") ];+}++/** The icon each button in the row wears, the adding button included. */+function iconFaces(container: HTMLElement) {+ return iconButtons(container).map((button) =>+ [ ...button.classList ].filter((name) => name.startsWith("bx")).join(" "));+}+function sizeButtons(container: HTMLElement) {return Array.from(section(container, SIZE).querySelectorAll<HTMLElement>(".btn"));}@@ -118,6 +128,24 @@ describe("gatherTags", () => {});});+describe("withIconAt", () => {+ it("takes an icon in at a place, and drops the one that was there", () => {+ const icons = [ "bx bx-star", "bx bx-cube" ];++ // The button at the end of the row stands one past it, which is where a pick is appended.+ expect(withIconAt(icons, icons.length, "bx bx-bulb")).toEqual([ "bx bx-star", "bx bx-cube", "bx bx-bulb" ]);+ expect(withIconAt([], 0, "bx bx-bulb")).toEqual([ "bx bx-bulb" ]);+ // Picking on an icon takes its place, wherever in the row it stands.+ expect(withIconAt(icons, 0, "bx bx-bulb")).toEqual([ "bx bx-bulb", "bx bx-cube" ]);+ expect(withIconAt(icons, 1, "bx bx-bulb")).toEqual([ "bx bx-star", "bx bx-bulb" ]);+ // Removing takes out that one alone, the same icon worn twice included.+ expect(withIconAt(icons, 0, null)).toEqual([ "bx bx-cube" ]);+ expect(withIconAt([ "bx bx-star", "bx bx-star" ], 1, null)).toEqual([ "bx bx-star" ]);+ // Nothing of the original is touched along the way.+ expect(icons).toEqual([ "bx bx-star", "bx bx-cube" ]);+ });+});+describe("applyTagTexts", () => {it("keeps a styled tag while its text stands, and plain text for the rest", () => {const styled = { text: "urgent", style: { color: "red" } };@@ -233,18 +261,29 @@ describe("MindMapNodePanel", () => {expect(reshapeNode).toHaveBeenNthCalledWith(1, mind.currentNodes[0], { branchColor: NODE_COLORS[1] });});- it("shows the icon the selection wears, and none where they disagree", () => {- const iconButton = (nodes: NodeObj[]) =>- renderInto(<MindMapNodePanel mind={buildMind(nodes).mind} nodes={nodes} />)- .querySelector(`.mind-map-node-panel-section:nth-child(${ICON + 1}) button`);+ it("shows every icon the selection wears, and a button for one more", () => {+ const nodes = [buildNode({ icons: ["bx bx-star", "bx bx-cube"] })];- expect(iconButton([buildNode({ icons: ["bx bx-star"] })])?.className).toContain("bx bx-star");- // Nothing of its own, and a selection that disagrees, both read as no icon.- expect(iconButton([buildNode()])?.className).toContain("bx bx-empty");- expect(iconButton([+ const container = renderInto(<MindMapNodePanel mind={buildMind(nodes).mind} nodes={nodes} />);++ // The icons themselves, then the button adding another.+ expect(iconFaces(container)).toEqual(["bx bx-star", "bx bx-cube", "bx bx-plus"]);+ // A node wearing none is the adding button alone, which is invitation enough.+ expect(iconFaces(renderInto(<MindMapNodePanel mind={buildMind([buildNode()]).mind} nodes={[buildNode()]} />)))+ .toEqual(["bx bx-plus"]);+ });++ it("shows what a disagreeing selection carries between them, for reading only", () => {+ const nodes = [buildNode({ id: "a", icons: ["bx bx-star"] }),buildNode({ id: "b", icons: ["bx bx-cube"] })- ])?.className).toContain("bx bx-empty");+ ];++ const container = renderInto(<MindMapNodePanel mind={buildMind(nodes).mind} nodes={nodes} />);++ // Everything they carry, and no way to add: a pick could only overwrite what each has.+ expect(iconFaces(container)).toEqual(["bx bx-star", "bx bx-cube"]);+ expect(iconButtons(container).every((button) => button.disabled)).toBe(true);});it("says what the selection is linked to, and what it would take to link it", async () => {diff --git a/apps/client/src/widgets/type_widgets/MindMapNodePanel.tsx b/apps/client/src/widgets/type_widgets/MindMapNodePanel.tsxindex 6778a62ab7..39c3e1cc89 100644--- a/apps/client/src/widgets/type_widgets/MindMapNodePanel.tsx+++ b/apps/client/src/widgets/type_widgets/MindMapNodePanel.tsx@@ -48,7 +48,7 @@ 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);- const icon = getCommonValue(nodes, (node) => node.icons?.[0]);+ const icons = gatherListValues(nodes, (node) => node.icons ?? []);const link = getCommonValue(nodes, (node) => node.hyperLink);const tags = gatherTags(nodes);@@ -104,13 +104,14 @@ export default function MindMapNodePanel({ mind, nodes }: MindMapNodePanelProps)/></PanelSection>- <PanelSection label={t("mind-map.icon")}>- <NodeIcon- currentValue={icon !== MIXED ? icon : null}- // A node takes one icon, like a note does, so what is picked replaces what was- // there rather than joining it.- onSelect={(iconClass) => patchSelectedNodes({ icons: [ iconClass ] })}- onClear={() => patchSelectedNodes({ icons: [] })}+ <PanelSection+ label={t("mind-map.icons")}+ title={icons.readOnly ? t("mind-map.icons-differ") : undefined}+ >+ <NodeIcons+ icons={icons.values}+ readOnly={icons.readOnly}+ onChange={(icons) => patchSelectedNodes({ icons })}/></PanelSection>@@ -169,23 +170,44 @@ function buildFontSizeOptions() {}/**- * What the tag field stands for, given the selection.+ * What a field standing for a property a node holds several of — its tags, its icons — shows for+ * 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.+ * Such a property belongs 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 one as read would mean writing one node's list over another's.*/-export function gatherTags(nodes: NodeObj[]): { texts: string[], readOnly: boolean } {- const perNode = nodes.map((node) => (node.tags ?? []).map(getTagText));+export function gatherListValues(nodes: NodeObj[], read: (node: NodeObj) => string[]): { values: string[], readOnly: boolean } {+ const perNode = nodes.map(read);const [ first = [], ...rest ] = perNode;- if (rest.every((texts) => texts.length === first.length && texts.every((text) => first.includes(text)))) {- return { texts: first, readOnly: false };+ if (rest.every((values) => values.length === first.length && values.every((value) => first.includes(value)))) {+ return { values: 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 };+ // In the order they are first met, so that what the node selected first carries leads.+ return { values: [ ...new Set(perNode.flat()) ], readOnly: true };+}++/**+ * The icons a node wears once the one at `index` becomes `iconClass`, or is dropped where none is+ * given. An index past the end appends, which is what the button at the end of the row stands at.+ */+export function withIconAt(icons: string[], index: number, iconClass: string | null): string[] {+ if (iconClass === null) {+ return icons.filter((_, at) => at !== index);+ }+ if (index >= icons.length) {+ return [ ...icons, iconClass ];+ }+ return icons.map((held, at) => (at === index ? iconClass : held));+}++/** The tags of the selection, by the rule above. */+export function gatherTags(nodes: NodeObj[]): { texts: string[], readOnly: boolean } {+ const { values, readOnly } = gatherListValues(nodes, (node) => (node.tags ?? []).map(getTagText));+ return { texts: values, readOnly };}/** The text a tag reads as, whether it carries a style of its own or is only the text. */@@ -211,15 +233,60 @@ function toPickerValue(value: string | null | typeof MIXED) {}/**- * The icon a node wears, chosen through the picker every other icon in Trilium is chosen through.+ * The icons a node wears, side by side, and a button adding one more.+ *+ * Each icon is its own picker: opening it swaps that icon for whichever is chosen next, and the+ * picker's own way back — the button offering to remove it — takes it out of the row. Nothing else+ * is needed to drop one, and with no icons at all the row is the adding button alone, which says+ * plainly enough what it is for.+ */+function NodeIcons({ icons, readOnly, onChange }: {+ icons: string[];+ readOnly: boolean;+ onChange(icons: string[]): void;+}) {+ return (+ <div className="mind-map-node-icons">+ {icons.map((iconClass, index) => (+ <NodeIcon+ // By position: a node may wear the same icon twice over, and the row is built+ // from what it holds rather than from a set.+ key={index}+ face={iconClass}+ title={t("mind-map.change-icon")}+ disabled={readOnly}+ onSelect={(picked) => onChange(withIconAt(icons, index, picked))}+ onRemove={() => onChange(withIconAt(icons, index, null))}+ />+ ))}++ {!readOnly && (+ // Standing one past the end, which is where what it takes is put.+ <NodeIcon+ face="bx bx-plus"+ title={t("mind-map.add-icon")}+ onSelect={(picked) => onChange(withIconAt(icons, icons.length, picked))}+ />+ )}+ </div>+ );+}++/**+ * One icon of a node, chosen through the picker every other icon in Trilium is chosen through.** The picker is only built once opened: it holds every icon of every installed pack, which is far* more work than a panel that merely happens to be on screen should be doing.*/-function NodeIcon({ currentValue, onSelect, onClear }: {- currentValue: string | null;+function NodeIcon({ face, title, disabled, onSelect, onRemove }: {+ /** The class the button wears, whether an icon of the node or the invitation to add one. */+ face: string;+ title: string;+ disabled?: boolean;onSelect(iconClass: string): void;- onClear(): void;+ /** Offered inside the picker. Left out — as it is on the adding button — there is nothing yet+ * to remove. */+ onRemove?(): void;}) {const dropdownRef = useRef<BootstrapDropdown>(null);const [ pickerShown, setPickerShown ] = useState(false);@@ -229,8 +296,9 @@ function NodeIcon({ currentValue, onSelect, onClear }: {// The legacy class dresses the menu the picker sits in, which the themes and the// picker's own stylesheet reach through it.className="mind-map-node-icon note-icon-widget"- buttonClassName={`note-icon tn-focusable-button ${currentValue ?? "bx bx-empty"}`}- title={t("mind-map.choose-icon")}+ buttonClassName={`note-icon tn-focusable-button ${face}`}+ title={title}+ disabled={disabled}dropdownRef={dropdownRef}dropdownContainerStyle={{ width: "620px" }}dropdownOptions={{ autoClose: "outside" }}@@ -249,10 +317,10 @@ function NodeIcon({ currentValue, onSelect, onClear }: {onSelect(iconClass);dropdownRef.current?.hide();}}- onReset={currentValue ? () => {- onClear();+ onReset={onRemove && (() => {+ onRemove();dropdownRef.current?.hide();- } : undefined}+ })}/>)}</Dropdown>