Skip to content

Build your personal knowledge base with Trilium Notes

feat(relation_map): pick a note's color from its context menu

Elian Doran808f4d4a2246
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.tsx
index 0b971d5b10..7069c1ade3 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,7 +36,7 @@ describe("relation map NoteBox", () => {
function Harness({ selected }: { selected: boolean }) {
const mapApiRef = useRef<RelationMapApi>(null);
- return <NoteBox noteId="boxnote" x={10} y={20} mapApiRef={mapApiRef} selected={selected} />;
+ return <NoteBox noteId="boxnote" x={10} y={20} mapApiRef={mapApiRef} selected={selected} isReadOnly={false} />;
}
async function mount(selected = false) {
diff --git a/apps/client/src/widgets/type_widgets/relation_map/NoteBox.tsx b/apps/client/src/widgets/type_widgets/relation_map/NoteBox.tsx
index 886cb01e1c..cf62933220 100644
--- a/apps/client/src/widgets/type_widgets/relation_map/NoteBox.tsx
+++ b/apps/client/src/widgets/type_widgets/relation_map/NoteBox.tsx
@@ -34,9 +34,11 @@ interface NoteBoxProps extends MapDataNoteEntry {
mapApiRef: RefObject<RelationMapApi | null>;
/** The note is open in the note pane. */
selected?: boolean;
+ /** The map cannot be edited, so the context menu offers no color picker. */
+ isReadOnly: boolean;
}
-export function NoteBox({ noteId, x, y, mapApiRef, selected }: NoteBoxProps) {
+export function NoteBox({ noteId, x, y, mapApiRef, selected, isReadOnly }: NoteBoxProps) {
const [ note, setNote ] = useState<FNote | null>();
const title = useNoteProperty(note, "title");
const icon = useNoteIcon(note);
@@ -46,8 +48,8 @@ export function NoteBox({ noteId, x, y, mapApiRef, selected }: NoteBoxProps) {
}, [ noteId ]);
const contextMenuHandler = useMemo(() => {
- return buildNoteContextMenuHandler(note, mapApiRef);
- }, [ note ]);
+ return buildNoteContextMenuHandler(note, mapApiRef, isReadOnly);
+ }, [ note, isReadOnly ]);
return note && (
<JsPlumbItem
diff --git a/apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsx b/apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsx
index 2c5bdf85c8..1677f61d58 100644
--- a/apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsx
+++ b/apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsx
@@ -181,7 +181,7 @@ export default function RelationMap({ note, noteContext, ntxId, parentComponent
onConnection={connectionCallback}
>
{data?.notes.map(note => (
- <NoteBox {...note} mapApiRef={mapApiRef} selected={note.noteId === selection?.noteId} />
+ <NoteBox {...note} mapApiRef={mapApiRef} selected={note.noteId === selection?.noteId} isReadOnly={isReadOnly} />
))}
{placement.placing && <GhostNoteBox elementRef={placement.ghostRef} />}
</JsPlumb>
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.tsx
new file mode 100644
index 0000000000..e6c0620a15
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/relation_map/context_menu.spec.tsx
@@ -0,0 +1,59 @@
+import { h, render } from "preact";
+import { act } from "preact/test-utils";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+import contextMenu, { type MenuItem } from "../../../menus/context_menu";
+import link_context_menu from "../../../menus/link_context_menu";
+import froca from "../../../services/froca";
+import { buildNote } from "../../../test/easy-froca";
+import type RelationMapApi from "./api";
+import { buildNoteContextMenuHandler } from "./context_menu";
+
+describe("relation map note context menu", () => {
+ let container: HTMLElement | undefined;
+ const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});
+ const mapApiRef = { current: null as RelationMapApi | null };
+
+ beforeEach(() => {
+ show.mockClear();
+ // Its items depend on the tab manager, which these tests do not build.
+ vi.spyOn(link_context_menu, "getItems").mockReturnValue([]);
+ buildNote({ id: "boxnote", title: "Specification" });
+ });
+
+ afterEach(() => {
+ if (container) {
+ render(null, container);
+ container.remove();
+ container = undefined;
+ }
+ });
+
+ /** Opens the menu of the box for `boxnote`, and returns the items it shows. */
+ function openMenu(isReadOnly: boolean) {
+ const handler = buildNoteContextMenuHandler(froca.notes["boxnote"], mapApiRef, isReadOnly);
+ handler(new MouseEvent("contextmenu", { cancelable: true }));
+ return (show.mock.calls.at(-1)?.[0].items ?? []) as (MenuItem<string> | null)[];
+ }
+
+ it("ends with a color picker for the note", async () => {
+ const last = openMenu(false).at(-1);
+ if (!last || !("kind" in last) || last.kind !== "custom") {
+ throw new Error("expected the color picker as the last item");
+ }
+
+ container = document.createElement("div");
+ document.body.appendChild(container);
+ await act(async () => {
+ render(h(last.componentFn, {}), container as HTMLElement);
+ });
+
+ 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");
+
+ expect(custom).toHaveLength(0);
+ });
+});
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.ts
index d8f3df2b8c..8b2a5976ef 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
@@ -4,6 +4,7 @@ import { RefObject } from "preact";
import appContext from "../../../components/app_context";
import FNote from "../../../entities/fnote";
import contextMenu from "../../../menus/context_menu";
+import NoteColorPicker from "../../../menus/custom-items/NoteColorPicker";
import link_context_menu from "../../../menus/link_context_menu";
import dialog from "../../../services/dialog";
import toast from "../../../services/toast";
@@ -12,7 +13,7 @@ import server from "../../../services/server";
import RelationMapApi from "./api";
import type { AskRelationName } from "./RelationNamePopover";
-export function buildNoteContextMenuHandler(note: FNote | null | undefined, mapApiRef: RefObject<RelationMapApi | null>) {
+export function buildNoteContextMenuHandler(note: FNote | null | undefined, mapApiRef: RefObject<RelationMapApi | null>, isReadOnly: boolean) {
return (e: MouseEvent) => {
if (!note) return;
e.preventDefault();
@@ -47,6 +48,10 @@ export function buildNoteContextMenuHandler(note: FNote | null | undefined, mapA
uiIcon: "bx bx-trash",
handler: () => confirmRemoveFromMap(note, mapApiRef)
},
+ ...(isReadOnly ? [] : [
+ { kind: "separator" as const },
+ { kind: "custom" as const, componentFn: () => NoteColorPicker({ note }) }
+ ]),
],
selectMenuItemHandler({ command }) {
// Pass the events to the link context menu
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.html
index 1ae2190257..e51f8348d9 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
@@ -47,8 +47,9 @@
window. The right click menu offers the same options.</li>
</ul>
</li>
- <li>To edit the title of a note or to delete it (either from the map, or delete
- it completely), right click the note.</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>
<li>To rename or delete a relationship, right click it and select the corresponding
option.</li>
</ul>
diff --git a/docs/User Guide/User Guide/Note Types/Relation Map.md b/docs/User Guide/User Guide/Note Types/Relation Map.md
index 6456e6bd2e..4bb1ed05e5 100644
--- a/docs/User Guide/User Guide/Note Types/Relation Map.md
+++ b/docs/User Guide/User Guide/Note Types/Relation Map.md
@@ -16,7 +16,7 @@ Relation map is a type of note which visualizes notes and their [relations](..
* Each note is shown as a card with its icon and title, colored in the note's color the same way as the cards of a <a class="reference-link" href="../Collections/Kanban%20Board.md">Kanban Board</a>.
* 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 or to delete it (either from the map, or delete it completely), right click the note.
+* 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 rename or delete a relationship, right click it and select the corresponding option.
## Note panel