Skip to content

Build your personal knowledge base with Trilium Notes

feat(geomap): let a marker be taken off the map from its own pane

Elian Dorandbe50eb22f21
diff --git a/apps/client/src/widgets/collections/geomap/DetailPane.css b/apps/client/src/widgets/collections/geomap/DetailPane.css
index bef92b2bf6..909a4e8141 100644
--- a/apps/client/src/widgets/collections/geomap/DetailPane.css
+++ b/apps/client/src/widgets/collections/geomap/DetailPane.css
@@ -55,6 +55,13 @@
display: flex;
align-items: center;
gap: 4px;
+
+ /* Taking a marker off the map is the one thing in the row that writes rather than reads, so it
+ stands at the far end of it: away from the four the reader is reaching for over and over, and
+ not somewhere the pointer lands by being a moment early. */
+ .geo-detail-pane-remove {
+ margin-inline-start: auto;
+ }
}
/*
diff --git a/apps/client/src/widgets/collections/geomap/DetailPane.spec.tsx b/apps/client/src/widgets/collections/geomap/DetailPane.spec.tsx
index 8c311d2698..1d6434446f 100644
--- a/apps/client/src/widgets/collections/geomap/DetailPane.spec.tsx
+++ b/apps/client/src/widgets/collections/geomap/DetailPane.spec.tsx
@@ -5,6 +5,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import appContext from "../../../components/app_context";
import Component from "../../../components/component";
import type FNote from "../../../entities/fnote";
+import attributes from "../../../services/attributes";
import link from "../../../services/link";
import { buildNote } from "../../../test/easy-froca";
import { ParentComponent } from "../../react/react_utils";
@@ -93,12 +94,12 @@ describe("DetailPane", () => {
});
/** Renders into the same container, so calling it again is a re-render with fresh props. */
- function mount(notes: FNote[], map: ReturnType<typeof fakeMap>, placing = false) {
+ function mount(notes: FNote[], map: ReturnType<typeof fakeMap>, placing = false, isReadOnly = false) {
return act(async () => {
render(
<ParentComponent.Provider value={new Component()}>
<ParentMap.Provider value={map as never}>
- <DetailPane notes={notes} placing={placing} />
+ <DetailPane notes={notes} placing={placing} isReadOnly={isReadOnly} />
</ParentMap.Provider>
</ParentComponent.Provider>,
container as HTMLElement
@@ -239,9 +240,8 @@ describe("DetailPane", () => {
});
describe("the ways of opening it", () => {
- /** Opens the pane on a note that hangs somewhere, so that there is a path to open. */
- async function openPaneFor(note: FNote, map: ReturnType<typeof fakeMap>) {
- await mount([ note ], map);
+ async function openPaneFor(note: FNote, map: ReturnType<typeof fakeMap>, isReadOnly = false) {
+ await mount([ note ], map, false, isReadOnly);
map.setUnderPointer([ markerFeature(note) ]);
await act(async () => map.click());
}
@@ -290,6 +290,52 @@ describe("DetailPane", () => {
});
});
+ describe("taking the marker off the map", () => {
+ function removeButton() {
+ return container?.querySelector<HTMLButtonElement>(".geo-detail-pane-actions button.bx-trash") ?? null;
+ }
+
+ /**
+ * Only the note's location goes — the note itself stays where it is in the tree — and the
+ * pane goes with it, there being no marker left for it to stand for.
+ */
+ it("clears the note's location and stands down", async () => {
+ const note = buildNote({ title: "Somewhere", "#geolocation": "1,2" });
+ const map = fakeMap();
+ const setLabel = vi.spyOn(attributes, "setLabel").mockResolvedValue(undefined);
+
+ try {
+ await mount([ note ], map);
+ map.setUnderPointer([ markerFeature(note) ]);
+ await act(async () => map.click());
+
+ await act(async () => { removeButton()?.click(); });
+ expect(setLabel).toHaveBeenCalledWith(note.noteId, "geolocation", "");
+
+ // What the server would send back, which is the note carrying nowhere to be drawn.
+ vi.spyOn(note, "getLabelValue").mockReturnValue(null);
+ await mount([ note ], map);
+ expect(pane()).toBeNull();
+ } finally {
+ setLabel.mockRestore();
+ }
+ });
+
+ it("is not offered at all on a map that cannot be edited", async () => {
+ const note = buildNote({ title: "Somewhere", "#geolocation": "1,2" });
+ const map = fakeMap();
+
+ await mount([ note ], map, false, true);
+ map.setUnderPointer([ markerFeature(note) ]);
+ await act(async () => map.click());
+
+ // The ways of reading the note stay; the one that writes it does not.
+ expect(pane()).toBeTruthy();
+ expect(container?.querySelector(".geo-detail-pane-actions button.bx-log-in")).toBeTruthy();
+ expect(removeButton()).toBeNull();
+ });
+ });
+
it("keeps clicks and key presses from reaching the map underneath", async () => {
const note = buildNote({ title: "Somewhere", "#geolocation": "1,2" });
const map = fakeMap();
diff --git a/apps/client/src/widgets/collections/geomap/DetailPane.tsx b/apps/client/src/widgets/collections/geomap/DetailPane.tsx
index 2c3370e58f..2e60ed96f3 100644
--- a/apps/client/src/widgets/collections/geomap/DetailPane.tsx
+++ b/apps/client/src/widgets/collections/geomap/DetailPane.tsx
@@ -11,6 +11,7 @@ import link from "../../../services/link";
import ActionButton from "../../react/ActionButton";
import { useNoteIcon, useNoteLabel, useNoteProperty } from "../../react/hooks";
import OverlayPanel, { OverlayPanelBody, OverlayPanelTitle } from "../../react/OverlayPanel";
+import { moveMarker } from "./api";
import { ParentMap } from "./map";
import { LOCATION_ATTRIBUTE, MARKER_LAYER, parseLocation } from "./Markers";
@@ -25,10 +26,12 @@ import { LOCATION_ATTRIBUTE, MARKER_LAYER, parseLocation } from "./Markers";
* what is left uncovered rather than to the middle of the map (see {@link paneOffset}), and the bar
* of buttons in the corner steps aside for it (see DetailPane.css).
*/
-export default function DetailPane({ notes, placing }: {
+export default function DetailPane({ notes, placing, isReadOnly }: {
notes: FNote[];
/** A marker is being placed, which is what the next click on the map is for. */
placing: boolean;
+ /** The map may not be edited, which leaves the pane the ways of opening a note and no more. */
+ isReadOnly: boolean;
}) {
const map = useContext(ParentMap);
const [ selectedNoteId, setSelectedNoteId ] = useState<string | null>(null);
@@ -94,7 +97,7 @@ export default function DetailPane({ notes, placing }: {
return null;
}
- return <MarkerDetails note={note} onClose={() => setSelectedNoteId(null)} />;
+ return <MarkerDetails note={note} isReadOnly={isReadOnly} onClose={() => setSelectedNoteId(null)} />;
}
/**
@@ -137,7 +140,7 @@ function paneOffset(map: MapLibreGLMap): [number, number] {
}
/** The pane itself, for a marker there is one to draw. */
-function MarkerDetails({ note, onClose }: { note: FNote; onClose(): void }) {
+function MarkerDetails({ note, isReadOnly, onClose }: { note: FNote; isReadOnly: boolean; onClose(): void }) {
const icon = useNoteIcon(note);
const title = useNoteProperty(note, "title");
@@ -148,7 +151,7 @@ function MarkerDetails({ note, onClose }: { note: FNote; onClose(): void }) {
close={{ text: t("geo-map.close-details"), onClick: onClose }}
>
<OverlayPanelBody>
- <MarkerActions note={note} />
+ <MarkerActions note={note} isReadOnly={isReadOnly} />
</OverlayPanelBody>
</OverlayPanel>
);
@@ -163,7 +166,7 @@ function MarkerDetails({ note, onClose }: { note: FNote; onClose(): void }) {
* the quick editor: it used to be what a click on a marker raised, and the pane took that click
* over, so offering it here would only put a modal back over the pane that replaced it.
*/
-function MarkerActions({ note }: { note: FNote }) {
+function MarkerActions({ note, isReadOnly }: { note: FNote; isReadOnly: boolean }) {
const [ location ] = useNoteLabel(note, LOCATION_ATTRIBUTE);
const hoistedNoteId = appContext.tabManager.getActiveContext()?.hoistedNoteId;
// The path the note is best reached by from wherever the reader is hoisted, rather than its id:
@@ -204,6 +207,25 @@ function MarkerActions({ note }: { note: FNote }) {
onClick={() => latLng && link.goToLinkExt(null, `geo:${latLng[1]},${latLng[0]}`)}
disabled={!latLng}
/>
+
+ {/* Left out rather than shown spent on a map that cannot be edited, as the map's own
+ right-click menu leaves it out: the rest of the row reads a note, and this is the
+ one thing here that writes one. */}
+ {!isReadOnly && (
+ <ActionButton
+ className="geo-detail-pane-remove"
+ icon="bx bx-trash"
+ text={t("geo-map-context.remove-from-map")}
+ // Only the note's location goes; the note itself stays exactly where it was in
+ // the tree. Nothing closes the pane afterwards because nothing has to — a note
+ // with nowhere to be drawn is a note the pane stops standing for (see above).
+ //
+ // Called rather than commanded, though `deleteFromMap` is a command the map
+ // already answers: a command is broadcast, and every geo map open at the time
+ // would take the same note off the same map in turn.
+ onClick={() => void moveMarker(note.noteId, null)}
+ />
+ )}
</div>
);
}
diff --git a/apps/client/src/widgets/collections/geomap/index.tsx b/apps/client/src/widgets/collections/geomap/index.tsx
index e4f57e3dc4..9ab18de176 100644
--- a/apps/client/src/widgets/collections/geomap/index.tsx
+++ b/apps/client/src/widgets/collections/geomap/index.tsx
@@ -208,7 +208,7 @@ export default function GeoView({ note, noteIds, viewConfig, saveConfig }: ViewM
>
<MapToolbar />
<Tooltips />
- <DetailPane notes={notes} placing={!!placement} />
+ <DetailPane notes={notes} placing={!!placement} isReadOnly={isReadOnly} />
<ContextMenus note={note} isReadOnly={isReadOnly} onRelocate={startMarkerRelocation} />
{/* The pane above is what a click on a marker opens now, so the markers no longer
open the note themselves — the two would otherwise both answer the same click,