Skip to content

Build your personal knowledge base with Trilium Notes

feat(mind-map): let the node panel be sent away

Elian Doran75c437eb28e2
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 3c56ee4665..52de998f33 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -3567,6 +3567,7 @@
"link-mixed": "Different links",
"links-differ": "The selected nodes don't carry the same link.",
"tab-format": "Format",
+ "hide-panel": "Hide this panel",
"memo": "Memo",
"memo-placeholder": "Write a memo...",
"memos-differ": "The selected nodes carry different memos, so this one can only be read.",
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 13536136e4..dc1247b1f4 100644
--- a/apps/client/src/widgets/type_widgets/mind_map/NodePanel.css
+++ b/apps/client/src/widgets/type_widgets/mind_map/NodePanel.css
@@ -40,6 +40,33 @@ body.experimental-feature-new-layout .mind-map-node-panel {
--node-panel-top: var(--mind-map-inset-top);
}
+/*
+ * The head of the panel: a strip of tabs or a heading of one line, and beside either of them the
+ * button that sends the panel away. Only the placing of that button is this row's business — what
+ * heads the panel keeps its own padding and the line under it (see below), the two headers standing
+ * at different heights.
+ */
+.mind-map-node-panel .mind-map-node-panel-header {
+ position: relative;
+ /* The head of a full panel keeps its height; what gives way is the tab under it, which scrolls. */
+ flex-shrink: 0;
+}
+
+/*
+ * At the end of the row and centred on it, whichever header it is standing at. What it looks like
+ * is the theme's — an "x" icon button is dressed as the one closing a tab is, here as everywhere.
+ *
+ * Laid over the row rather than standing in it: the tabs are centred on the panel, and a button
+ * taking its own place beside them would push them off it. There is room to spare next to two tabs
+ * and next to one word, so the two never meet.
+ */
+.mind-map-node-panel .mind-map-node-panel-close {
+ position: absolute;
+ inset-inline-end: 6px;
+ top: 50%;
+ transform: translateY(-50%);
+}
+
/*
* The strip is the right pane's, at the size it stands there (see TabStrip.css); what is left here
* is the row it heads. It parts from the tab under it by a line the width of the panel, so that it
@@ -61,6 +88,8 @@ body.experimental-feature-new-layout .mind-map-node-panel {
align-items: center;
gap: 6px;
padding: 10px 15px;
+ /* Clear of the button laid over the end of the row, for a heading longer than English gives it. */
+ padding-inline-end: 36px;
border-bottom: 1px solid var(--main-border-color);
font-weight: 550;
}
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 a10fa929c3..7274db304d 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
@@ -868,6 +868,43 @@ describe("NodePanel", () => {
expect(reshapeNode).not.toHaveBeenCalled();
});
+ it("goes away when sent away, and comes back with the next selection", async () => {
+ const first = buildNode({ id: "a" });
+ const second = buildNode({ id: "b" });
+ let container: HTMLElement | undefined;
+ await act(async () => {
+ container = renderInto(<NodePanel mind={buildMind([first]).mind} noteId="mapNote" nodes={[first]} />);
+ });
+ if (!container) throw new Error("render produced no container");
+ const panel = container;
+
+ /** Renders the panel for the given selection, as the map does when the selection changes. */
+ const select = async (nodes: NodeObj[]) => act(async () => {
+ render(<NodePanel mind={buildMind(nodes).mind} noteId="mapNote" nodes={nodes} />, panel);
+ });
+ const shown = () => !!panel.querySelector(".mind-map-node-panel");
+ /** Presses the button that sends the panel away, the way it is pressed on the map. */
+ const dismiss = async () => act(async () => {
+ panel.querySelector<HTMLButtonElement>(".mind-map-node-panel-close")?.click();
+ });
+
+ await dismiss();
+ expect(shown()).toBe(false);
+
+ // The node is still the selected one — what was sent away is the panel, not the selection —
+ // so it stays away for as long as that selection stands, edits to the node included.
+ await select([first]);
+ expect(shown()).toBe(false);
+
+ // Anything selected after it raises the panel again, the node just left among them.
+ await select([second]);
+ expect(shown()).toBe(true);
+ await dismiss();
+ expect(shown()).toBe(false);
+ await select([first]);
+ expect(shown()).toBe(true);
+ });
+
it("keeps clicks and key presses from reaching the map underneath", () => {
const nodes = [buildNode()];
const { mind } = buildMind(nodes);
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 1ad857741b..ce1883b385 100644
--- a/apps/client/src/widgets/type_widgets/mind_map/NodePanel.tsx
+++ b/apps/client/src/widgets/type_widgets/mind_map/NodePanel.tsx
@@ -4,7 +4,7 @@ import { parseMindMapNoteLink } from "@triliumnext/commons";
import { Dropdown as BootstrapDropdown } from "bootstrap";
import type { MindElixirInstance, NodeObj, TagObj } from "mind-elixir";
import { ComponentChildren } from "preact";
-import { useRef, useState } from "preact/hooks";
+import { useEffect, useRef, useState } from "preact/hooks";
import appContext from "../../../components/app_context";
import { t } from "../../../services/i18n";
@@ -69,6 +69,14 @@ export default function NodePanel({ mind, noteId, nodes, readOnly }: NodePanelPr
const tags = gatherTags(nodes);
const memo = getCommonValue(nodes, getNodeMemo);
const [ activeTabId, setActiveTabId ] = useState<NodePanelTabId>("format");
+ const selectionKey = nodes.map((node) => node.id).join(" ");
+
+ // The panel can be sent away while the selection it stands for is kept, for the corner of the
+ // map it covers. It is only sent away for as long as that selection lasts: what brings it back
+ // is selecting something — anything, this node included, once something else has been selected
+ // since — which is also what a panel put away by mistake is recovered by.
+ const [ dismissed, setDismissed ] = useState(false);
+ useEffect(() => setDismissed(false), [ selectionKey ]);
// 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
@@ -83,6 +91,10 @@ export default function NodePanel({ mind, noteId, nodes, readOnly }: NodePanelPr
return null;
}
+ if (dismissed) {
+ return null;
+ }
+
/**
* 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.
@@ -144,20 +156,31 @@ export default function NodePanel({ mind, noteId, nodes, readOnly }: NodePanelPr
onMouseDown={(e) => e.stopPropagation()}
onKeyDown={(e) => e.stopPropagation()}
>
- {/* With the memo the only thing on show there is no choice to offer, so the strip gives
- way to a plain heading — the panel still says what it is holding. */}
- {readOnly ? (
- <div className="mind-map-node-panel-title">
- <Icon icon="bx bx-notepad" />{t("mind-map.memo")}
- </div>
- ) : (
- <TabStrip
- className="mind-map-node-panel-tabs"
- tabs={buildTabs()}
- activeTabId={activeTabId}
- onSelect={setActiveTabId}
+ {/* What heads the panel, and the way to send it away — laid over the head of it rather
+ than beside what is there, so that the tabs stay centred on the panel (see the CSS). */}
+ <div className="mind-map-node-panel-header">
+ {/* With the memo the only thing on show there is no choice to offer, so the strip
+ gives way to a plain heading — the panel still says what it is holding. */}
+ {readOnly ? (
+ <div className="mind-map-node-panel-title">
+ <Icon icon="bx bx-notepad" />{t("mind-map.memo")}
+ </div>
+ ) : (
+ <TabStrip
+ className="mind-map-node-panel-tabs"
+ tabs={buildTabs()}
+ activeTabId={activeTabId}
+ onSelect={setActiveTabId}
+ />
+ )}
+
+ <ActionButton
+ className="mind-map-node-panel-close"
+ icon="bx bx-x"
+ text={t("mind-map.hide-panel")}
+ onClick={() => setDismissed(true)}
/>
- )}
+ </div>
{/* The tabs are stacked one over the other rather than shown one at a time, so that
the panel keeps the height of the tallest of them whichever is on show. */}
@@ -277,7 +300,7 @@ export default function NodePanel({ mind, noteId, nodes, readOnly }: NodePanelPr
title={memo === MIXED ? t("mind-map.memos-differ") : undefined}
>
<NodeMemo
- selectionKey={nodes.map((node) => node.id).join(" ")}
+ selectionKey={selectionKey}
memo={memo !== MIXED ? memo : null}
readOnly={readOnly || memo === MIXED}
onCommit={(memo) => patchGivenNodes({ memo } as Partial<NodeObj>)}