Skip to content

Build your personal knowledge base with Trilium Notes

feat(geomap): move adding a note onto an editing bar of the map's own

Elian Doran61c8ae22a80b
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 03d1036972..f4b0b8a537 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2739,7 +2739,7 @@
},
"geo-map": {
"create-child-note-title": "Create a new child note and add it to the map",
- "create-child-note-text": "Add marker",
+ "create-child-note-cancel": "Cancel adding the note",
"create-child-note-instruction": "Click on the map to create a new note at that location or press Escape to dismiss.",
"create-child-note-toast-title": "New note",
"move-marker-instruction": "Click on the map to move the marker to that location or press Escape to dismiss.",
diff --git a/apps/client/src/widgets/collections/geomap/EditToolbar.css b/apps/client/src/widgets/collections/geomap/EditToolbar.css
new file mode 100644
index 0000000000..7255fdd7bf
--- /dev/null
+++ b/apps/client/src/widgets/collections/geomap/EditToolbar.css
@@ -0,0 +1,16 @@
+/*
+ * The editing bar stands in the top leading corner, laid down the column rather than along the row:
+ * the other corners are spoken for — the detail pane holds the whole trailing edge, the camera bar
+ * one side of the foot and the scale and attribution the other — and a column grows by a button
+ * without reaching along the map's edge toward any of them.
+ *
+ * Held off the edges by the shared inset, and dressed with the rest of the map's furniture: the
+ * palette handoff both bars wear is settled once in index.css.
+ */
+.geo-map-container .geo-edit-toolbar {
+ top: var(--geo-map-inset);
+ inset-inline-start: var(--geo-map-inset);
+ /* Above the canvas, clearing MapLibre's own control corners at 2 (see MapToolbar.css). */
+ z-index: 3;
+ flex-direction: column;
+}
diff --git a/apps/client/src/widgets/collections/geomap/EditToolbar.spec.tsx b/apps/client/src/widgets/collections/geomap/EditToolbar.spec.tsx
new file mode 100644
index 0000000000..2b20c9e968
--- /dev/null
+++ b/apps/client/src/widgets/collections/geomap/EditToolbar.spec.tsx
@@ -0,0 +1,63 @@
+/**
+ * The bar of editing actions over a geo map (see EditToolbar.tsx). What is checked is the one
+ * button it holds so far: that a press arms the map and a press on an armed one stands it down,
+ * that the mode is worn as held down, and that a map that may not be edited refuses it.
+ */
+import { act } from "preact/test-utils";
+import { describe, expect, it, vi } from "vitest";
+
+import { renderInto } from "../../../test/render";
+import EditToolbar from "./EditToolbar";
+import { ParentMap } from "./map";
+
+/** Builds the bar over a map, which it asks nothing more of than being there at all. */
+function renderBar({ map = {} as never, isReadOnly = false, placing = false } = {}) {
+ const onTogglePlacement = vi.fn();
+ let container: HTMLElement | undefined;
+ act(() => {
+ container = renderInto(
+ <ParentMap.Provider value={map}>
+ <EditToolbar isReadOnly={isReadOnly} placing={placing} onTogglePlacement={onTogglePlacement} />
+ </ParentMap.Provider>
+ );
+ });
+ if (!container) throw new Error("the toolbar was not rendered");
+
+ return {
+ onTogglePlacement,
+ button: () => container?.querySelector<HTMLButtonElement>(".geo-edit-toolbar button") ?? null
+ };
+}
+
+describe("geo map EditToolbar", () => {
+ it("offers to add a note, and hands the arming to the map view", () => {
+ const { button, onTogglePlacement } = renderBar();
+
+ expect(button()?.className).toContain("bx-plus");
+ expect(button()?.classList.contains("active")).toBe(false);
+
+ act(() => button()?.click());
+ expect(onTogglePlacement).toHaveBeenCalledTimes(1);
+ });
+
+ it("wears an armed map as held down, and the same press stands it down", () => {
+ const { button, onTogglePlacement } = renderBar({ placing: true });
+
+ expect(button()?.classList.contains("active")).toBe(true);
+
+ act(() => button()?.click());
+ expect(onTogglePlacement).toHaveBeenCalledTimes(1);
+ });
+
+ it("refuses on a map that may not be edited", () => {
+ const { button } = renderBar({ isReadOnly: true });
+
+ expect(button()?.disabled).toBe(true);
+ });
+
+ it("stands aside where there is no map at all", () => {
+ const { button } = renderBar({ map: null as never });
+
+ expect(button()).toBeNull();
+ });
+});
diff --git a/apps/client/src/widgets/collections/geomap/EditToolbar.tsx b/apps/client/src/widgets/collections/geomap/EditToolbar.tsx
new file mode 100644
index 0000000000..0f5239ac4e
--- /dev/null
+++ b/apps/client/src/widgets/collections/geomap/EditToolbar.tsx
@@ -0,0 +1,51 @@
+import "./EditToolbar.css";
+
+import { useContext } from "preact/hooks";
+
+import { t } from "../../../services/i18n";
+import OverlayToolbar, { OverlayToolbarButton } from "../../react/OverlayToolbar";
+import { ParentMap } from "./map";
+
+interface EditToolbarProps {
+ /** The map may not be edited, which is every one of these buttons refused at once. */
+ isReadOnly: boolean;
+ /** The map is armed for the next click to place a new note, which the button wears as held
+ * down (see OverlayToolbar's active styling). */
+ placing: boolean;
+ /** Arms the map for a note to be placed, or stands it down again — the visible counterpart of
+ * the Escape the instruction toast offers (see index.tsx). */
+ onTogglePlacement: () => void;
+}
+
+/**
+ * The bar of editing actions, standing in the map's top leading corner: adding a note today, with
+ * room down the column for whatever editing the map comes to offer next.
+ *
+ * A bar of its own rather than more buttons on {@link MapToolbar}: that one is the camera — how
+ * close in the map is drawn, how much screen it gets — and what changes the map is another kind of
+ * thing. The corner is the last one free, and the one with room to grow: the trailing edge belongs
+ * to the detail pane, and the foot to the camera bar on one side and the scale on the other.
+ *
+ * It stands on the map rather than in the collection bar above it because the map alone is what
+ * goes fullscreen (see MapToolbar): the collection bar stays behind, and while the button lived
+ * there, a fullscreen map could only be added to through its right-click menu.
+ */
+export default function EditToolbar({ isReadOnly, placing, onTogglePlacement }: EditToolbarProps) {
+ const map = useContext(ParentMap);
+
+ // No bar over a map that could not be drawn (see the WebGL fallback in map.tsx).
+ if (!map) return null;
+
+ return (
+ // Standing at the leading edge, the tooltips open inward, over the map.
+ <OverlayToolbar className="geo-edit-toolbar" titlePosition={glob.isRtl ? "left" : "right"}>
+ <OverlayToolbarButton
+ icon="bx bx-plus"
+ text={placing ? t("geo-map.create-child-note-cancel") : t("geo-map.create-child-note-title")}
+ active={placing}
+ disabled={isReadOnly}
+ onClick={onTogglePlacement}
+ />
+ </OverlayToolbar>
+ );
+}
diff --git a/apps/client/src/widgets/collections/geomap/MapToolbar.css b/apps/client/src/widgets/collections/geomap/MapToolbar.css
index 6d3f439583..cb6ffb35f9 100644
--- a/apps/client/src/widgets/collections/geomap/MapToolbar.css
+++ b/apps/client/src/widgets/collections/geomap/MapToolbar.css
@@ -17,21 +17,5 @@
z-index: 3;
}
-/*
- * The bar is dressed in the set the map's furniture shares (see index.css, which is where a dark map
- * darkens it), handed over here under the names the shared bar reads.
- *
- * Set on the bar rather than on the map, so that nothing else standing over the map is dressed for a
- * theme the reader did not choose — a marker's preview is the app's own note tooltip, and it belongs
- * to the app. The last pair is for whatever this bar comes to hold that stands for a choice in force
- * (see OverlayToolbar.css), which today is nothing.
- */
-.geo-map-container .geo-map-toolbar {
- --menu-background-color: var(--geo-map-panel-background);
- --menu-text-color: var(--geo-map-panel-text-color);
- --dropdown-border-color: var(--geo-map-panel-border-color);
- --icon-button-hover-background: var(--geo-map-panel-hover-background);
- --icon-button-hover-color: var(--geo-map-panel-hover-text-color);
- --active-item-background-color: var(--geo-map-panel-active-background);
- --active-item-text-color: var(--geo-map-panel-active-text-color);
-}
+/* The bar is dressed in the set the map's furniture shares, handed over once for every bar standing
+ over this map — see the palette handoff in index.css, which is also where a dark map darkens it. */
diff --git a/apps/client/src/widgets/collections/geomap/index.css b/apps/client/src/widgets/collections/geomap/index.css
index 7c31973c9d..b4b76f972a 100644
--- a/apps/client/src/widgets/collections/geomap/index.css
+++ b/apps/client/src/widgets/collections/geomap/index.css
@@ -95,6 +95,26 @@
--geo-map-panel-active-text-color: var(--tooltip-foreground-color);
}
+/*
+ * The bars standing over the map — the camera bar in its foot corner (MapToolbar) and the editing
+ * bar at its head (EditToolbar) — dressed in the set the map's furniture shares, handed over here
+ * under the names the shared bar reads (see OverlayToolbar.css).
+ *
+ * Set on the bars rather than on the map, so that nothing else standing over the map is dressed for
+ * a theme the reader did not choose — a marker's preview is the app's own note tooltip, and it
+ * belongs to the app. The last pair marks a choice in force, which is the editing bar's + while the
+ * map waits to be told a place.
+ */
+.geo-map-container .tn-overlay-toolbar {
+ --menu-background-color: var(--geo-map-panel-background);
+ --menu-text-color: var(--geo-map-panel-text-color);
+ --dropdown-border-color: var(--geo-map-panel-border-color);
+ --icon-button-hover-background: var(--geo-map-panel-hover-background);
+ --icon-button-hover-color: var(--geo-map-panel-hover-text-color);
+ --active-item-background-color: var(--geo-map-panel-active-background);
+ --active-item-text-color: var(--geo-map-panel-active-text-color);
+}
+
/*
* MapLibre's own furniture, dressed as the bar opposite it is.
*
diff --git a/apps/client/src/widgets/collections/geomap/index.tsx b/apps/client/src/widgets/collections/geomap/index.tsx
index 3240b36d4b..b4834ef558 100644
--- a/apps/client/src/widgets/collections/geomap/index.tsx
+++ b/apps/client/src/widgets/collections/geomap/index.tsx
@@ -14,12 +14,12 @@ import toast from "../../../services/toast";
import { escapeHtml } from "../../../services/utils";
import CollectionProperties from "../../note_bars/CollectionProperties";
import ActionButton from "../../react/ActionButton";
-import { ButtonOrActionButton } from "../../react/Button";
import { useCollectionTreeDrag, useNoteBlob, useNoteLabel, useNoteLabelBoolean, useNoteProperty, useSpacedUpdate, useTriliumEvent } from "../../react/hooks";
import { ViewModeProps } from "../interface";
import { createNewNote, moveMarker } from "./api";
import ContextMenus from "./ContextMenus";
import DetailPane, { PaneSelection } from "./DetailPane";
+import EditToolbar from "./EditToolbar";
import GhostPin from "./GhostPin";
import { GpxTrack } from "./GpxTrack";
import Map, { GeoMouseEvent } from "./map";
@@ -89,10 +89,17 @@ export default function GeoView({ note, noteIds, viewConfig, saveConfig }: ViewM
// Note creation and marker relocation. Both are scoped to this map instance via local callbacks
// rather than global commands: embedded maps share no note context (no distinct ntxId), so a
- // broadcast command would arm placement mode on every map at once. The button and the marker's
+ // broadcast command would arm placement mode on every map at once. The edit bar and the marker's
// context menu are these callbacks' only triggers, so a direct handler keeps each isolated to the
// map that was clicked.
- const startNotePlacement = useCallback(() => setPlacement({ mode: "new" }), []);
+ //
+ // A toggle rather than an arming: the button on the edit bar shows the mode as held down, so
+ // pressing it again is the visible way out of it — the counterpart of the toast's Escape. It
+ // also takes over a map armed to move a marker, a press on + saying what the next click is for
+ // more plainly than whatever was armed before.
+ const toggleNotePlacement = useCallback(() => {
+ setPlacement((current) => current?.mode === "new" ? undefined : { mode: "new" });
+ }, []);
const startMarkerRelocation = useCallback((noteId: string) => setPlacement({ mode: "move", noteId }), []);
/**
@@ -202,18 +209,11 @@ export default function GeoView({ note, noteIds, viewConfig, saveConfig }: ViewM
return (
<div className={`geo-view ${placement ? "placing-note" : ""}`}>
+ {/* Only the lock at its end: adding a note lives on the map itself now (see
+ EditToolbar), where it survives the map going fullscreen without this bar. */}
<CollectionProperties
note={note}
- rightChildren={<>
- <ToggleReadOnlyButton note={note} />
- <ButtonOrActionButton
- icon="bx bx-plus"
- text={t("geo-map.create-child-note-text")}
- title={t("geo-map.create-child-note-title")}
- onClick={startNotePlacement}
- disabled={isReadOnly}
- />
- </>}
+ rightChildren={<ToggleReadOnlyButton note={note} />}
/>
{ coordinates !== undefined && zoom !== undefined && <Map
apiRef={apiRef} containerRef={containerRef}
@@ -229,6 +229,11 @@ export default function GeoView({ note, noteIds, viewConfig, saveConfig }: ViewM
scale={hasScale}
>
<MapToolbar />
+ <EditToolbar
+ isReadOnly={isReadOnly}
+ placing={placement?.mode === "new"}
+ onTogglePlacement={toggleNotePlacement}
+ />
<Tooltips />
{/* The preview under the pointer while a click is armed to mean a place — the note
being moved wearing its own pin, a note to be created wearing the pin it will be