Skip to content

Build your personal knowledge base with Trilium Notes

fix(geomap): keep a marker's preview off the marker it belongs to

Elian Doran74ac118f79f3
diff --git a/apps/client/src/widgets/collections/geomap/Markers.tsx b/apps/client/src/widgets/collections/geomap/Markers.tsx
index 7d559c4448..83ba307ea8 100644
--- a/apps/client/src/widgets/collections/geomap/Markers.tsx
+++ b/apps/client/src/widgets/collections/geomap/Markers.tsx
@@ -13,9 +13,14 @@ export const MARKER_LAYER = "points-layer";
export const MARKER_SOURCE = "points";
const DEFAULT_MARKER_COLOR = "#2A81CB";
-/** The pin, in the coordinates its SVG is drawn in. */
-const MARKER_WIDTH = 25;
-const MARKER_HEIGHT = 41;
+/**
+ * The pin, in the coordinates its SVG is drawn in.
+ *
+ * Exported because a pin stands on its coordinate rather than over it, so anything else the map puts
+ * at that coordinate has to know how much of the pin is in the way (see {@link Tooltips}).
+ */
+export const MARKER_WIDTH = 25;
+export const MARKER_HEIGHT = 41;
/**
* Room left around the pin for its shadow, in those same coordinates.
*
diff --git a/apps/client/src/widgets/collections/geomap/Tooltips.spec.tsx b/apps/client/src/widgets/collections/geomap/Tooltips.spec.tsx
index a6ffa8d8cf..b3f03b12f7 100644
--- a/apps/client/src/widgets/collections/geomap/Tooltips.spec.tsx
+++ b/apps/client/src/widgets/collections/geomap/Tooltips.spec.tsx
@@ -12,7 +12,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type FNote from "../../../entities/fnote";
import { buildNote } from "../../../test/easy-froca";
import { ParentMap } from "./map";
-import { MARKER_LAYER } from "./Markers";
+import { MARKER_HEIGHT, MARKER_LAYER, MARKER_WIDTH } from "./Markers";
import Tooltips from "./Tooltips";
/** What a note's preview renders to, standing in for the shared tooltip renderer. */
@@ -32,6 +32,11 @@ const { FakePopup } = vi.hoisted(() => {
html = "";
lngLat: unknown;
element: HTMLElement | null = null;
+ options: { offset?: Record<string, [ number, number ]> };
+
+ constructor(options: { offset?: Record<string, [ number, number ]> }) {
+ this.options = options;
+ }
setLngLat(lngLat: unknown) { this.lngLat = lngLat; return this; }
@@ -97,6 +102,34 @@ function fakeMap() {
};
}
+/** A preview about the size the app renders one at, for the placement arithmetic below. */
+const PREVIEW_WIDTH = 500;
+const PREVIEW_HEIGHT = 120;
+
+/** The pin, in pixels around the coordinate it stands on. */
+const PIN = {
+ left: -MARKER_WIDTH / 2,
+ right: MARKER_WIDTH / 2,
+ top: -MARKER_HEIGHT,
+ bottom: 0
+};
+
+/**
+ * Where each of MapLibre's placements puts the preview's top-left corner before the offset is
+ * applied — its `anchorTranslate` table, worked out for a preview of the size above. `center` is
+ * left out: MapLibre's own placement logic never arrives at it.
+ */
+const PLACEMENTS: Record<string, [ number, number ]> = {
+ "top": [ -PREVIEW_WIDTH / 2, 0 ],
+ "bottom": [ -PREVIEW_WIDTH / 2, -PREVIEW_HEIGHT ],
+ "left": [ 0, -PREVIEW_HEIGHT / 2 ],
+ "right": [ -PREVIEW_WIDTH, -PREVIEW_HEIGHT / 2 ],
+ "top-left": [ 0, 0 ],
+ "top-right": [ -PREVIEW_WIDTH, 0 ],
+ "bottom-left": [ 0, -PREVIEW_HEIGHT ],
+ "bottom-right": [ -PREVIEW_WIDTH, -PREVIEW_HEIGHT ]
+};
+
/** Drains the promise chain in `show` (note read → preview rendered) without moving the clock. */
async function flush() {
await act(async () => {
@@ -215,6 +248,33 @@ describe("Tooltips", () => {
expect(shownPreview()).toBeUndefined();
});
+ /**
+ * A pin stands on its coordinate, so every placement MapLibre can choose opens onto some part of
+ * it. Checked as the geometry it is rather than as a rule per axis: the first attempt cleared the
+ * pin's height, which reads as right until a corner placement — which spreads sideways from the
+ * point, not upwards — lies across half the pin anyway.
+ */
+ it("keeps clear of the pin wherever the map decides to put it", async () => {
+ const note = buildNote({ title: "Somewhere", "#geolocation": "1,2" });
+ const map = fakeMap();
+ await mount(map);
+
+ map.hover(note);
+ await advance(500);
+ const offset = FakePopup.open[0]?.options.offset;
+
+ for (const [ placement, corner ] of Object.entries(PLACEMENTS)) {
+ const [ offsetX, offsetY ] = offset?.[placement] ?? [ 0, 0 ];
+ const left = corner[0] + offsetX;
+ const top = corner[1] + offsetY;
+ const clear = left >= PIN.right || left + PREVIEW_WIDTH <= PIN.left
+ || top >= PIN.bottom || top + PREVIEW_HEIGHT <= PIN.top;
+
+ // Named in the assertion so a failure says which placement covers the pin.
+ expect(`${placement} is ${clear ? "clear of" : "over"} the pin`).toBe(`${placement} is clear of the pin`);
+ }
+ });
+
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 ac381c46fe..a0f297c4b5 100644
--- a/apps/client/src/widgets/collections/geomap/Tooltips.tsx
+++ b/apps/client/src/widgets/collections/geomap/Tooltips.tsx
@@ -1,4 +1,4 @@
-import { type MapGeoJSONFeature, type MapMouseEvent, Popup } from "maplibre-gl";
+import { type MapGeoJSONFeature, type MapMouseEvent, type Offset, Popup } from "maplibre-gl";
import { useContext, useEffect } from "preact/hooks";
import froca from "../../../services/froca";
@@ -6,7 +6,7 @@ import { renderTooltip } from "../../../services/note_tooltip";
import { sanitizeNoteContentHtml } from "../../../services/sanitize_content";
import { isHtmlEmpty } from "../../../services/utils";
import { ParentMap } from "./map";
-import { MARKER_LAYER } from "./Markers";
+import { MARKER_HEIGHT, MARKER_LAYER, MARKER_WIDTH } from "./Markers";
/**
* How long the pointer has to rest on a marker before its note is read.
@@ -26,6 +26,48 @@ const HOVER_DELAY = 500;
*/
const DISMISS_DELAY = 400;
+/** How much air is left between the pin and the preview, whichever side it lands on. */
+const MARKER_GAP = 8;
+
+/** What it takes to get past the pin sideways, and to get above it. */
+const CLEAR_SIDEWAYS = MARKER_WIDTH / 2 + MARKER_GAP;
+const CLEAR_ABOVE = MARKER_HEIGHT + MARKER_GAP;
+
+/**
+ * Where the preview sits, for each placement MapLibre might choose.
+ *
+ * A pin stands on its coordinate rather than over it: everything it draws is *above* that point, and
+ * within half its width either side. A preview pushed away from the point by one flat distance
+ * therefore covers the very marker it belongs to, so each placement has to clear whichever part of
+ * the pin it opens onto — which is not the same part each time, and is why MapLibre asks for the
+ * offset per placement at all (see the `popupOffsets` example in its `Popup` documentation).
+ *
+ * What opens onto what is easy to get wrong. Placed straight above the point the preview meets the
+ * pin's full height, and straight below it meets nothing. But a preview placed from a *corner* has
+ * that corner on the point and spreads sideways from it, so what stands in its way there is the
+ * pin's width, not its height — clearing only the height leaves the corner placements lying across
+ * whichever half of the pin they open towards.
+ */
+const PREVIEW_OFFSET: Offset = {
+ // Straight below the point, where the pin draws nothing.
+ "top": [ 0, MARKER_GAP ],
+ // Straight above it, with the whole pin standing in between.
+ "bottom": [ 0, -CLEAR_ABOVE ],
+ // Beside it: past the pin's width, and lifted to the middle of its height rather than left
+ // hanging off the tip.
+ "left": [ CLEAR_SIDEWAYS, -MARKER_HEIGHT / 2 ],
+ "right": [ -CLEAR_SIDEWAYS, -MARKER_HEIGHT / 2 ],
+ // From a corner, spreading sideways: past the pin's width in whichever direction it opens, and
+ // clear of the point itself vertically. Getting past the width is what does the work here, so
+ // there is no need to rise the pin's whole height as well and leave the preview stranded up in
+ // the air away from the marker it belongs to.
+ "top-left": [ CLEAR_SIDEWAYS, MARKER_GAP ],
+ "top-right": [ -CLEAR_SIDEWAYS, MARKER_GAP ],
+ "bottom-left": [ CLEAR_SIDEWAYS, -MARKER_GAP ],
+ "bottom-right": [ -CLEAR_SIDEWAYS, -MARKER_GAP ],
+ "center": [ 0, 0 ]
+};
+
/**
* The preview shown while the pointer rests on a marker.
*
@@ -44,7 +86,7 @@ export default function Tooltips() {
const tooltip = new Popup({
closeButton: false,
closeOnClick: false,
- offset: 12,
+ offset: PREVIEW_OFFSET,
// The preview brings its own width — `.tooltip-inner` caps it at 500px — and MapLibre's
// 240px default would squeeze a note's content into a column half that wide.
maxWidth: "none",