Skip to content

Build your personal knowledge base with Trilium Notes

feat(mind-map): give a node's memo a tab of its own

Elian Doran455504959a8c
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index fdb4aa9701..b262001f49 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -3552,6 +3552,7 @@
"clear-link": "Remove the link",
"link-mixed": "Different links",
"links-differ": "The selected nodes don't carry the same link.",
+ "tab-format": "Format",
"memo": "Memo",
"memos-differ": "The selected nodes carry different memos, so this one can only be read.",
"tags": "Tags",
diff --git a/apps/client/src/widgets/type_widgets/mind_map/NodePanel.css b/apps/client/src/widgets/type_widgets/mind_map/NodePanel.css
index 2d399dbfd9..2927fc199f 100644
--- a/apps/client/src/widgets/type_widgets/mind_map/NodePanel.css
+++ b/apps/client/src/widgets/type_widgets/mind_map/NodePanel.css
@@ -5,9 +5,11 @@
inset-inline-end: 20px;
width: 240px;
max-height: calc(100% - 100px);
- overflow: auto;
+ /* The tab strip is the panel's header and stays put; only the tab under it scrolls. */
+ display: flex;
+ flex-direction: column;
+ overflow: hidden;
box-sizing: border-box;
- padding: 15px;
background: var(--menu-background-color);
color: var(--menu-text-color);
border: 1px solid var(--dropdown-border-color);
@@ -20,6 +22,50 @@
top: calc(20px + env(safe-area-inset-top));
}
+/*
+ * The strip parts from the tab under it by a line the width of the panel, so that it reads as the
+ * panel's header rather than as one more choice among the fields — which is what the same button
+ * group means everywhere else in here (the font sizes, the picture's shapes).
+ */
+.mind-map-node-panel .mind-map-node-panel-tabs {
+ display: flex;
+ justify-content: center;
+ flex-shrink: 0;
+ padding: 8px;
+ border-bottom: 1px solid var(--main-border-color);
+
+ .mind-map-node-panel-tab {
+ /* The icon leads the name, as it does on a tab of the right pane. */
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ gap: 5px;
+ }
+}
+
+/* Each tab holds the padding, rather than the panel: the memo's is its own (see below). */
+.mind-map-node-panel .mind-map-node-panel-body {
+ padding: 15px;
+ overflow: auto;
+ /* Without it a flex item refuses to shrink past its content, and the panel scrolls as a whole. */
+ min-height: 0;
+}
+
+/*
+ * The memo is the whole of its tab: it takes the height the panel can spare and grows the panel to
+ * a comfortable writing box where there is room, instead of the three lines it had as a field.
+ */
+.mind-map-node-panel .mind-map-node-panel-memo-body {
+ display: flex;
+ padding: 8px;
+
+ .mind-map-node-memo {
+ flex: 1;
+ min-height: 220px;
+ max-height: none;
+ }
+}
+
.mind-map-node-panel .mind-map-node-panel-section + .mind-map-node-panel-section {
margin-top: 16px;
}
diff --git a/apps/client/src/widgets/type_widgets/mind_map/NodePanel.spec.tsx b/apps/client/src/widgets/type_widgets/mind_map/NodePanel.spec.tsx
index a280e248b9..027799b2c4 100644
--- a/apps/client/src/widgets/type_widgets/mind_map/NodePanel.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/mind_map/NodePanel.spec.tsx
@@ -234,6 +234,30 @@ describe("the memo field of the panel", () => {
return container.querySelector(".mind-map-node-memo")?.getAttribute("data-current-value");
}
+ /**
+ * Brings a tab to the front, named by the icon it wears rather than by its title, so that the
+ * tests speak of the panel and not of the wording it happens to carry.
+ */
+ async function openTab(container: HTMLElement, icon: string) {
+ const tab = container.querySelector(`[role="tab"] .${icon}`)?.closest("button");
+ if (!tab) throw new Error(`the panel has no ${icon} tab`);
+
+ await act(async () => tab.click());
+ }
+
+ /**
+ * The panel opens on the formatting tab, and the editor is built only when the memo's own is
+ * first opened — so there is nothing to read or write into before this.
+ */
+ const openMemoTab = (container: HTMLElement) => openTab(container, "bx-notepad");
+ const openFormatTab = (container: HTMLElement) => openTab(container, "bx-palette");
+
+ /** The icon of the tab on show, which is what the panel says it is showing. */
+ function activeTabIcon(container: HTMLElement) {
+ const active = container.querySelector('[role="tab"][aria-selected="true"] .tn-icon');
+ return [ ...active?.classList ?? [] ].find((name) => name.startsWith("bx-"));
+ }
+
/** Writes into the field, as someone typing does: what it shows is what it then reports. */
async function typeMemo(html: string) {
await act(async () => {
@@ -242,6 +266,54 @@ describe("the memo field of the panel", () => {
});
}
+ it("keeps the memo in a tab of its own, raised only once it is asked for", async () => {
+ const node = buildNode({ id: "a", memo: "<p>About A</p>" } as Partial<NodeObj>);
+ let container: HTMLElement | undefined;
+
+ await act(async () => {
+ container = renderInto(<NodePanel mind={buildMind([node]).mind} noteId="map" nodes={[node]} />);
+ });
+ if (!container) throw new Error("render produced no container");
+ const panel = container;
+
+ // The panel opens on the fields, the editor not yet built — which is what keeps a map whose
+ // memos are never touched from paying for one on every selection.
+ expect(activeTabIcon(panel)).toBe("bx-palette");
+ expect(panel.querySelector(".mind-map-node-memo")).toBeNull();
+
+ await openMemoTab(panel);
+ expect(activeTabIcon(panel)).toBe("bx-notepad");
+ expect(shownMemo(panel)).toBe("<p>About A</p>");
+ // The fields are put out of sight rather than taken down, so returning to them is instant
+ // and nothing half-typed in them is lost on the way to the memo.
+ expect(section(panel, SIZE)).toBeTruthy();
+ });
+
+ it("writes a memo back from behind the other tab, the editor staying up once raised", async () => {
+ // Switching away must not take the editor down: it is what writes a memo back as the
+ // selection moves on, and someone who types one and returns to the fields would lose it.
+ const first = buildNode({ id: "a" });
+ const second = buildNode({ id: "b" });
+ const firstMind = buildMind([first]);
+ let container: HTMLElement | undefined;
+
+ await act(async () => {
+ container = renderInto(<NodePanel mind={firstMind.mind} noteId="map" nodes={[first]} />);
+ });
+ if (!container) throw new Error("render produced no container");
+ const panel = container;
+
+ await openMemoTab(panel);
+ await typeMemo("<p>About A</p>");
+ await openFormatTab(panel);
+ expect(activeTabIcon(panel)).toBe("bx-palette");
+
+ await act(async () => {
+ render(<NodePanel mind={buildMind([second]).mind} noteId="map" nodes={[second]} />, panel);
+ });
+ expect(firstMind.reshapeNode).toHaveBeenCalledWith(firstMind.mind.currentNodes[0], { memo: "<p>About A</p>" });
+ });
+
it("shows the memo of whatever is selected now, not of what was selected before", async () => {
// One field serves every selection it outlives, so the memo it shows has to follow the
// selection — otherwise the memo of the node left behind is what is read, and written over.
@@ -254,6 +326,7 @@ describe("the memo field of the panel", () => {
});
if (!container) throw new Error("render produced no container");
const panel = container;
+ await openMemoTab(panel);
expect(shownMemo(panel)).toBe("<p>About A</p>");
// Rendered into the same container, so the panel is updated rather than built anew.
@@ -281,6 +354,7 @@ describe("the memo field of the panel", () => {
});
if (!container) throw new Error("render produced no container");
const panel = container;
+ await openMemoTab(panel);
await typeMemo("<p>Typed for A</p>");
expect(shownMemoText).toBe("<p>Typed for A</p>");
@@ -302,6 +376,7 @@ describe("the memo field of the panel", () => {
});
if (!container) throw new Error("render produced no container");
const panel = container;
+ await openMemoTab(panel);
await typeMemo("<p>About A, at length</p>");
diff --git a/apps/client/src/widgets/type_widgets/mind_map/NodePanel.tsx b/apps/client/src/widgets/type_widgets/mind_map/NodePanel.tsx
index 2a45143f2a..e95c46831b 100644
--- a/apps/client/src/widgets/type_widgets/mind_map/NodePanel.tsx
+++ b/apps/client/src/widgets/type_widgets/mind_map/NodePanel.tsx
@@ -2,6 +2,7 @@ import "./NodePanel.css";
import { parseMindMapNoteLink } from "@triliumnext/commons";
import { Dropdown as BootstrapDropdown } from "bootstrap";
+import clsx from "clsx";
import type { MindElixirInstance, NodeObj, TagObj } from "mind-elixir";
import { ComponentChildren } from "preact";
import { useEffect, useRef, useState } from "preact/hooks";
@@ -62,6 +63,13 @@ export default function NodePanel({ mind, noteId, nodes }: NodePanelProps) {
const link = getCommonValue(nodes, (node) => node.hyperLink);
const tags = gatherTags(nodes);
const memo = getCommonValue(nodes, getNodeMemo);
+ const [ activeTabId, setActiveTabId ] = useState<NodePanelTabId>("format");
+
+ // The memo is built when its tab is first opened and kept mounted from then on. The editor is
+ // costly to raise, and it is also what writes a memo back as the selection moves on — which
+ // happens just as often while the other tab is the one on show, and has to keep working there.
+ const openedTabs = useRef(new Set<NodePanelTabId>());
+ openedTabs.current.add(activeTabId);
/**
* Applies a patch to every selected node. The selection is read back from the instance rather
@@ -123,115 +131,178 @@ export default function NodePanel({ mind, noteId, nodes }: NodePanelProps) {
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")}>
- <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.
- onChange={(color) => patchSelectedNodes({ style: { color: color ?? "" } })}
- />
- </PanelSection>
+ <NodePanelTabs activeTabId={activeTabId} onSelect={setActiveTabId} />
- <PanelSection label={t("mind-map.background-color")}>
- <ColorPicker
- presets={NODE_BACKGROUND_COLORS}
- {...toPickerValue(backgroundColor)}
- onChange={(color) => patchSelectedNodes({ style: { background: color ?? "" } })}
- />
- </PanelSection>
+ <div
+ role="tabpanel"
+ className={clsx("mind-map-node-panel-body", activeTabId !== "format" && "hidden-ext")}
+ >
+ <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")}>
+ <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.
+ onChange={(color) => patchSelectedNodes({ style: { color: color ?? "" } })}
+ />
+ </PanelSection>
- <PanelSection label={t("mind-map.branch-color")}>
- <ColorPicker
- presets={NODE_COLORS}
- {...toPickerValue(branchColor)}
- onChange={(color) => patchSelectedNodes({ branchColor: color ?? "" })}
- />
- </PanelSection>
+ <PanelSection label={t("mind-map.background-color")}>
+ <ColorPicker
+ presets={NODE_BACKGROUND_COLORS}
+ {...toPickerValue(backgroundColor)}
+ onChange={(color) => patchSelectedNodes({ style: { background: color ?? "" } })}
+ />
+ </PanelSection>
- <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>
+ <PanelSection label={t("mind-map.branch-color")}>
+ <ColorPicker
+ presets={NODE_COLORS}
+ {...toPickerValue(branchColor)}
+ onChange={(color) => patchSelectedNodes({ branchColor: color ?? "" })}
+ />
+ </PanelSection>
+
+ <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>
+
+ <PanelSection
+ label={t("mind-map.image")}
+ title={image === MIXED ? t("mind-map.images-differ") : undefined}
+ >
+ <NodeImage
+ hasImage={!!image}
+ indeterminate={image === MIXED}
+ width={imageWidth !== MIXED && imageWidth ? Number(imageWidth) : null}
+ shape={imageShape !== MIXED ? imageShape : null}
+ onShape={(shape) => void shapeImages(shape)}
+ // A node carries one picture, so what is taken in takes the place of what was
+ // there, at the size it comes in at.
+ onPick={(file) => void takeInImage(file)}
+ // Derived per node: the nodes are set to the same width, but each keeps the
+ // proportions of the picture it carries.
+ onResize={(width) => patchSelectedNodes((node) => ({
+ image: node.image && fitNodeImage(node.image, width)
+ }))}
+ onRemove={() => patchSelectedNodes({ image: undefined })}
+ />
+ </PanelSection>
+
+ <PanelSection
+ label={t("mind-map.node-link")}
+ title={link === MIXED ? t("mind-map.links-differ") : undefined}
+ >
+ <NodeLink
+ currentValue={link !== MIXED ? link : null}
+ indeterminate={link === MIXED}
+ // A node carries one link, so what is picked takes the place of what was there.
+ // Clearing blanks the property, Mind Elixir only ever assigning the ones it is
+ // given (see the colors above).
+ onChange={(link) => patchSelectedNodes({ hyperLink: link ?? "" })}
+ />
+ </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>
- <PanelSection
- label={t("mind-map.image")}
- title={image === MIXED ? t("mind-map.images-differ") : undefined}
- >
- <NodeImage
- hasImage={!!image}
- indeterminate={image === MIXED}
- width={imageWidth !== MIXED && imageWidth ? Number(imageWidth) : null}
- shape={imageShape !== MIXED ? imageShape : null}
- onShape={(shape) => void shapeImages(shape)}
- // A node carries one picture, so what is taken in takes the place of what was
- // there, at the size it comes in at.
- onPick={(file) => void takeInImage(file)}
- // Derived per node: the nodes are set to the same width, but each keeps the
- // proportions of the picture it carries.
- onResize={(width) => patchSelectedNodes((node) => ({
- image: node.image && fitNodeImage(node.image, width)
- }))}
- onRemove={() => patchSelectedNodes({ image: undefined })}
- />
- </PanelSection>
+ {/* The memo goes without a label of its own: the tab it fills is the label, and the
+ editor is the whole of the tab rather than a field within it. */}
+ {openedTabs.current.has("memo") && (
+ <div
+ role="tabpanel"
+ className={clsx("mind-map-node-panel-body mind-map-node-panel-memo-body", activeTabId !== "memo" && "hidden-ext")}
+ title={memo === MIXED ? t("mind-map.memos-differ") : undefined}
+ >
+ <NodeMemo
+ selectionKey={nodes.map((node) => node.id).join(" ")}
+ memo={memo !== MIXED ? memo : null}
+ readOnly={memo === MIXED}
+ onCommit={writeMemo}
+ />
+ </div>
+ )}
+ </div>
+ );
+}
- <PanelSection
- label={t("mind-map.node-link")}
- title={link === MIXED ? t("mind-map.links-differ") : undefined}
- >
- <NodeLink
- currentValue={link !== MIXED ? link : null}
- indeterminate={link === MIXED}
- // A node carries one link, so what is picked takes the place of what was there.
- // Clearing blanks the property, Mind Elixir only ever assigning the ones it is
- // given (see the colors above).
- onChange={(link) => patchSelectedNodes({ hyperLink: link ?? "" })}
- />
- </PanelSection>
+type NodePanelTabId = "format" | "memo";
- <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>
+/**
+ * The groups the panel's fields are divided into, in the order they are offered: what a node is
+ * made to look like, and what is written about it.
+ *
+ * The memo is a tab of its own rather than the last field under the others: it is written into
+ * rather than picked from, and a paragraph of prose wants the room the whole panel has — which, at
+ * the foot of eight other fields, meant scrolling to the bottom to reach a box three lines tall.
+ */
+function buildTabs(): { id: NodePanelTabId, title: string, icon: string }[] {
+ return [
+ { id: "format", title: t("mind-map.tab-format"), icon: "bx bx-palette" },
+ { id: "memo", title: t("mind-map.memo"), icon: "bx bx-notepad" }
+ ];
+}
- <PanelSection
- label={t("mind-map.memo")}
- title={memo === MIXED ? t("mind-map.memos-differ") : undefined}
- >
- <NodeMemo
- selectionKey={nodes.map((node) => node.id).join(" ")}
- memo={memo !== MIXED ? memo : null}
- readOnly={memo === MIXED}
- onCommit={writeMemo}
- />
- </PanelSection>
+/**
+ * The panel's header: which group of fields is on show.
+ *
+ * Built as the right pane's tab strip is — a button group as a tablist, the tab on show raised out
+ * of the recessed track by the theme — but with each tab named beside its icon rather than left to
+ * a tooltip. That strip is icon-only because five tabs have to fit a pane; two tabs have the width
+ * of this panel between them, and the same button group the panel already uses for its font sizes
+ * spells its segments out.
+ */
+function NodePanelTabs({ activeTabId, onSelect }: { activeTabId: NodePanelTabId, onSelect: (tabId: NodePanelTabId) => void }) {
+ return (
+ <div className="mind-map-node-panel-tabs">
+ <div className="btn-group btn-group-sm" role="tablist">
+ {buildTabs().map((tab) => (
+ <button
+ key={tab.id}
+ type="button"
+ role="tab"
+ aria-selected={tab.id === activeTabId}
+ // `active` is what the theme raises the tab on show with; the rest is the
+ // segment the panel's other button groups are built from, so the strip
+ // carries the same weight as the rows under it.
+ className={clsx("btn btn-secondary btn-sm mind-map-node-panel-tab", tab.id === activeTabId && "active")}
+ onClick={() => onSelect(tab.id)}
+ >
+ <Icon icon={tab.icon} />
+ {tab.title}
+ </button>
+ ))}
+ </div>
</div>
);
}