Skip to content

Build your personal knowledge base with Trilium Notes

feat(geomap): light up the marker the detail pane stands for

Elian Doran758594849c7b
diff --git a/apps/client/src/widgets/collections/geomap/Markers.spec.tsx b/apps/client/src/widgets/collections/geomap/Markers.spec.tsx
index 0ea128c71e..7b55a9c975 100644
--- a/apps/client/src/widgets/collections/geomap/Markers.spec.tsx
+++ b/apps/client/src/widgets/collections/geomap/Markers.spec.tsx
@@ -23,14 +23,17 @@ import { buildNote } from "../../../test/easy-froca";
import { ParentComponent } from "../../react/react_utils";
import { CLUSTER_COUNT_LAYER, CLUSTER_LAYER } from "./clusters";
import { ParentMap } from "./map";
-import Markers, { formatLocation, MARKER_LAYER, MARKER_SOURCE, parseLocation } from "./Markers";
+import Markers, { formatLocation, MARKER_LAYER, MARKER_SOURCE, parseLocation, SELECTION_LAYER } from "./Markers";
vi.mock("../../../services/icon_glyphs", () => ({
renderIconImage: vi.fn(async () => "data:image/svg+xml;base64,PHN2Zz48L3N2Zz4=")
}));
-/** What a map that gathers its notes puts up: the pins, the bubbles, and the bubbles' counts. */
-const CLUSTERED_LAYER_COUNT = 3;
+/** What every map puts up: the pins, and the glow that lights whichever of them is selected. */
+const BASE_LAYER_COUNT = 2;
+
+/** What a map that gathers its notes adds to that: the bubbles, and the bubbles' counts. */
+const CLUSTERED_LAYER_COUNT = BASE_LAYER_COUNT + 2;
interface FakeLayer {
id: string;
@@ -139,7 +142,10 @@ function fakeMap() {
},
removeLayer(id: string) { layers.delete(id); calls.removeLayer++; },
setLayoutProperty(id: string, name: string, value: unknown) { properties.set(`${id}/${name}`, value); },
- setPaintProperty(id: string, name: string, value: unknown) { properties.set(`${id}/${name}`, value); }
+ setPaintProperty(id: string, name: string, value: unknown) { properties.set(`${id}/${name}`, value); },
+ // Filed with the properties: what a layer's filter has been repointed to is read the same
+ // way as what it has been repainted with.
+ setFilter(id: string, filter: unknown) { properties.set(`${id}/filter`, filter); }
};
}
@@ -193,7 +199,7 @@ describe("Markers", () => {
});
/** Renders into the same container, so calling it again is a re-render with fresh props. */
- function mount(notes: FNote[], map: ReturnType<typeof fakeMap>, parent: Component, look?: { hideLabels?: boolean, isDarkTheme?: boolean, clustered?: boolean, placing?: boolean, opensNotes?: boolean }) {
+ function mount(notes: FNote[], map: ReturnType<typeof fakeMap>, parent: Component, look?: { hideLabels?: boolean, isDarkTheme?: boolean, clustered?: boolean, placing?: boolean, opensNotes?: boolean, selectedNoteId?: string | null }) {
return act(async () => {
render(
<ParentComponent.Provider value={parent}>
@@ -208,6 +214,7 @@ describe("Markers", () => {
// map itself passes false, a marker being opened into the detail pane
// there instead (see index.tsx).
opensNotes={look?.opensNotes ?? true}
+ selectedNoteId={look?.selectedNoteId ?? null}
/>
</ParentMap.Provider>
</ParentComponent.Provider>,
@@ -229,7 +236,7 @@ describe("Markers", () => {
});
expect(map.getLayer(MARKER_LAYER)).toBeTruthy();
- expect(map.calls.addLayer).toBe(1);
+ expect(map.calls.addLayer).toBe(BASE_LAYER_COUNT);
expect(map.lastFeatures).toHaveLength(1);
const setDataBefore = map.calls.setData;
@@ -245,7 +252,7 @@ describe("Markers", () => {
// The layer stood throughout — it was handed fresh data rather than torn down and remade.
expect(map.calls.removeLayer).toBe(0);
expect(map.calls.removeSource).toBe(0);
- expect(map.calls.addLayer).toBe(1);
+ expect(map.calls.addLayer).toBe(BASE_LAYER_COUNT);
expect(map.calls.setData).toBeGreaterThan(setDataBefore);
expect(map.lastFeatures).toHaveLength(1);
});
@@ -274,7 +281,7 @@ describe("Markers", () => {
// The layer stood throughout, and the markers were never built again for it.
expect(map.calls.removeLayer).toBe(0);
expect(map.calls.removeSource).toBe(0);
- expect(map.calls.addLayer).toBe(1);
+ expect(map.calls.addLayer).toBe(BASE_LAYER_COUNT);
expect(map.calls.setData).toBe(setDataBefore);
expect(map.property(MARKER_LAYER, "text-color")).toBe("#fff");
@@ -388,7 +395,7 @@ describe("Markers", () => {
expect(map.source(MARKER_SOURCE)).not.toMatchObject({ cluster: true });
expect(map.layer(CLUSTER_LAYER)).toBeUndefined();
expect(map.layer(CLUSTER_COUNT_LAYER)).toBeUndefined();
- expect(map.calls.addLayer).toBe(1);
+ expect(map.calls.addLayer).toBe(BASE_LAYER_COUNT);
// The pin layer keeps its filter either way — a source that gathers nothing produces no
// group for it to hide, so it passes every note through.
expect(map.lastFeatures).toHaveLength(2);
@@ -436,13 +443,13 @@ describe("Markers", () => {
map.fireStyleLoad();
await settle();
});
- expect(map.calls.addLayer).toBe(1);
+ expect(map.calls.addLayer).toBe(BASE_LAYER_COUNT);
await act(async () => {
render(null, container as HTMLElement);
});
- expect(map.calls.removeLayer).toBe(1);
+ expect(map.calls.removeLayer).toBe(BASE_LAYER_COUNT);
expect(map.calls.removeSource).toBe(1);
});
@@ -465,7 +472,7 @@ describe("Markers", () => {
map.fireStyleLoad();
await settle();
});
- expect(map.calls.addLayer).toBe(1);
+ expect(map.calls.addLayer).toBe(BASE_LAYER_COUNT);
map.remove();
@@ -545,6 +552,89 @@ describe("Markers", () => {
expect(map.cursor).toBe("");
});
});
+
+ /**
+ * The map's half of the detail pane's selection: the chosen pin is grown, drawn above its
+ * neighbours, and lit from below by the glow layer. All of it property updates on standing
+ * layers — choosing a marker must never take every other marker off the map and back, for the
+ * same reason a note being recoloured must not (see the redraw test above).
+ */
+ describe("highlighting the selected marker", () => {
+ it("points the highlight at the marker, and away again when nothing is selected", async () => {
+ const note = buildNote({ title: "Somewhere", "#geolocation": "1,2" });
+ const map = fakeMap();
+ const parent = new Component();
+
+ await mount([ note ], map, parent);
+ await act(async () => {
+ map.fireStyleLoad();
+ await settle();
+ });
+
+ // The glow stands from the start, aimed at nothing: no note's id is the empty string.
+ expect(map.layer(SELECTION_LAYER)?.filter).toEqual([ "==", [ "get", "id" ], "" ]);
+ expect(map.layer(MARKER_LAYER)?.layout?.["icon-size"]).toBe(1);
+ const layersBefore = map.calls.addLayer;
+
+ // A marker is selected: everything is repointed rather than rebuilt.
+ await mount([ note ], map, parent, { selectedNoteId: note.noteId });
+ expect(map.calls.addLayer).toBe(layersBefore);
+ expect(map.property(SELECTION_LAYER, "filter")).toEqual([ "==", [ "get", "id" ], note.noteId ]);
+ expect(map.property(MARKER_LAYER, "icon-size"))
+ .toEqual([ "case", [ "==", [ "get", "id" ], note.noteId ], expect.any(Number), 1 ]);
+ expect(map.property(MARKER_LAYER, "symbol-sort-key"))
+ .toEqual([ "case", [ "==", [ "get", "id" ], note.noteId ], 1, 0 ]);
+
+ // The glow keeps the titles' bargain, so it changes sides with them.
+ expect(map.layer(SELECTION_LAYER)?.paint?.["circle-color"]).toBe("rgba(0, 0, 0, 0.35)");
+ await mount([ note ], map, parent, { selectedNoteId: note.noteId, isDarkTheme: true });
+ expect(map.property(SELECTION_LAYER, "circle-color")).toBe("rgba(255, 255, 255, 0.4)");
+
+ // The pane closes, and the map stops pointing at anything.
+ await mount([ note ], map, parent, { isDarkTheme: true });
+ expect(map.property(SELECTION_LAYER, "filter")).toEqual([ "==", [ "get", "id" ], "" ]);
+ expect(map.property(MARKER_LAYER, "icon-size")).toBe(1);
+ });
+
+ /**
+ * A style switch tears every layer down and `install` puts fresh ones up, reading the
+ * selection from a ref rather than depending on it — a layer added while the pane is open
+ * has to come up already highlighted, not wait for a selection change that may never come.
+ */
+ it("gives a layer added with a marker already selected the highlight built in", async () => {
+ const note = buildNote({ title: "Somewhere", "#geolocation": "1,2" });
+ const map = fakeMap();
+
+ await mount([ note ], map, new Component(), { selectedNoteId: note.noteId });
+ await act(async () => {
+ map.fireStyleLoad();
+ await settle();
+ });
+
+ expect(map.layer(SELECTION_LAYER)?.filter).toEqual([ "==", [ "get", "id" ], note.noteId ]);
+ expect(map.layer(MARKER_LAYER)?.layout?.["icon-size"])
+ .toEqual([ "case", [ "==", [ "get", "id" ], note.noteId ], expect.any(Number), 1 ]);
+ });
+
+ it("keeps saying the selected marker's title while the rest are hidden", async () => {
+ const note = buildNote({ title: "Somewhere", "#geolocation": "1,2" });
+ const map = fakeMap();
+ const parent = new Component();
+
+ await mount([ note ], map, parent, { hideLabels: true });
+ await act(async () => {
+ map.fireStyleLoad();
+ await settle();
+ });
+ expect(map.layer(MARKER_LAYER)?.layout?.["text-field"]).toBe("");
+
+ // A pane discussing a note whose name the map refuses to utter would leave the
+ // highlight pointing at an anonymous pin.
+ await mount([ note ], map, parent, { hideLabels: true, selectedNoteId: note.noteId });
+ expect(map.property(MARKER_LAYER, "text-field"))
+ .toEqual([ "case", [ "==", [ "get", "id" ], note.noteId ], [ "get", "name" ], "" ]);
+ });
+ });
});
/**
diff --git a/apps/client/src/widgets/collections/geomap/Markers.tsx b/apps/client/src/widgets/collections/geomap/Markers.tsx
index 951468a2c7..27f86806ff 100644
--- a/apps/client/src/widgets/collections/geomap/Markers.tsx
+++ b/apps/client/src/widgets/collections/geomap/Markers.tsx
@@ -1,4 +1,4 @@
-import { AddLayerObject, type GeoJSONSource, type MapGeoJSONFeature, type Map as MapLibreGLMap, type MapMouseEvent } from "maplibre-gl";
+import { AddLayerObject, type ExpressionSpecification, type GeoJSONSource, type MapGeoJSONFeature, type Map as MapLibreGLMap, type MapMouseEvent } from "maplibre-gl";
import { useCallback, useContext, useEffect, useRef, useState } from "preact/hooks";
import appContext from "../../../components/app_context";
@@ -12,6 +12,8 @@ import { ParentMap } from "./map";
export const LOCATION_ATTRIBUTE = "geolocation";
export const MARKER_LAYER = "points-layer";
export const MARKER_SOURCE = "points";
+/** The glow put under the selected marker, drawn from the same source beneath the pins. */
+export const SELECTION_LAYER = "selection-layer";
export const DEFAULT_MARKER_COLOR = "#2A81CB";
/**
@@ -80,6 +82,30 @@ export const LABEL_PAINT = {
*/
const HIDDEN_TEXT_FIELD = "";
+/**
+ * How much bigger the selected pin is drawn than its neighbours.
+ *
+ * A layout scale on the standing layer rather than a second image: the pin is rasterized per colour
+ * and icon, and a "selected" variant apiece would double that work for the sake of one marker at a
+ * time. The `icon-offset` compensating for the shadow padding is multiplied by `icon-size`, so the
+ * grown pin keeps standing on its coordinate with no further arithmetic.
+ */
+const SELECTED_PIN_SCALE = 1.3;
+
+/**
+ * The glow itself: a blurred disc at the selected pin's tip, so the marker the pane stands for reads
+ * as standing in a spotlight. Dark over a light map and light over a dark one, striking the same
+ * bargain as the titles (see {@link LABEL_PAINT}) — a hue of its own would vanish behind any pin
+ * that happened to share it.
+ */
+const SELECTION_GLOW_PAINT = {
+ light: { "circle-color": "rgba(0, 0, 0, 0.35)" },
+ dark: { "circle-color": "rgba(255, 255, 255, 0.4)" }
+};
+
+/** The glow's reach in screen pixels, a little wider than the grown pin standing over it. */
+const SELECTION_GLOW_RADIUS = 18;
+
/**
* Every note that carries a location, drawn as one symbol layer.
*
@@ -107,9 +133,15 @@ interface MarkersProps {
* — should one ever be wanted — still opens its notes the way it always did.
*/
opensNotes: boolean;
+ /**
+ * The note whose marker the detail pane stands for, or `null` while the pane is down. Its pin
+ * is drawn grown, above its neighbours and lit from below, so the map and the pane visibly
+ * agree on which place is being talked about.
+ */
+ selectedNoteId: string | null;
}
-export default function Markers({ notes, hideLabels, isDarkTheme, clustered, placing, opensNotes }: MarkersProps) {
+export default function Markers({ notes, hideLabels, isDarkTheme, clustered, placing, opensNotes, selectedNoteId }: MarkersProps) {
const map = useContext(ParentMap);
const version = useNoteChangeVersion(notes);
// Whether the style has finished loading at least once. Held outside the effects because either
@@ -125,6 +157,11 @@ export default function Markers({ notes, hideLabels, isDarkTheme, clustered, pla
// one already standing is repainted by the effect at the end.
const labelStyle = useRef({ hideLabels, isDarkTheme });
labelStyle.current = { hideLabels, isDarkTheme };
+ // Which marker is selected, read rather than depended on for the same reason as the titles'
+ // look: selecting one is a repaint of the standing layers (see the effect at the end), not a
+ // rebuild of them. Only a layer being added fresh reads this.
+ const selected = useRef(selectedNoteId);
+ selected.current = selectedNoteId;
/**
* Puts the layer and its data on the map. A style is a world of its own — switching one wipes
@@ -168,7 +205,12 @@ export default function Markers({ notes, hideLabels, isDarkTheme, clustered, pla
filter: UNCLUSTERED_ONLY,
layout: {
"icon-image": [ "get", "icon" ],
- "icon-size": 1,
+ "icon-size": selectionPinSize(selected.current),
+ // The selected pin drawn above its neighbours, which a grown pin half-buried
+ // under them would undo. Placement runs in the other order, so the selected
+ // title is also the likeliest to lose a contest — bearable, the pane spelling
+ // the title out anyway.
+ "symbol-sort-key": selectionSortKey(selected.current),
"icon-anchor": "bottom",
// The image carries padding for the shadow, so its bottom edge sits below
// the pin's tip. Push it back down by exactly that much, or every marker
@@ -181,7 +223,7 @@ export default function Markers({ notes, hideLabels, isDarkTheme, clustered, pla
// laid over one.
"icon-allow-overlap": true,
...LABEL_LAYOUT,
- ...(labelStyle.current.hideLabels ? { "text-field": HIDDEN_TEXT_FIELD } : {})
+ "text-field": titleField(labelStyle.current.hideLabels, selected.current)
},
paint: {
// Archived notes are drawn faintly, as they were when each marker was an
@@ -195,6 +237,26 @@ export default function Markers({ notes, hideLabels, isDarkTheme, clustered, pla
});
}
+ // The glow slides in beneath the pins — added after them, so there is a layer to name as
+ // the one to go under. It stands whether or not anything is selected: pointing its filter
+ // at the selection is a property set on a standing layer, the discipline everything else
+ // here follows.
+ if (!map.getLayer(SELECTION_LAYER)) {
+ map.addLayer({
+ id: SELECTION_LAYER,
+ type: "circle",
+ source: MARKER_SOURCE,
+ filter: isSelected(selected.current),
+ paint: {
+ "circle-radius": SELECTION_GLOW_RADIUS,
+ // Faded from the centre outwards, so it reads as light on the ground rather
+ // than as a disc the pin stands on.
+ "circle-blur": 1,
+ ...SELECTION_GLOW_PAINT[labelStyle.current.isDarkTheme ? "dark" : "light"]
+ }
+ }, MARKER_LAYER);
+ }
+
if (clustered) {
installClusterLayers(map, MARKER_SOURCE);
}
@@ -252,7 +314,7 @@ export default function Markers({ notes, hideLabels, isDarkTheme, clustered, pla
// The layers before the source they all draw from: a source still in use cannot be
// removed.
- for (const layer of [ MARKER_LAYER, ...CLUSTER_LAYERS ]) {
+ for (const layer of [ MARKER_LAYER, SELECTION_LAYER, ...CLUSTER_LAYERS ]) {
if (map.getLayer(layer)) {
map.removeLayer(layer);
}
@@ -288,12 +350,28 @@ export default function Markers({ notes, hideLabels, isDarkTheme, clustered, pla
useEffect(() => {
if (!map?.getLayer(MARKER_LAYER)) return;
- map.setLayoutProperty(MARKER_LAYER, "text-field",
- hideLabels ? HIDDEN_TEXT_FIELD : LABEL_LAYOUT?.["text-field"]);
+ map.setLayoutProperty(MARKER_LAYER, "text-field", titleField(hideLabels, selectedNoteId));
for (const [ property, value ] of Object.entries(LABEL_PAINT[isDarkTheme ? "dark" : "light"])) {
map.setPaintProperty(MARKER_LAYER, property, value);
}
- }, [ map, hideLabels, isDarkTheme ]);
+ // The glow keeps the same bargain the titles do, so it changes sides with them.
+ for (const [ property, value ] of Object.entries(SELECTION_GLOW_PAINT[isDarkTheme ? "dark" : "light"])) {
+ map.setPaintProperty(SELECTION_LAYER, property, value);
+ }
+ }, [ map, hideLabels, isDarkTheme, selectedNoteId ]);
+
+ // The selected marker, told apart on the standing layers: its pin grown and raised above its
+ // neighbours, the glow's filter pointed at it. Property updates for the same reason as the
+ // titles' look — selecting a marker must not take every marker off the map and back. Nothing to
+ // do until the layers are up, since ones added after this have the selection built in (see
+ // `install`).
+ useEffect(() => {
+ if (!map?.getLayer(MARKER_LAYER)) return;
+
+ map.setLayoutProperty(MARKER_LAYER, "icon-size", selectionPinSize(selectedNoteId));
+ map.setLayoutProperty(MARKER_LAYER, "symbol-sort-key", selectionSortKey(selectedNoteId));
+ map.setFilter(SELECTION_LAYER, isSelected(selectedNoteId));
+ }, [ map, selectedNoteId ]);
useClusterExpansion(map, MARKER_SOURCE, clustered && !placing);
useMarkerOpening(map, opensNotes && !placing);
@@ -301,6 +379,40 @@ export default function Markers({ notes, hideLabels, isDarkTheme, clustered, pla
return null;
}
+/**
+ * Whether a feature is the note the detail pane stands for.
+ *
+ * Matches nothing when nothing is selected — no note's id is the empty string — which is what lets
+ * the glow layer stand permanently and be pointed by filter rather than added and taken away. A
+ * cluster carries no `id` of its own, so a bubble never matches even while it holds the selected
+ * note: a marker folded into one simply goes unhighlighted until the map is near enough to show it.
+ */
+function isSelected(selectedNoteId: string | null): ExpressionSpecification {
+ return [ "==", [ "get", "id" ], selectedNoteId ?? "" ];
+}
+
+/** The selected pin grown and every other left alone; the plain size where nothing is selected. */
+function selectionPinSize(selectedNoteId: string | null): ExpressionSpecification | number {
+ return selectedNoteId ? [ "case", isSelected(selectedNoteId), SELECTED_PIN_SCALE, 1 ] : 1;
+}
+
+/** The selected pin sorted above its neighbours, higher keys being drawn later and so on top. */
+function selectionSortKey(selectedNoteId: string | null): ExpressionSpecification | number {
+ return selectedNoteId ? [ "case", isSelected(selectedNoteId), 1, 0 ] : 0;
+}
+
+/**
+ * What the titles say: all of them, none of them — or, with the titles hidden, the selected one
+ * alone. A pane discussing a note whose name the map refuses to utter would leave the highlight
+ * pointing at an anonymous pin.
+ */
+function titleField(hideLabels: boolean, selectedNoteId: string | null) {
+ if (!hideLabels) return LABEL_LAYOUT?.["text-field"];
+ return selectedNoteId
+ ? [ "case", isSelected(selectedNoteId), [ "get", "name" ], HIDDEN_TEXT_FIELD ] as ExpressionSpecification
+ : HIDDEN_TEXT_FIELD;
+}
+
/**
* Opening a note by clicking its marker, which is what a marker is for.
*
diff --git a/apps/client/src/widgets/collections/geomap/index.tsx b/apps/client/src/widgets/collections/geomap/index.tsx
index 0a55aa0bc5..3240b36d4b 100644
--- a/apps/client/src/widgets/collections/geomap/index.tsx
+++ b/apps/client/src/widgets/collections/geomap/index.tsx
@@ -239,7 +239,7 @@ export default function GeoView({ note, noteIds, viewConfig, saveConfig }: ViewM
{/* 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,
raising the quick editor over the pane that had just opened behind it. */}
- <Markers notes={notes} hideLabels={hideLabels} isDarkTheme={layerData.isDarkTheme ?? false} clustered={clustered} placing={!!placement} opensNotes={false} />
+ <Markers notes={notes} hideLabels={hideLabels} isDarkTheme={layerData.isDarkTheme ?? false} clustered={clustered} placing={!!placement} opensNotes={false} selectedNoteId={selection?.noteId ?? null} />
{notes.map(note => <NoteGpxTrackWrapper note={note} hideLabels={hideLabels} isDarkTheme={layerData.isDarkTheme ?? false} />)}
</Map>}
</div>