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 memo be read on a map that cannot be edited
Elian Doran14885277246b
diff --git a/apps/client/src/widgets/type_widgets/mind_map/MindMap.tsx b/apps/client/src/widgets/type_widgets/mind_map/MindMap.tsxindex a5ebf494c4..5a1a5d9ecd 100644--- a/apps/client/src/widgets/type_widgets/mind_map/MindMap.tsx+++ b/apps/client/src/widgets/type_widgets/mind_map/MindMap.tsx@@ -190,8 +190,11 @@ export default function MindMap({ note, ntxId, noteContext }: TypeWidgetProps) {onKeyDown}}>- {!isReadOnly && selectedNodes.length > 0 && apiRef.current &&- <NodePanel mind={apiRef.current} noteId={note.noteId} nodes={selectedNodes} />}+ {/* Offered on a read-only map as well: Mind Elixir still selects a node there, and the+ panel then holds the memo alone — the one thing a node carries that the map itself+ never draws, and which would otherwise be out of reach (see NodePanel). */}+ {selectedNodes.length > 0 && apiRef.current &&+ <NodePanel mind={apiRef.current} noteId={note.noteId} nodes={selectedNodes} readOnly={isReadOnly} />}</MindElixir>);}diff --git a/apps/client/src/widgets/type_widgets/mind_map/NodePanel.css b/apps/client/src/widgets/type_widgets/mind_map/NodePanel.cssindex 2173a3370f..5367e9e822 100644--- a/apps/client/src/widgets/type_widgets/mind_map/NodePanel.css+++ b/apps/client/src/widgets/type_widgets/mind_map/NodePanel.css@@ -33,6 +33,20 @@border-bottom: 1px solid var(--main-border-color);}+/*+ * Where the memo is all there is (a map that can only be read), the strip gives way to a heading,+ * dressed as the strip was: the same line under it, and the same room — reckoned from the memo's own+ * padding, so that the heading stands over the writing rather than beside it.+ */+.mind-map-node-panel .mind-map-node-panel-title {+ display: flex;+ align-items: center;+ gap: 6px;+ padding: 10px 15px;+ border-bottom: 1px solid var(--main-border-color);+ font-weight: 550;+}+/** Every tab is laid into the same cell, so the panel stands as tall as the tallest of them and* keeps that height whichever is on show. Were each tab to size the panel in turn, switching to thediff --git a/apps/client/src/widgets/type_widgets/mind_map/NodePanel.spec.tsx b/apps/client/src/widgets/type_widgets/mind_map/NodePanel.spec.tsxindex 19140ad0ea..8fdab885d0 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@@ -405,6 +405,63 @@ describe("the memo field of the panel", () => {expect(secondMind.reshapeNode).not.toHaveBeenCalled();expect(firstMind.reshapeNode).toHaveBeenCalledTimes(1);});++ /** The panel as a map that can only be read raises it, which is the memo or nothing at all. */+ async function renderReadOnly(nodes: NodeObj[], mind = buildMind(nodes).mind) {+ let container: HTMLElement | undefined;++ await act(async () => {+ container = renderInto(<NodePanel mind={mind} noteId="map" nodes={nodes} readOnly />);+ });+ if (!container) throw new Error("render produced no container");+ return container;+ }++ it("holds the memo alone, and nothing to edit with, on a map that can only be read", async () => {+ const node = buildNode({ id: "a", memo: "<p>About A</p>" } as Partial<NodeObj>);++ const panel = await renderReadOnly([node]);++ // What is written about the node is there to be read at once, with no tab to reach it+ // through: with the fields gone there is nothing left to choose between.+ expect(shownMemo(panel)).toBe("<p>About A</p>");+ expect(panel.querySelectorAll('[role="tab"]')).toHaveLength(0);+ expect(panel.querySelector(".mind-map-node-panel-title")).toBeTruthy();+ // None of the fields, every one of which edits — and what they stand for is on the node.+ expect(panel.querySelectorAll(".mind-map-node-panel-section")).toHaveLength(0);+ });++ it("stands aside where there is no one memo to read", async () => {+ // Nothing written about the node, and nothing to show for a selection whose memos differ:+ // either way the panel could only put an empty pane between the reader and the map.+ expect((await renderReadOnly([buildNode()])).querySelector(".mind-map-node-panel")).toBeNull();+ expect((await renderReadOnly([+ buildNode({ id: "a", memo: "<p>About A</p>" } as Partial<NodeObj>),+ buildNode({ id: "b", memo: "<p>About B</p>" } as Partial<NodeObj>)+ ])).querySelector(".mind-map-node-panel")).toBeNull();++ // A map that can be edited raises the panel for the very same node, memo or no memo.+ const editable = renderInto(<NodePanel mind={buildMind([buildNode()]).mind} noteId="map" nodes={[buildNode()]} />);+ expect(editable.querySelector(".mind-map-node-panel")).toBeTruthy();+ });++ it("writes nothing back from a map that can only be read", async () => {+ const first = buildNode({ id: "a", memo: "<p>About A</p>" } as Partial<NodeObj>);+ const second = buildNode({ id: "b", memo: "<p>About B</p>" } as Partial<NodeObj>);+ const firstMind = buildMind([first]);++ const panel = await renderReadOnly([first], firstMind.mind);++ // Nothing can be typed into it in the first place, the field being held read-only; what it+ // is holding is written back all the same as the selection moves on, and that is the way a+ // map being read would come to be written to.+ await typeMemo("<p>About A, meddled with</p>");+ await act(async () => {+ render(<NodePanel mind={buildMind([second]).mind} noteId="map" nodes={[second]} readOnly />, panel);+ });++ expect(firstMind.reshapeNode).not.toHaveBeenCalled();+ });});describe("applyTagTexts", () => {diff --git a/apps/client/src/widgets/type_widgets/mind_map/NodePanel.tsx b/apps/client/src/widgets/type_widgets/mind_map/NodePanel.tsxindex 32be3cc4db..c84ab28b97 100644--- a/apps/client/src/widgets/type_widgets/mind_map/NodePanel.tsx+++ b/apps/client/src/widgets/type_widgets/mind_map/NodePanel.tsx@@ -44,13 +44,20 @@ interface NodePanelProps {noteId: string;/** The currently selected nodes; the panel edits all of them at once. */nodes: NodeObj[];+ /**+ * The map cannot be edited, which leaves the panel the memo alone: every other field either+ * edits a node or stands for something already drawn on it, while the memo is the one thing a+ * node carries that the map itself never shows.+ */+ readOnly?: boolean;}/*** Floating panel displayed over a mind map while at least one node is selected, holding the- * formatting controls for the selection.+ * formatting controls for the selection — or, where the map is only being read, the memo of the+ * selected node and nothing else (see {@link NodePanelProps.readOnly}).*/-export default function NodePanel({ mind, noteId, nodes }: NodePanelProps) {+export default function NodePanel({ mind, noteId, nodes, readOnly }: NodePanelProps) {const fontSize = getCommonValue(nodes, (node) => node.style?.fontSize);const textColor = getCommonValue(nodes, (node) => node.style?.color);const backgroundColor = getCommonValue(nodes, (node) => node.style?.background);@@ -70,6 +77,13 @@ export default function NodePanel({ mind, noteId, nodes }: NodePanelProps) {const openedTabs = useRef(new Set<NodePanelTabId>());openedTabs.current.add(activeTabId);+ // A map that cannot be edited is shown the memo or nothing at all: a panel holding an empty pane+ // would only stand between the reader and the map. That takes in a selection whose memos differ+ // as well — there is no one memo to show, and nothing here to fill the pane with instead.+ if (readOnly && (memo === null || memo === MIXED)) {+ 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.@@ -130,17 +144,25 @@ export default function NodePanel({ mind, noteId, nodes }: NodePanelProps) {onMouseDown={(e) => e.stopPropagation()}onKeyDown={(e) => e.stopPropagation()}>- <TabStrip- className="mind-map-node-panel-tabs"- tabs={buildTabs()}- activeTabId={activeTabId}- onSelect={setActiveTabId}- />+ {/* 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}+ />+ )}{/* The tabs are stacked one over the other rather than shown one at a time, so thatthe panel keeps the height of the tallest of them whichever is on show. */}<div className="mind-map-node-panel-bodies">- <div+ {!readOnly && <divrole="tabpanel"className="mind-map-node-panel-body"aria-hidden={activeTabId !== "format"}@@ -242,21 +264,22 @@ export default function NodePanel({ mind, noteId, nodes }: NodePanelProps) {onCommit={(texts) => patchSelectedNodes((node) => ({ tags: applyTagTexts(node.tags, texts) }))}/></PanelSection>- </div>+ </div>}- {/* 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") && (+ {/* The memo goes without a label of its own: what stands above it says what it is —+ the tab it fills, or the heading where there are no tabs — and the editor is the+ whole of the tab rather than a field within it. */}+ {(readOnly || openedTabs.current.has("memo")) && (<div- role="tabpanel"+ role={!readOnly ? "tabpanel" : undefined}className="mind-map-node-panel-body mind-map-node-panel-memo-body"- aria-hidden={activeTabId !== "memo"}+ aria-hidden={!readOnly ? activeTabId !== "memo" : undefined}title={memo === MIXED ? t("mind-map.memos-differ") : undefined}><NodeMemoselectionKey={nodes.map((node) => node.id).join(" ")}memo={memo !== MIXED ? memo : null}- readOnly={memo === MIXED}+ readOnly={readOnly || memo === MIXED}onCommit={writeMemo}/></div>