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 font size selector to the node panel
Elian Doran721fcf9cfe8c
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 1b50a6e7d7..e2c6c4f6e3 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -3525,7 +3525,12 @@"summary": "Summary","text-color": "Text","background-color": "Background",- "branch-color": "Branch"+ "branch-color": "Branch",+ "font-size": "Size",+ "font-size-small": "S",+ "font-size-medium": "M",+ "font-size-large": "L",+ "font-size-extra-large": "XL"},"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.css b/apps/client/src/widgets/type_widgets/MindMapNodePanel.cssindex 247526a44a..0cfde51571 100644--- a/apps/client/src/widgets/type_widgets/MindMapNodePanel.css+++ b/apps/client/src/widgets/type_widgets/MindMapNodePanel.css@@ -33,6 +33,15 @@opacity: .75;}+/* The sizes share the width of the panel evenly, rather than each taking its label's width. */+.mind-map-node-panel .btn-group {+ width: 100%;++ .btn {+ flex: 1;+ }+}+/* Each palette is meant to fit on one line; wrapping is only a safety net for larger cells. */.mind-map-node-panel .color-picker {flex-wrap: wrap;diff --git a/apps/client/src/widgets/type_widgets/MindMapNodePanel.spec.tsx b/apps/client/src/widgets/type_widgets/MindMapNodePanel.spec.tsxindex c4910dbe12..b36ae031e2 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, { getCommonValue, MIXED, NODE_BACKGROUND_COLORS, NODE_COLORS } from "./MindMapNodePanel";+import MindMapNodePanel, { DEFAULT_FONT_SIZE, getCommonValue, MIXED, NODE_BACKGROUND_COLORS, NODE_COLORS } from "./MindMapNodePanel";function buildNode(node: Partial<NodeObj> = {}): NodeObj {return { id: "n1", topic: "Node", ...node };@@ -21,11 +21,25 @@ function buildMind(nodes: NodeObj[]) {return { mind, reshapeNode };}+/** The order the panel lays its sections out in. */+const SIZE = 0;+const TEXT = 1;+const BACKGROUND = 2;+const BRANCH = 3;+function section(container: HTMLElement, index: number) {const sections = container.querySelectorAll<HTMLElement>(".mind-map-node-panel-section");return sections[index];}+function sizeButtons(container: HTMLElement) {+ return Array.from(section(container, SIZE).querySelectorAll<HTMLElement>(".btn"));+}++function activeSize(container: HTMLElement) {+ return sizeButtons(container).findIndex((button) => button.classList.contains("active"));+}+function presetCells(root: HTMLElement) {return Array.from(root.querySelectorAll<HTMLElement>(".color-cell")).filter((cell) =>!cell.classList.contains("color-cell-reset") && !cell.classList.contains("custom-color-cell"));@@ -72,7 +86,7 @@ describe("MindMapNodePanel", () => {const container = renderInto(<MindMapNodePanel mind={mind} nodes={nodes} />);- for (const index of [0, 1, 2]) {+ for (const index of [TEXT, BACKGROUND, BRANCH]) {// A row is the presets plus the clear and custom cells; more would wrap in the panel.expect(presetCells(section(container, index))).toHaveLength(NODE_COLORS.length);expect(section(container, index).querySelectorAll(".color-cell")).toHaveLength(NODE_COLORS.length + 2);@@ -80,6 +94,37 @@ describe("MindMapNodePanel", () => {expect(NODE_BACKGROUND_COLORS).toEqual(NODE_COLORS.map((color) => `${color}40`));});+ it("shows a node with no size of its own as medium, and applies the sizes it is asked for", () => {+ const nodes = [buildNode({ id: "a" }), buildNode({ id: "b" })];+ const { mind, reshapeNode } = buildMind(nodes);++ const container = renderInto(<MindMapNodePanel mind={mind} nodes={nodes} />);++ expect(sizeButtons(container)).toHaveLength(4);+ expect(activeSize(container)).toBe(1);++ sizeButtons(container)[2].click();+ expect(reshapeNode).toHaveBeenNthCalledWith(1, mind.currentNodes[0], { style: { fontSize: "24px" } });+ expect(reshapeNode).toHaveBeenNthCalledWith(2, mind.currentNodes[1], { style: { fontSize: "24px" } });++ // Back to medium means back to having no size of its own, rather than the size of an+ // ordinary node — the root is larger than that.+ reshapeNode.mockClear();+ sizeButtons(container)[1].click();+ expect(reshapeNode).toHaveBeenNthCalledWith(1, mind.currentNodes[0], { style: { fontSize: DEFAULT_FONT_SIZE } });+ expect(DEFAULT_FONT_SIZE).toBe("");+ });++ it("shows the size the selection already carries, and none when the nodes disagree", () => {+ const large = { style: { fontSize: "24px" } };+ const { mind } = buildMind([buildNode(large)]);+ expect(activeSize(renderInto(<MindMapNodePanel mind={mind} nodes={[buildNode(large)]} />))).toBe(2);++ const mixed = [buildNode({ id: "a", ...large }), buildNode({ id: "b", style: { fontSize: "32px" } })];+ const mixedMind = buildMind(mixed);+ expect(activeSize(renderInto(<MindMapNodePanel mind={mixedMind.mind} nodes={mixed} />))).toBe(-1);+ });+it("shows the colors the selection already carries", () => {const nodes = [buildNode({style: { color: NODE_COLORS[1], background: NODE_BACKGROUND_COLORS[2] },@@ -91,10 +136,10 @@ describe("MindMapNodePanel", () => {const selectedIn = (index: number) => presetCells(section(container, index)).findIndex((cell) => cell.classList.contains("selected"));- expect(selectedIn(0)).toBe(1);+ expect(selectedIn(TEXT)).toBe(1);// The translucent background has to survive being matched against its swatch.- expect(selectedIn(1)).toBe(2);- expect(selectedIn(2)).toBe(0);+ expect(selectedIn(BACKGROUND)).toBe(2);+ expect(selectedIn(BRANCH)).toBe(0);});it("selects nothing for a property the selected nodes disagree on", () => {@@ -106,10 +151,10 @@ describe("MindMapNodePanel", () => {const container = renderInto(<MindMapNodePanel mind={mind} nodes={nodes} />);- const textCells = Array.from(section(container, 0).querySelectorAll(".color-cell"));+ const textCells = Array.from(section(container, TEXT).querySelectorAll(".color-cell"));expect(textCells.some((cell) => cell.classList.contains("selected"))).toBe(false);// The properties they do agree on are still shown as unset.- const backgroundReset = section(container, 1).querySelector(".color-cell-reset");+ const backgroundReset = section(container, BACKGROUND).querySelector(".color-cell-reset");expect(backgroundReset?.classList.contains("selected")).toBe(true);});@@ -119,21 +164,21 @@ describe("MindMapNodePanel", () => {const container = renderInto(<MindMapNodePanel mind={mind} nodes={nodes} />);- presetCells(section(container, 0))[3].click();+ presetCells(section(container, TEXT))[3].click();expect(reshapeNode).toHaveBeenCalledTimes(2);expect(reshapeNode).toHaveBeenNthCalledWith(1, mind.currentNodes[0], { style: { color: NODE_COLORS[3] } });expect(reshapeNode).toHaveBeenNthCalledWith(2, mind.currentNodes[1], { style: { color: NODE_COLORS[3] } });reshapeNode.mockClear();- presetCells(section(container, 1))[3].click();+ presetCells(section(container, BACKGROUND))[3].click();expect(reshapeNode).toHaveBeenNthCalledWith(1, mind.currentNodes[0], { style: { background: NODE_BACKGROUND_COLORS[3] } });reshapeNode.mockClear();- section(container, 1).querySelector<HTMLElement>(".color-cell-reset")?.click();+ section(container, BACKGROUND).querySelector<HTMLElement>(".color-cell-reset")?.click();expect(reshapeNode).toHaveBeenNthCalledWith(1, mind.currentNodes[0], { style: { background: "" } });reshapeNode.mockClear();- presetCells(section(container, 2))[1].click();+ presetCells(section(container, BRANCH))[1].click();expect(reshapeNode).toHaveBeenNthCalledWith(1, mind.currentNodes[0], { branchColor: NODE_COLORS[1] });});diff --git a/apps/client/src/widgets/type_widgets/MindMapNodePanel.tsx b/apps/client/src/widgets/type_widgets/MindMapNodePanel.tsxindex 57a08436bd..8e2bbfe7e7 100644--- a/apps/client/src/widgets/type_widgets/MindMapNodePanel.tsx+++ b/apps/client/src/widgets/type_widgets/MindMapNodePanel.tsx@@ -5,6 +5,7 @@ import { ComponentChildren } from "preact";import { t } from "../../services/i18n";import ColorPicker, { DEFAULT_COLOR_PALETTE } from "../react/ColorPicker";+import SegmentedChoice from "../react/SegmentedChoice";/*** The hues offered by the panel: as many of the shared palette as fit on a single row next to the@@ -30,6 +31,7 @@ interface MindMapNodePanelProps {* formatting controls for the selection.*/export default function MindMapNodePanel({ mind, nodes }: MindMapNodePanelProps) {+ const fontSize = getCommonValue(nodes, (node) => node.style?.fontSize);const textColor = getCommonValue(nodes, (node) => node.style?.color);const backgroundColor = getCommonValue(nodes, (node) => node.style?.background);const branchColor = getCommonValue(nodes, (node) => node.branchColor);@@ -51,6 +53,14 @@ export default function MindMapNodePanel({ mind, nodes }: MindMapNodePanelProps)onMouseDown={(e) => e.stopPropagation()}onKeyDown={(e) => e.stopPropagation()}>+ <PanelSection label={t("mind-map.font-size")}>+ <SegmentedChoice+ options={buildFontSizeOptions()}+ currentValue={fontSize !== MIXED ? fontSize ?? DEFAULT_FONT_SIZE : MIXED_FONT_SIZE}+ onChange={(fontSize) => patchSelectedNodes({ style: { fontSize } })}+ />+ </PanelSection>+<PanelSection label={t("mind-map.text-color")}><ColorPickerpresets={NODE_COLORS}@@ -80,6 +90,26 @@ export default function MindMapNodePanel({ mind, nodes }: MindMapNodePanelProps));}+/**+ * A node of medium size is one with no size of its own: that is what a node comes with, and it+ * leaves the root, which is larger by default, at the size its level implies rather than pinning+ * it to the size of an ordinary node.+ */+export const DEFAULT_FONT_SIZE = "";++/** Matches none of the sizes, for a selection whose nodes are not all of the same size. */+const MIXED_FONT_SIZE = "mixed";++/** The sizes a node can be given, after the ones the canvas note type offers. */+function buildFontSizeOptions() {+ return [+ { value: "12px", label: t("mind-map.font-size-small") },+ { value: DEFAULT_FONT_SIZE, label: t("mind-map.font-size-medium") },+ { value: "24px", label: t("mind-map.font-size-large") },+ { value: "32px", label: t("mind-map.font-size-extra-large") }+ ];+}+/** Turns the outcome of {@link getCommonValue} into the matching {@link ColorPicker} state. */function toPickerValue(value: string | null | typeof MIXED) {return {