Skip to content

Build your personal knowledge base with Trilium Notes

feat(relation-map): move adding a note onto an editing group of the map's own

Elian Doran3f6fcf175f9b
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index cb89348d43..96221d0114 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -1194,6 +1194,7 @@
},
"relation_map_buttons": {
"create_child_note_title": "Create child note and add it to map",
+ "create_child_note_text": "Add note",
"reset_pan_zoom_title": "Reset pan & zoom to initial coordinates and magnification",
"zoom_in_title": "Zoom In",
"zoom_out_title": "Zoom Out"
diff --git a/apps/client/src/widgets/FloatingButtonsDefinitions.tsx b/apps/client/src/widgets/FloatingButtonsDefinitions.tsx
index 30ea2dc139..1bf8f5c47e 100644
--- a/apps/client/src/widgets/FloatingButtonsDefinitions.tsx
+++ b/apps/client/src/widgets/FloatingButtonsDefinitions.tsx
@@ -255,12 +255,8 @@ function RelationMapButtons({ note, isDefaultViewMode, triggerEvent }: FloatingB
const isEnabled = (note.type === "relationMap" && isDefaultViewMode);
return isEnabled && (
<>
- <FloatingButton
- icon="bx bx-folder-plus"
- text={t("relation_map_buttons.create_child_note_title")}
- onClick={() => triggerEvent("relationMapCreateChildNote")}
- />
-
+ {/* Adding a note is not offered here: it stands on the map itself, where the click that
+ places the note lands (see EditToolbar.tsx in the relation map). */}
<FloatingButton
icon="bx bx-crop"
text={t("relation_map_buttons.reset_pan_zoom_title")}
diff --git a/apps/client/src/widgets/ribbon/NoteActionsCustom.tsx b/apps/client/src/widgets/ribbon/NoteActionsCustom.tsx
index 5ef95703bf..39ba9bed44 100644
--- a/apps/client/src/widgets/ribbon/NoteActionsCustom.tsx
+++ b/apps/client/src/widgets/ribbon/NoteActionsCustom.tsx
@@ -72,7 +72,6 @@ export default function NoteActionsCustom(props: NoteActionsCustomProps) {
ref={containerRef}
className="note-actions-custom"
>
- <AddChildButton {...innerProps} />
<RunActiveNoteButton {...innerProps } />
<SwitchSplitOrientationButton {...innerProps} />
<DisplayModeSwitcher {...innerProps} />
@@ -316,16 +315,6 @@ function InAppHelpButton({ note }: NoteActionsCustomInnerProps) {
);
}
-function AddChildButton({ parentComponent, noteType, ntxId, isReadOnly }: NoteActionsCustomInnerProps) {
- if (noteType === "relationMap") {
- return <NoteAction
- icon="bx bx-folder-plus"
- text={t("relation_map_buttons.create_child_note_title")}
- onClick={() => parentComponent.triggerEvent("relationMapCreateChildNote", { ntxId })}
- disabled={isReadOnly}
- />;
- }
-}
//#endregion
function NoteAction({ text, active, ...props }: Pick<ActionButtonProps, "text" | "icon" | "disabled" | "triggerCommand" | "active"> & {
diff --git a/apps/client/src/widgets/type_widgets/relation_map/MapToolbar.css b/apps/client/src/widgets/type_widgets/relation_map/MapToolbar.css
index 6df988cdee..2152441355 100644
--- a/apps/client/src/widgets/type_widgets/relation_map/MapToolbar.css
+++ b/apps/client/src/widgets/type_widgets/relation_map/MapToolbar.css
@@ -1,6 +1,23 @@
-.relation-map-wrapper .relation-map-toolbar {
+.relation-map-wrapper .relation-map-toolbar,
+.relation-map-wrapper .relation-map-edit-toolbar {
position: absolute;
- inset-inline-end: var(--overlay-button-inset, 10px);
bottom: var(--overlay-button-inset, 10px);
z-index: 10;
}
+
+.relation-map-wrapper .relation-map-toolbar {
+ inset-inline-end: var(--overlay-button-inset, 10px);
+}
+
+.relation-map-wrapper .relation-map-edit-toolbar {
+ inset-inline-start: var(--overlay-button-inset, 10px);
+}
+
+.relation-map-wrapper .relation-map-add-note-button {
+ gap: 6px;
+ padding-inline: 12px;
+}
+
+.relation-map-wrapper .relation-map-add-note-button .bx {
+ font-size: var(--overlay-button-icon-size);
+}
diff --git a/apps/client/src/widgets/type_widgets/relation_map/MapToolbar.spec.tsx b/apps/client/src/widgets/type_widgets/relation_map/MapToolbar.spec.tsx
index 629b17bc07..d8b368a941 100644
--- a/apps/client/src/widgets/type_widgets/relation_map/MapToolbar.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/relation_map/MapToolbar.spec.tsx
@@ -1,80 +1,133 @@
+/**
+ * The two groups standing over a relation map (see MapToolbar.tsx): the camera at one foot corner,
+ * and the editing actions at the other.
+ */
import { render } from "preact";
import { act } from "preact/test-utils";
-import { afterEach, describe, expect, it, vi } from "vitest";
+import { describe, expect, it, vi } from "vitest";
-// The bootstrap tooltip the buttons wear needs real layout, which happy-dom hasn't.
-vi.mock("../../react/hooks", async (importOriginal) => ({
- ...(await importOriginal<object>()),
- useStaticTooltip: () => {}
-}));
-vi.mock("../../../services/i18n", () => ({ t: (key: string) => key }));
-
-import MapToolbar, { type MapCommand } from "./MapToolbar";
+import { renderInto } from "../../../test/render";
+import MapToolbar, { EditToolbar, type MapCommand } from "./MapToolbar";
-let container: HTMLDivElement;
-
-afterEach(() => {
- act(() => render(null, container));
- container.remove();
-});
+vi.mock("../../../services/i18n", () => ({ t: (key: string) => key }));
-describe("MapToolbar", () => {
+describe("relation map MapToolbar", () => {
it("says the scale the map is drawn at, following it as the map is moved", () => {
- const map = fakeMap();
- mount(map);
+ const { map, readout } = renderToolbar();
- expect(readout().textContent).toBe("100%");
+ expect(readout()?.textContent).toBe("100%");
act(() => map.moveTo(2.5));
- expect(readout().textContent).toBe("250%");
+ expect(readout()?.textContent).toBe("250%");
// A pan leaves the scale where it was, so the readout says the same thing.
act(() => map.moveTo(2.5));
- expect(readout().textContent).toBe("250%");
+ expect(readout()?.textContent).toBe("250%");
});
it("leaves a step with no room left to it disabled", () => {
- const map = fakeMap();
- mount(map);
+ const { map, zoomIn, zoomOut } = renderToolbar();
- expect([ zoomOut().disabled, zoomIn().disabled ]).toEqual([ false, false ]);
+ expect([ zoomOut()?.disabled, zoomIn()?.disabled ]).toEqual([ false, false ]);
act(() => map.moveTo(MIN_ZOOM));
- expect([ zoomOut().disabled, zoomIn().disabled ]).toEqual([ true, false ]);
+ expect([ zoomOut()?.disabled, zoomIn()?.disabled ]).toEqual([ true, false ]);
act(() => map.moveTo(MAX_ZOOM));
- expect([ zoomOut().disabled, zoomIn().disabled ]).toEqual([ false, true ]);
+ expect([ zoomOut()?.disabled, zoomIn()?.disabled ]).toEqual([ false, true ]);
});
it("asks for what each button stands for rather than moving the map itself", () => {
- const commands: MapCommand[] = [];
- const map = fakeMap();
- mount(map, (command) => commands.push(command));
+ const { map, commands, readout, zoomIn, zoomOut } = renderToolbar();
- act(() => zoomOut().click());
- act(() => readout().click());
- act(() => zoomIn().click());
+ act(() => zoomOut()?.click());
+ act(() => readout()?.click());
+ act(() => zoomIn()?.click());
expect(commands).toEqual([ "relationMapResetZoomOut", "relationMapResetPanZoom", "relationMapResetZoomIn" ]);
expect(map.transformListeners()).toBe(1);
});
- it("stands aside while there is no map to read, and stops listening to one that is torn down", () => {
- const map = fakeMap();
- mount(undefined);
- expect(container.querySelector(".relation-map-toolbar")).toBeNull();
+ it("stands aside while there is no map to read", () => {
+ const { buttons } = renderToolbar({ withMap: false });
+
+ expect(buttons()).toHaveLength(0);
+ });
- mount(map);
+ it("stops listening to a map that is torn down", () => {
+ const { map, unmount } = renderToolbar();
expect(map.transformListeners()).toBe(1);
- act(() => render(null, container));
+ unmount();
expect(map.transformListeners()).toBe(0);
});
});
+describe("relation map EditToolbar", () => {
+ it("offers to add a note in words as well as in a mark, and hands the asking to the map view", () => {
+ const { button, onAddNote } = renderEditToolbar();
+
+ // The mark is a child of the button rather than the button's own class — the words beside it
+ // are to stay words (see OverlayControlGroup.tsx).
+ expect(button()?.querySelector(".bx-folder-plus")).not.toBeNull();
+ expect(button()?.textContent).toBe("relation_map_buttons.create_child_note_text");
+
+ act(() => button()?.click());
+ expect(onAddNote).toHaveBeenCalledTimes(1);
+ });
+
+ it("refuses on a map that may not be edited", () => {
+ const { button } = renderEditToolbar({ isReadOnly: true });
+
+ expect(button()?.disabled).toBe(true);
+ });
+});
+
const MIN_ZOOM = 0.3;
const MAX_ZOOM = 2;
+/** Builds the camera group over a map, holding on to what the buttons ask of it. */
+function renderToolbar({ withMap = true } = {}) {
+ const map = fakeMap();
+ const commands: MapCommand[] = [];
+ let container: HTMLElement | undefined;
+ act(() => {
+ container = renderInto(
+ <MapToolbar
+ panZoom={withMap ? map as unknown as Parameters<typeof MapToolbar>[0]["panZoom"] : undefined}
+ onCommand={(command) => commands.push(command)}
+ />
+ );
+ });
+ if (!container) throw new Error("the toolbar was not rendered");
+
+ const all = () => [ ...container?.querySelectorAll<HTMLButtonElement>(".relation-map-toolbar button") ?? [] ];
+ return {
+ map,
+ commands,
+ buttons: all,
+ zoomOut: () => all()[0],
+ readout: () => all()[1],
+ zoomIn: () => all()[2],
+ unmount: () => act(() => render(null, container as HTMLElement))
+ };
+}
+
+/** Builds the editing group, which asks for nothing beyond what its one button is driven by. */
+function renderEditToolbar({ isReadOnly = false } = {}) {
+ const onAddNote = vi.fn();
+ let container: HTMLElement | undefined;
+ act(() => {
+ container = renderInto(<EditToolbar isReadOnly={isReadOnly} onAddNote={onAddNote} />);
+ });
+ if (!container) throw new Error("the toolbar was not rendered");
+
+ return {
+ onAddNote,
+ button: () => container?.querySelector<HTMLButtonElement>(".relation-map-edit-toolbar button") ?? null
+ };
+}
+
/** A stand-in for panzoom: a scale that can be moved, told to whoever asked to be told. */
function fakeMap() {
let scale = 1;
@@ -94,21 +147,3 @@ function fakeMap() {
transformListeners: () => listeners.size
};
}
-
-function mount(map: ReturnType<typeof fakeMap> | undefined, onCommand: (command: MapCommand) => void = () => {}) {
- container ??= document.createElement("div");
- document.body.appendChild(container);
- act(() => render(
- <MapToolbar panZoom={map as unknown as Parameters<typeof MapToolbar>[0]["panZoom"]} onCommand={onCommand} />,
- container
- ));
- return container;
-}
-
-function buttons() {
- return container.querySelectorAll<HTMLButtonElement>(".relation-map-toolbar button");
-}
-
-const zoomOut = () => buttons()[0];
-const readout = () => buttons()[1];
-const zoomIn = () => buttons()[2];
diff --git a/apps/client/src/widgets/type_widgets/relation_map/MapToolbar.tsx b/apps/client/src/widgets/type_widgets/relation_map/MapToolbar.tsx
index 2fa0ecd1a7..b397146a68 100644
--- a/apps/client/src/widgets/type_widgets/relation_map/MapToolbar.tsx
+++ b/apps/client/src/widgets/type_widgets/relation_map/MapToolbar.tsx
@@ -59,6 +59,43 @@ export default function MapToolbar({ panZoom, onCommand }: MapToolbarProps) {
);
}
+interface EditToolbarProps {
+ /** The map may not be edited, which is every one of these buttons refused at once. */
+ isReadOnly: boolean;
+ /** Asks for a title and leaves the note waiting for a place to be clicked (see `useNoteCreation`
+ * in RelationMap.tsx). */
+ onAddNote: () => void;
+}
+
+/**
+ * The editing actions, standing in the corner of the foot opposite, on a group of their own — adding
+ * a note today, with room along the row for whatever editing the map comes to offer next.
+ *
+ * A group of its own rather than more buttons on {@link MapToolbar}, as on the geo map: that one is
+ * the camera — how close in the map is drawn and where it stands — and what changes the map is
+ * another kind of thing. Beside it here rather than in a module of its own, as the mind map's two
+ * bars are: a group of one button is not a file's worth, and the two are read together.
+ *
+ * Adding a note is the one thing this map is edited by, so the + carries its name in words rather
+ * than standing as a bare glyph, as the geo map's does. It stands on the map rather than in the
+ * note's own bar of actions, where it was: what it starts is finished by a click on the map, so it
+ * belongs beside the canvas that answers it — the toast that follows says as much in words.
+ */
+export function EditToolbar({ isReadOnly, onAddNote }: EditToolbarProps) {
+ return (
+ <OverlayControlGroup className="relation-map-edit-toolbar">
+ <OverlayControlButton
+ title={t("relation_map_buttons.create_child_note_title")}
+ icon="bx-folder-plus"
+ text={t("relation_map_buttons.create_child_note_text")}
+ className="relation-map-add-note-button"
+ disabled={isReadOnly}
+ onClick={onAddNote}
+ />
+ </OverlayControlGroup>
+ );
+}
+
/**
* The scale the map is drawn at, followed as it changes — by these buttons, by the wheel, or by the
* saved view being restored.
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 10cdcd0399..260bc915e4 100644
--- a/apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsx
+++ b/apps/client/src/widgets/type_widgets/relation_map/RelationMap.tsx
@@ -16,12 +16,12 @@ import { isExperimentalFeatureEnabled } from "../../../services/experimental_fea
import { t } from "../../../services/i18n";
import server from "../../../services/server";
import toast from "../../../services/toast";
-import { useEditorSpacedUpdate, useTriliumEvent, useTriliumEvents } from "../../react/hooks";
+import { useEditorSpacedUpdate, useNoteLabelBoolean, useTriliumEvent, useTriliumEvents } from "../../react/hooks";
import { TypeWidgetProps } from "../type_widget";
import RelationMapApi, { ClientRelation, MapData, MapDataNoteEntry, RelationType } from "./api";
import { buildRelationContextMenuHandler } from "./context_menu";
import { JsPlumb } from "./jsplumb";
-import MapToolbar from "./MapToolbar";
+import MapToolbar, { EditToolbar } from "./MapToolbar";
import { NoteBox } from "./NoteBox";
import setupOverlays, { uniDirectionalOverlays } from "./overlays";
import { getMousePosition, getZoom, idToNoteId, noteIdToId, promptForRelationName } from "./utils";
@@ -52,6 +52,8 @@ declare module "jsplumb" {
export default function RelationMap({ note, noteContext, ntxId, parentComponent }: TypeWidgetProps) {
const [ data, setData ] = useState<MapData>();
+ // The same read-only the note's own bar of actions read while the + stood there.
+ const [ isReadOnly ] = useNoteLabelBoolean(note, "readOnly");
const containerRef = useRef<HTMLDivElement>(null);
const mapApiRef = useRef<RelationMapApi>(null);
const pbApiRef = useRef<jsPlumbInstance>(null);
@@ -165,6 +167,14 @@ export default function RelationMap({ note, noteContext, ntxId, parentComponent
))}
</JsPlumb>
+ {/* Stands on the map in either layout, unlike the camera below, so that the one thing
+ the map is edited by is on the map itself wherever the rest of the note's buttons
+ happen to live. */}
+ <EditToolbar
+ isReadOnly={isReadOnly}
+ onAddNote={() => parentComponent?.triggerEvent("relationMapCreateChildNote", { ntxId })}
+ />
+
{isNewLayout && (
<MapToolbar
panZoom={panZoom}