Skip to content

Build your personal knowledge base with Trilium Notes

fix(geomap): put the marker preview away on click, and read during the wait

Elian Doran1371faa922b2
diff --git a/apps/client/src/widgets/collections/geomap/Tooltips.spec.tsx b/apps/client/src/widgets/collections/geomap/Tooltips.spec.tsx
index b3f03b12f7..eb2186c812 100644
--- a/apps/client/src/widgets/collections/geomap/Tooltips.spec.tsx
+++ b/apps/client/src/widgets/collections/geomap/Tooltips.spec.tsx
@@ -71,19 +71,30 @@ const { FakePopup } = vi.hoisted(() => {
vi.mock("maplibre-gl", () => ({ Popup: FakePopup }));
-/** A map that only delegates layer events, which is all this component asks of one. */
+/** A map that only delegates events — per layer or map-wide — which is all this component asks of one. */
function fakeMap() {
const listeners = new Map<string, Set<(e: unknown) => void>>();
const key = (event: string, layer: string) => `${event}:${layer}`;
+ // The two shapes MapLibre's `on`/`off` take: with a layer between event and listener, and without.
+ const unpack = (layerOrFn: string | ((e: unknown) => void), fn?: (e: unknown) => void) =>
+ typeof layerOrFn === "function" ? { layer: "", fn: layerOrFn } : { layer: layerOrFn, fn };
return {
- on(event: string, layer: string, fn: (e: unknown) => void) {
+ on(event: string, layerOrFn: string | ((e: unknown) => void), maybeFn?: (e: unknown) => void) {
+ const { layer, fn } = unpack(layerOrFn, maybeFn);
+ if (!fn) return;
const listenersForKey = listeners.get(key(event, layer)) ?? new Set();
listeners.set(key(event, layer), listenersForKey.add(fn));
},
- off(event: string, layer: string, fn: (e: unknown) => void) {
+ off(event: string, layerOrFn: string | ((e: unknown) => void), maybeFn?: (e: unknown) => void) {
+ const { layer, fn } = unpack(layerOrFn, maybeFn);
+ if (!fn) return;
listeners.get(key(event, layer))?.delete(fn);
},
+ /** A click landing anywhere on the map — a marker, a track or bare ground alike. */
+ click() {
+ for (const fn of listeners.get(key("click", "")) ?? []) fn({});
+ },
/** The pointer coming to rest on a marker, as MapLibre reports it. */
hover(note: FNote) {
const feature = {
@@ -168,11 +179,13 @@ describe("Tooltips", () => {
renderTooltip.mockClear();
});
- function mount(map: ReturnType<typeof fakeMap>) {
+ /** Mounts — or, called again, re-renders — the component. `selectedNoteId` is the marker the
+ * detail pane stands for, as the map view passes it down. */
+ function mount(map: ReturnType<typeof fakeMap>, selectedNoteId: string | null = null) {
return act(async () => {
render(
<ParentMap.Provider value={map as never}>
- <Tooltips />
+ <Tooltips selectedNoteId={selectedNoteId} />
</ParentMap.Provider>,
container as HTMLElement
);
@@ -186,11 +199,17 @@ describe("Tooltips", () => {
map.hover(note);
// A marker merely passed over is not a note worth reading.
- await advance(200);
+ await advance(100);
expect(shownPreview()).toBeUndefined();
expect(renderTooltip).not.toHaveBeenCalled();
- await advance(400);
+ // A marker rested on is read at once — but shown only once the full wait is up, so the
+ // round trip hides inside the wait instead of stretching it.
+ await advance(200);
+ expect(renderTooltip).toHaveBeenCalled();
+ expect(shownPreview()).toBeUndefined();
+
+ await advance(200);
expect(shownPreview()).toContain("Body of Somewhere");
// Drawn in the shape the app's tooltip styles are written against.
expect(FakePopup.open[0]?.html).toContain(`class="tooltip note-tooltip show"`);
@@ -275,6 +294,67 @@ describe("Tooltips", () => {
}
});
+ /**
+ * A click is always something being done — a marker opened into the pane, a place chosen — and
+ * the preview must not stand over the result. A preview whose marker was clicked used to stand
+ * exactly where the pane then opened, and one still on its way landed on top of it.
+ */
+ it("dismisses the preview, up or still on its way, when the map is clicked", async () => {
+ const note = buildNote({ title: "Somewhere", "#geolocation": "1,2" });
+ const map = fakeMap();
+ await mount(map);
+
+ map.hover(note);
+ await advance(500);
+ expect(shownPreview()).toContain("Body of Somewhere");
+
+ await act(async () => { map.click(); });
+ expect(shownPreview()).toBeUndefined();
+
+ // A preview already being read when the click lands never arrives at all.
+ map.hover(note);
+ await advance(300);
+ expect(renderTooltip).toHaveBeenCalledTimes(2);
+ await act(async () => { map.click(); });
+ await advance(1000);
+ expect(shownPreview()).toBeUndefined();
+ });
+
+ it("never previews the marker the detail pane already stands for", async () => {
+ const selected = buildNote({ title: "Somewhere", "#geolocation": "1,2" });
+ const other = buildNote({ title: "Elsewhere", "#geolocation": "3,4" });
+ const map = fakeMap();
+ await mount(map, selected.noteId);
+
+ // The pane beside it already says everything the preview would — the note is not even read.
+ map.hover(selected);
+ await advance(1000);
+ expect(shownPreview()).toBeUndefined();
+ expect(renderTooltip).not.toHaveBeenCalled();
+
+ // Every other marker keeps its preview while the pane is up.
+ map.hover(other);
+ await advance(500);
+ expect(shownPreview()).toContain("Body of Elsewhere");
+ });
+
+ /**
+ * Not every selection is made by a click on the map: a note just created is opened into the
+ * pane by the code that created it. The selection changing is a dismissal in itself.
+ */
+ it("takes the preview down when its marker is selected without a click", async () => {
+ const note = buildNote({ title: "Somewhere", "#geolocation": "1,2" });
+ const map = fakeMap();
+ await mount(map);
+
+ map.hover(note);
+ await advance(500);
+ expect(shownPreview()).toContain("Body of Somewhere");
+
+ await mount(map, note.noteId);
+ expect(shownPreview()).toBeUndefined();
+ });
+
it("closes the preview when the pointer leaves the marker without reaching it", async () => {
const note = buildNote({ title: "Somewhere", "#geolocation": "1,2" });
const map = fakeMap();
diff --git a/apps/client/src/widgets/collections/geomap/Tooltips.tsx b/apps/client/src/widgets/collections/geomap/Tooltips.tsx
index a0f297c4b5..0ff172020f 100644
--- a/apps/client/src/widgets/collections/geomap/Tooltips.tsx
+++ b/apps/client/src/widgets/collections/geomap/Tooltips.tsx
@@ -11,9 +11,21 @@ import { MARKER_HEIGHT, MARKER_LAYER, MARKER_WIDTH } from "./Markers";
/**
* How long the pointer has to rest on a marker before its note is read.
*
- * The same wait the tooltips everywhere else in the app make, and for the same reason: a preview is
- * a note's content, so dragging the pointer across a crowded map would otherwise read every note it
- * happens to brush past.
+ * A preview is a note's content, fetched from the server, so dragging the pointer across a crowded
+ * map would otherwise read every note it happens to brush past. Deliberately shorter than the
+ * {@link HOVER_DELAY} the preview itself sits out: the read begins part-way through that rest, so
+ * the round trip happens *during* the remainder of the wait rather than after it. Waiting the whole
+ * rest and only then reading is what made the preview arrive noticeably later here than anywhere
+ * else in the app, whose tooltips race the render against the delay (see note_tooltip.ts).
+ */
+const READ_DELAY = 150;
+
+/**
+ * How long the pointer has to rest on a marker before its preview is shown.
+ *
+ * The same wait the tooltips everywhere else in the app make: a preview flashing up under every
+ * passing pointer is flicker, not help. The note is read before this wait is over (see
+ * {@link READ_DELAY}), but shown no sooner than this however fast the read comes back.
*/
const HOVER_DELAY = 500;
@@ -76,8 +88,19 @@ const PREVIEW_OFFSET: Offset = {
* MapLibre popup rather than by the app's tooltip service because a marker is no longer an element:
* the notes are drawn into one symbol layer (see {@link Markers}), and the service works by hovering
* elements. What the layer gives us instead is the note behind the pixel under the pointer.
+ *
+ * Not every marker previews, and no preview outlives a click. The marker the detail pane stands for
+ * shows nothing — the pane beside it already says everything the preview would, at full length. And
+ * a click is always something being done — a marker opened into the pane, a place chosen, a pan
+ * begun — whose result the preview would otherwise stand over; the app's tooltip service dismisses
+ * on click for the same reason (see note_tooltip.ts). Without this, a preview whose marker was
+ * clicked stood exactly where the pane then opened, and one still on its way landed on top of it.
*/
-export default function Tooltips() {
+export default function Tooltips({ selectedNoteId }: {
+ /** The note the detail pane stands for, or `null` while the pane is down and every marker
+ * previews. See the selection in DetailPane. */
+ selectedNoteId: string | null;
+}) {
const map = useContext(ParentMap);
useEffect(() => {
@@ -104,15 +127,24 @@ export default function Tooltips() {
let shown: string | null = null;
// Whether the pointer is on the preview itself, which is what keeps it open.
let onTooltip = false;
+ // Whether this binding of the effect has been torn down, which is what stops a show still
+ // in flight from adding its popup to a map nothing manages any more.
+ let cancelled = false;
let showTimer: ReturnType<typeof setTimeout> | undefined;
let dismissTimer: ReturnType<typeof setTimeout> | undefined;
async function show(noteId: string, coordinates: [ number, number ]) {
- const note = await froca.getNote(noteId);
- const content = await renderTooltip(note);
- // A note with no content and no path renders to nothing, and the pointer may well have
- // moved on to another marker while this one was being read.
- if (!map || hovered !== noteId || !content || isHtmlEmpty(content)) return;
+ // The read raced against the rest of the wait rather than run after it: the preview
+ // holds to the full HOVER_DELAY however fast the note comes back, but a round trip no
+ // longer stretches that wait unless it outlasts what is left of it.
+ const [ content ] = await Promise.all([
+ froca.getNote(noteId).then((note) => renderTooltip(note)),
+ new Promise((resolve) => setTimeout(resolve, HOVER_DELAY - READ_DELAY))
+ ]);
+ // A note with no content and no path renders to nothing; the pointer may well have
+ // moved on to another marker while this one was being read; and a click may have
+ // dismissed the preview while it was still on its way (see dismiss).
+ if (cancelled || !map || hovered !== noteId || !content || isHtmlEmpty(content)) return;
shown = noteId;
tooltip
@@ -161,8 +193,25 @@ export default function Tooltips() {
// there wrong for as long as the new one takes to read.
if (shown && shown !== noteId) hide();
+ // The marker the pane stands for gets no preview: the pane beside it already says
+ // everything the preview would, at full length.
+ if (noteId === selectedNoteId) return;
+
const coordinates = feature.geometry.coordinates as [ number, number ];
- showTimer = setTimeout(() => show(noteId, coordinates), HOVER_DELAY);
+ showTimer = setTimeout(() => show(noteId, coordinates), READ_DELAY);
+ }
+
+ /**
+ * Puts the preview away — up or still on its way — because the map was clicked, whatever
+ * the click was for. Clearing `hovered` is what drops a show already past its timer, at
+ * the guard it shares with every other way of being overtaken; it also means the marker
+ * clicked does not re-arm until the pointer has left it and come back, which is the
+ * behaviour wanted of a marker that was just acted on.
+ */
+ function dismiss() {
+ hovered = null;
+ clearTimeout(dismissTimer);
+ hide();
}
function onMouseLeave() {
@@ -183,15 +232,23 @@ export default function Tooltips() {
map.on("mousemove", MARKER_LAYER, onMouseMove);
map.on("mouseleave", MARKER_LAYER, onMouseLeave);
+ // Bound to the map rather than to the marker layer: a click anywhere is something being
+ // done, and the preview goes whether the click landed on a pin, a track or bare ground.
+ map.on("click", dismiss);
return () => {
+ cancelled = true;
clearTimeout(showTimer);
clearTimeout(dismissTimer);
map.off("mousemove", MARKER_LAYER, onMouseMove);
map.off("mouseleave", MARKER_LAYER, onMouseLeave);
+ map.off("click", dismiss);
tooltip.remove();
};
- }, [ map ]);
+ // Depending on the selection makes every change of it a dismissal in itself, which covers
+ // the selections no click announces: a note created onto the map is opened into the pane
+ // by the code that created it (see index.tsx).
+ }, [ map, selectedNoteId ]);
return null;
}
diff --git a/apps/client/src/widgets/collections/geomap/index.tsx b/apps/client/src/widgets/collections/geomap/index.tsx
index 2d2f772ae9..8a22cd9573 100644
--- a/apps/client/src/widgets/collections/geomap/index.tsx
+++ b/apps/client/src/widgets/collections/geomap/index.tsx
@@ -252,7 +252,7 @@ export default function GeoView({ note, noteIds, viewConfig, saveConfig }: ViewM
onTogglePlacement={toggleNotePlacement}
onAddGpxTrack={addGpxTrack}
/>
- <Tooltips />
+ <Tooltips selectedNoteId={selection?.noteId ?? null} />
{/* 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
given (see api.ts). */}