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
fix(relation_map): offer no editing on a read-only map
Elian Dorana277253a17f9
diff --git a/apps/client/src/widgets/type_widgets/relation_map/NoteBox.spec.tsx b/apps/client/src/widgets/type_widgets/relation_map/NoteBox.spec.tsxindex a67915066c..7c25729628 100644--- a/apps/client/src/widgets/type_widgets/relation_map/NoteBox.spec.tsx+++ b/apps/client/src/widgets/type_widgets/relation_map/NoteBox.spec.tsx@@ -36,16 +36,16 @@ describe("relation map NoteBox", () => {const onResize = vi.fn();- function Harness({ selected }: { selected: boolean }) {+ function Harness({ selected, isReadOnly }: { selected: boolean; isReadOnly: boolean }) {const mapApiRef = useRef<RelationMapApi>(null);- return <NoteBox noteId="boxnote" x={10} y={20} mapApiRef={mapApiRef} selected={selected} isReadOnly={false} onPointerDown={() => {}} onResize={onResize} />;+ return <NoteBox noteId="boxnote" x={10} y={20} mapApiRef={mapApiRef} selected={selected} isReadOnly={isReadOnly} onPointerDown={() => {}} onResize={onResize} />;}- async function mount(selected = false) {+ async function mount(selected = false, isReadOnly = false) {await act(async () => {render(<ParentComponent.Provider value={component}>- <Harness selected={selected} />+ <Harness selected={selected} isReadOnly={isReadOnly} /></ParentComponent.Provider>,container as HTMLElement);@@ -71,6 +71,15 @@ describe("relation map NoteBox", () => {expect(onResize).toHaveBeenCalledWith("boxnote", { width: 0, height: 0 });});+ it("offers a handle to draw a relation from only on a map that can be edited", async () => {+ await mount();+ expect(box()?.querySelector(".endpoint")).not.toBeNull();++ await mount(false, true);+ expect(box()).not.toBeNull();+ expect(box()?.querySelector(".endpoint")).toBeNull();+ });+it("follows the note's colour and the selection", async () => {await mount();const coloured = cssClassManager.createClassForColor("#ff0000");diff --git a/apps/client/src/widgets/type_widgets/relation_map/NoteBox.tsx b/apps/client/src/widgets/type_widgets/relation_map/NoteBox.tsxindex ac2899bc4a..ca8833a221 100644--- a/apps/client/src/widgets/type_widgets/relation_map/NoteBox.tsx+++ b/apps/client/src/widgets/type_widgets/relation_map/NoteBox.tsx@@ -68,7 +68,7 @@ export function NoteBox({ noteId, x, y, mapApiRef, selected, highlighted, dropTa><span className={clsx("note-box-icon", icon)} /><span className="note-box-title">{title}</span>- <div className="endpoint" title={t("relation_map.start_dragging_relations")} />+ {!isReadOnly && <div className="endpoint" title={t("relation_map.start_dragging_relations")} />}</div>);}diff --git a/apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsx b/apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsxindex 110a2420bb..d32b7c7448 100644--- a/apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsx+++ b/apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsx@@ -142,8 +142,8 @@ export default function RelationMap({ note, noteContext, ntxId, onOpenNote }: Reconst onRelationContextMenu = useCallback((relation: ClientRelation, e: MouseEvent) => {const anchor = () => containerRef.current?.querySelector(`[data-connection-id="${CSS.escape(relation.attributeId)}"]`);- showRelationContextMenu(e, relation, mapApiRef, (defaultValue) => relationNamePrompt.ask(anchor, defaultValue));- }, [ relationNamePrompt.ask ]);+ showRelationContextMenu(e, relation, mapApiRef, (defaultValue) => relationNamePrompt.ask(anchor, defaultValue), isReadOnly);+ }, [ relationNamePrompt.ask, isReadOnly ]);const onCanvasContextMenu = useCallback((e: MouseEvent) => {const isOnItem = e.target instanceof Element && e.target.closest(PAN_EXCLUDED.map((name) => `.${name}`).join(","));@@ -194,6 +194,7 @@ export default function RelationMap({ note, noteContext, ntxId, onOpenNote }: RedropTarget={entry.noteId === pending?.targetNoteId}isReadOnly={isReadOnly}onPointerDown={(e) => {+ if (isReadOnly) return;if (e.target instanceof Element && e.target.closest(".endpoint")) {startDrawing(e, entry.noteId);} else {diff --git a/apps/client/src/widgets/type_widgets/relation_map/context_menu.spec.tsx b/apps/client/src/widgets/type_widgets/relation_map/context_menu.spec.tsxindex 77d8302678..b019f83658 100644--- a/apps/client/src/widgets/type_widgets/relation_map/context_menu.spec.tsx+++ b/apps/client/src/widgets/type_widgets/relation_map/context_menu.spec.tsx@@ -54,10 +54,10 @@ describe("relation map note context menu", () => {expect(container.querySelector(".note-color-picker")).toBeTruthy();});- it("offers no color picker on a read-only map", () => {- const custom = openMenu(true).filter((item) => item && "kind" in item && item.kind === "custom");+ it("offers only the link's own items on a read-only map", () => {+ vi.spyOn(link_context_menu, "getItems").mockReturnValue([ { title: "Open in a new tab" } ]);- expect(custom).toHaveLength(0);+ expect(openMenu(true)).toEqual([ { title: "Open in a new tab" } ]);});});@@ -98,7 +98,7 @@ describe("relation map relation context menu", () => {async function choose(command: string) {const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});const event = new MouseEvent("contextmenu", { cancelable: true });- showRelationContextMenu(event, relation, mapApiRef, askRelationName);+ showRelationContextMenu(event, relation, mapApiRef, askRelationName, false);expect(event.defaultPrevented).toBe(true);const options = show.mock.calls.at(-1)?.[0];@@ -131,6 +131,15 @@ describe("relation map relation context menu", () => {expect(showError).toHaveBeenCalledTimes(1);});+ it("offers nothing to do to a relation on a read-only map", () => {+ const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});+ const event = new MouseEvent("contextmenu", { cancelable: true });+ showRelationContextMenu(event, relation, mapApiRef, askRelationName, true);++ expect(show).not.toHaveBeenCalled();+ expect(event.defaultPrevented).toBe(false);+ });+it("removes the relation once confirmed", async () => {const confirm = vi.spyOn(dialog, "confirm").mockResolvedValueOnce(false).mockResolvedValueOnce(true);diff --git a/apps/client/src/widgets/type_widgets/relation_map/context_menu.ts b/apps/client/src/widgets/type_widgets/relation_map/context_menu.tsindex 164727b7e5..2165077c36 100644--- a/apps/client/src/widgets/type_widgets/relation_map/context_menu.ts+++ b/apps/client/src/widgets/type_widgets/relation_map/context_menu.ts@@ -12,6 +12,10 @@ import server from "../../../services/server";import toast from "../../../services/toast";import RelationMapApi, { type ClientRelation } from "./api";+/**+ * Returns the handler of a box's context menu: the link's own items, and on a map that can be+ * edited, renaming the note, removing it from the map and its color.+ */export function buildNoteContextMenuHandler(note: FNote | null | undefined, mapApiRef: RefObject<RelationMapApi | null>, isReadOnly: boolean) {return (e: MouseEvent) => {if (!note) return;@@ -20,7 +24,7 @@ export function buildNoteContextMenuHandler(note: FNote | null | undefined, mapAcontextMenu.show({x: e.pageX,y: e.pageY,- items: [+ items: isReadOnly ? link_context_menu.getItems(e) : [...link_context_menu.getItems(e),{ kind: "separator" },{@@ -47,10 +51,8 @@ export function buildNoteContextMenuHandler(note: FNote | null | undefined, mapAuiIcon: "bx bx-trash",handler: () => confirmRemoveFromMap(note, mapApiRef)},- ...(isReadOnly ? [] : [- { kind: "separator" as const },- { kind: "custom" as const, componentFn: () => NoteColorPicker({ note }) }- ]),+ { kind: "separator" },+ { kind: "custom", componentFn: () => NoteColorPicker({ note }) }],selectMenuItemHandler({ command }) {// Pass the events to the link context menu@@ -97,9 +99,11 @@ export function showCanvasContextMenu(event: MouseEvent, { onPaste, onAddNote }:/*** Shows the context menu of a relation, which renames or removes it. `askRelationName` asks for the- * new name next to the relation.+ * new name next to the relation. A read-only map has nothing to offer, and leaves the event alone.*/-export function showRelationContextMenu(event: MouseEvent, relation: ClientRelation, mapApiRef: RefObject<RelationMapApi | null>, askRelationName: (defaultValue: string) => Promise<string | null>) {+export function showRelationContextMenu(event: MouseEvent, relation: ClientRelation, mapApiRef: RefObject<RelationMapApi | null>, askRelationName: (defaultValue: string) => Promise<string | null>, isReadOnly: boolean) {+ if (isReadOnly) return;+event.preventDefault();event.stopPropagation();diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Relation Map.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Relation Map.htmlindex 85f269e88c..278a53b0c8 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Relation Map.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Relation Map.html@@ -66,10 +66,13 @@</ul></li><li>To edit the title of a note, change its color or delete it (either from- the map, or delete it completely), right click the note. The color picker- is not shown if the map is read-only.</li>+ the map, or delete it completely), right click the note.</li><li>To rename or delete a relationship, right click it and select the correspondingoption.</li>+ <li>On a <a href="#root/_help_CoFPLs3dRlXc">read-only</a> map, notes cannot+ be moved and relationships cannot be drawn, renamed or deleted. The right+ click menu of a note only offers to open it, and a relationship has no+ right click menu.</li><li>To move around the map, drag an empty part of it, and zoom with the mousewheel or the buttons at the bottom-right corner. Clicking the zoom percentagegoes back to 100%, and <span class="tn-icon bx bx-scan"></span> <em>Fit to view</em> zoomsdiff --git a/docs/User Guide/User Guide/Note Types/Relation Map.md b/docs/User Guide/User Guide/Note Types/Relation Map.mdindex 3b4075b438..5100dbb463 100644--- a/docs/User Guide/User Guide/Note Types/Relation Map.md+++ b/docs/User Guide/User Guide/Note Types/Relation Map.md@@ -22,8 +22,9 @@ Relation map is a type of note which visualizes notes and their [relations](..* Hovering a note highlights its relationships, in the note's color if it has one, and fades the other relationships.* To view or edit a note, click it on the map. This opens the note panel (see below).* To open the note in a new tab instead, <kbd>Ctrl</kbd>+click it or click it with the middle mouse button. <kbd>Shift</kbd>+click opens it in a new window. The right click menu offers the same options.-* To edit the title of a note, change its color or delete it (either from the map, or delete it completely), right click the note. The color picker is not shown if the map is read-only.+* To edit the title of a note, change its color or delete it (either from the map, or delete it completely), right click the note.* To rename or delete a relationship, right click it and select the corresponding option.+* On a [read-only](../Basic%20Concepts%20and%20Features/Notes/Read-Only%20Notes.md) map, notes cannot be moved and relationships cannot be drawn, renamed or deleted. The right click menu of a note only offers to open it, and a relationship has no right click menu.* To move around the map, drag an empty part of it, and zoom with the mouse wheel or the buttons at the bottom-right corner. Clicking the zoom percentage goes back to 100%, and <span class="tn-icon bx bx-scan"></span> _Fit to view_ zooms and pans so that all the notes are visible.* Once the map has been clicked, the arrow keys or <kbd>W</kbd> <kbd>A</kbd> <kbd>S</kbd> <kbd>D</kbd> pan it, and <kbd>+</kbd> and <kbd>-</kbd> zoom it. The <kbd>?</kbd> button at the top-right corner of the map lists all its keyboard shortcuts.