Skip to content

Build your personal knowledge base with Trilium Notes

feat(mind-map): fit each palette on one row, tint backgrounds instead

Elian Doran64cbf2880882
diff --git a/apps/client/src/widgets/react/ColorPicker.spec.tsx b/apps/client/src/widgets/react/ColorPicker.spec.tsx
index 677fd0ab78..6952c20a78 100644
--- a/apps/client/src/widgets/react/ColorPicker.spec.tsx
+++ b/apps/client/src/widgets/react/ColorPicker.spec.tsx
@@ -101,6 +101,23 @@ describe("ColorPicker", () => {
expect(customCell(container)?.classList.contains("selected")).toBe(false);
});
+ it("keeps the alpha channel when matching translucent presets", () => {
+ // A palette may be translucent (the mind map tints node backgrounds this way), so the
+ // alpha must survive normalisation — otherwise every entry collapses onto its opaque hue.
+ const presets = ["#e64d4d40", "#4d99e640"];
+ const container = renderInto(<ColorPicker currentValue="RGBA(230, 77, 77, 0.251)" onChange={vi.fn()} presets={presets} />);
+
+ const selected = presetCells(container).filter((cell) => cell.classList.contains("selected"));
+ expect(selected).toHaveLength(1);
+ expect(selected[0].getAttribute("style")).toContain(presets[0]);
+ expect(customCell(container)?.classList.contains("selected")).toBe(false);
+
+ // The opaque hue is a different colour, and belongs to no preset in this palette.
+ const opaque = renderInto(<ColorPicker currentValue="#e64d4d" onChange={vi.fn()} presets={presets} />);
+ expect(presetCells(opaque).some((cell) => cell.classList.contains("selected"))).toBe(false);
+ expect(customCell(opaque)?.classList.contains("selected")).toBe(true);
+ });
+
it("marks the custom cell selected for a colour outside the palette", () => {
const container = renderInto(<ColorPicker currentValue="#123456" onChange={vi.fn()} />);
diff --git a/apps/client/src/widgets/react/ColorPicker.tsx b/apps/client/src/widgets/react/ColorPicker.tsx
index 5a7d9b8dcd..4c1740d202 100644
--- a/apps/client/src/widgets/react/ColorPicker.tsx
+++ b/apps/client/src/widgets/react/ColorPicker.tsx
@@ -46,7 +46,7 @@ export interface ColorPickerProps {
* variant that reads and writes the `color` label, see `NoteColorPicker`.
*/
export default function ColorPicker({ currentValue, onChange, presets = DEFAULT_COLOR_PALETTE, indeterminate, disabled, className, tooltips }: ColorPickerProps) {
- const normalizedValue = currentValue ? tryParseColor(currentValue)?.hex().toLowerCase() ?? null : null;
+ const normalizedValue = normalizeColor(currentValue);
const isCustomColor = !indeterminate && normalizedValue !== null && presets.indexOf(normalizedValue) === -1;
return <div className={clsx("color-picker", className)}>
@@ -163,6 +163,19 @@ function CustomColorCell(props: ColorCellProps) {
</div>;
}
+/**
+ * Brings a color into the notation the presets are written in, so that the two can be compared.
+ * The alpha channel is kept when there is one, letting callers offer translucent presets.
+ */
+function normalizeColor(color: string | null) {
+ if (!color) return null;
+
+ const parsed = tryParseColor(color);
+ if (!parsed) return null;
+
+ return (parsed.alpha() < 1 ? parsed.hexa() : parsed.hex()).toLowerCase();
+}
+
function getForegroundColor(backgroundColor: string | null) {
if (backgroundColor === null) return "inherit";
diff --git a/apps/client/src/widgets/type_widgets/MindMapNodePanel.css b/apps/client/src/widgets/type_widgets/MindMapNodePanel.css
index ccb12dac47..247526a44a 100644
--- a/apps/client/src/widgets/type_widgets/MindMapNodePanel.css
+++ b/apps/client/src/widgets/type_widgets/MindMapNodePanel.css
@@ -33,8 +33,8 @@
opacity: .75;
}
-/* The palettes are wider than the panel, so they wrap instead of spreading across a single row. */
+/* 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;
- justify-content: flex-start;
+ row-gap: 8px;
}
diff --git a/apps/client/src/widgets/type_widgets/MindMapNodePanel.spec.tsx b/apps/client/src/widgets/type_widgets/MindMapNodePanel.spec.tsx
index 4ddb6ea31f..c4910dbe12 100644
--- a/apps/client/src/widgets/type_widgets/MindMapNodePanel.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/MindMapNodePanel.spec.tsx
@@ -2,23 +2,19 @@ import type { MindElixirInstance, NodeObj } from "mind-elixir";
import { describe, expect, it, vi } from "vitest";
import { renderInto } from "../../test/render";
-import { DEFAULT_COLOR_PALETTE } from "../react/ColorPicker";
-import MindMapNodePanel, { getCommonValue, MIXED } from "./MindMapNodePanel";
-
-const THEME_PALETTE = ["#dd7878", "#ea76cb", "#8839ef"];
+import MindMapNodePanel, { getCommonValue, MIXED, NODE_BACKGROUND_COLORS, NODE_COLORS } from "./MindMapNodePanel";
function buildNode(node: Partial<NodeObj> = {}): NodeObj {
return { id: "n1", topic: "Node", ...node };
}
/**
- * A stand-in for the Mind Elixir instance exposing only what the panel touches: the theme palette,
- * the live selection and `reshapeNode`.
+ * A stand-in for the Mind Elixir instance exposing only what the panel touches: the live selection
+ * and `reshapeNode`.
*/
function buildMind(nodes: NodeObj[]) {
const reshapeNode = vi.fn();
const mind = {
- theme: { palette: THEME_PALETTE },
currentNodes: nodes.map((nodeObj) => ({ nodeObj })),
reshapeNode
} as unknown as MindElixirInstance;
@@ -70,21 +66,24 @@ describe("getCommonValue", () => {
});
describe("MindMapNodePanel", () => {
- it("offers the theme palette for branches and the default one for text and background", () => {
+ it("offers every row on a single line, backgrounds as translucent variants of the same hues", () => {
const nodes = [buildNode()];
const { mind } = buildMind(nodes);
const container = renderInto(<MindMapNodePanel mind={mind} nodes={nodes} />);
- expect(presetCells(section(container, 0))).toHaveLength(DEFAULT_COLOR_PALETTE.length);
- expect(presetCells(section(container, 1))).toHaveLength(DEFAULT_COLOR_PALETTE.length);
- expect(presetCells(section(container, 2))).toHaveLength(THEME_PALETTE.length);
+ for (const index of [0, 1, 2]) {
+ // 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);
+ }
+ expect(NODE_BACKGROUND_COLORS).toEqual(NODE_COLORS.map((color) => `${color}40`));
});
it("shows the colors the selection already carries", () => {
const nodes = [buildNode({
- style: { color: DEFAULT_COLOR_PALETTE[1], background: DEFAULT_COLOR_PALETTE[2] },
- branchColor: THEME_PALETTE[0]
+ style: { color: NODE_COLORS[1], background: NODE_BACKGROUND_COLORS[2] },
+ branchColor: NODE_COLORS[0]
})];
const { mind } = buildMind(nodes);
@@ -93,14 +92,15 @@ describe("MindMapNodePanel", () => {
const selectedIn = (index: number) => presetCells(section(container, index))
.findIndex((cell) => cell.classList.contains("selected"));
expect(selectedIn(0)).toBe(1);
+ // The translucent background has to survive being matched against its swatch.
expect(selectedIn(1)).toBe(2);
expect(selectedIn(2)).toBe(0);
});
it("selects nothing for a property the selected nodes disagree on", () => {
const nodes = [
- buildNode({ id: "a", style: { color: DEFAULT_COLOR_PALETTE[0] } }),
- buildNode({ id: "b", style: { color: DEFAULT_COLOR_PALETTE[1] } })
+ buildNode({ id: "a", style: { color: NODE_COLORS[0] } }),
+ buildNode({ id: "b", style: { color: NODE_COLORS[1] } })
];
const { mind } = buildMind(nodes);
@@ -121,8 +121,12 @@ describe("MindMapNodePanel", () => {
presetCells(section(container, 0))[3].click();
expect(reshapeNode).toHaveBeenCalledTimes(2);
- expect(reshapeNode).toHaveBeenNthCalledWith(1, mind.currentNodes[0], { style: { color: DEFAULT_COLOR_PALETTE[3] } });
- expect(reshapeNode).toHaveBeenNthCalledWith(2, mind.currentNodes[1], { style: { color: DEFAULT_COLOR_PALETTE[3] } });
+ 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();
+ expect(reshapeNode).toHaveBeenNthCalledWith(1, mind.currentNodes[0], { style: { background: NODE_BACKGROUND_COLORS[3] } });
reshapeNode.mockClear();
section(container, 1).querySelector<HTMLElement>(".color-cell-reset")?.click();
@@ -130,7 +134,7 @@ describe("MindMapNodePanel", () => {
reshapeNode.mockClear();
presetCells(section(container, 2))[1].click();
- expect(reshapeNode).toHaveBeenNthCalledWith(1, mind.currentNodes[0], { branchColor: THEME_PALETTE[1] });
+ expect(reshapeNode).toHaveBeenNthCalledWith(1, mind.currentNodes[0], { branchColor: NODE_COLORS[1] });
});
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 9d12daeccc..57a08436bd 100644
--- a/apps/client/src/widgets/type_widgets/MindMapNodePanel.tsx
+++ b/apps/client/src/widgets/type_widgets/MindMapNodePanel.tsx
@@ -4,7 +4,20 @@ import type { MindElixirInstance, NodeObj } from "mind-elixir";
import { ComponentChildren } from "preact";
import { t } from "../../services/i18n";
-import ColorPicker from "../react/ColorPicker";
+import ColorPicker, { DEFAULT_COLOR_PALETTE } from "../react/ColorPicker";
+
+/**
+ * The hues offered by the panel: as many of the shared palette as fit on a single row next to the
+ * clear and custom cells.
+ */
+export const NODE_COLORS = [0, 1, 2, 4, 7, 9].map((index) => DEFAULT_COLOR_PALETTE[index]);
+
+/**
+ * Backgrounds are the same hues at a quarter opacity. That keeps a node readable when its text and
+ * its background are set to "the same" color, and lets a single set of swatches sit well on both
+ * the light and the dark map theme, since the tint takes on whatever the canvas is.
+ */
+export const NODE_BACKGROUND_COLORS = NODE_COLORS.map((color) => `${color}40`);
interface MindMapNodePanelProps {
mind: MindElixirInstance;
@@ -40,6 +53,7 @@ export default function MindMapNodePanel({ mind, nodes }: MindMapNodePanelProps)
>
<PanelSection label={t("mind-map.text-color")}>
<ColorPicker
+ presets={NODE_COLORS}
{...toPickerValue(textColor)}
// Mind Elixir only ever assigns the style properties it is given and never
// resets the ones it isn't, so clearing has to blank the property explicitly.
@@ -49,6 +63,7 @@ export default function MindMapNodePanel({ mind, nodes }: MindMapNodePanelProps)
<PanelSection label={t("mind-map.background-color")}>
<ColorPicker
+ presets={NODE_BACKGROUND_COLORS}
{...toPickerValue(backgroundColor)}
onChange={(color) => patchSelectedNodes({ style: { background: color ?? "" } })}
/>
@@ -56,9 +71,7 @@ export default function MindMapNodePanel({ mind, nodes }: MindMapNodePanelProps)
<PanelSection label={t("mind-map.branch-color")}>
<ColorPicker
- // The branches of a map are drawn from the theme's own palette, so those are
- // the colors offered here.
- presets={mind.theme.palette}
+ presets={NODE_COLORS}
{...toPickerValue(branchColor)}
onChange={(color) => patchSelectedNodes({ branchColor: color ?? "" })}
/>