Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(geomap): search the map's own markers, and gate the geocoder behind a row
Elian Doran567d44fe2f80
diff --git a/apps/client/src/test/setup.ts b/apps/client/src/test/setup.tsindex 30d037bf68..0830312f5b 100644--- a/apps/client/src/test/setup.ts+++ b/apps/client/src/test/setup.ts@@ -35,7 +35,10 @@ function mockWebsocket() {// consumers also import these as named exports (e.g. useNoteIds); leaving them out makes// the subscription effect throw, which silently skips every later effect of the componentsubscribeToMessages,- unsubscribeToMessage+ unsubscribeToMessage,+ // Code that reports a failure this way is usually in a catch block, so an undefined export+ // here throws over the error being handled and loses whatever the component did about it.+ logError(_message: string) {}};}diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 90c8e3e9dd..234428c9ec 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2955,7 +2955,11 @@"marker-color": "Marker color","copy-coordinates": "Copy coordinates","search": "Search",- "search-placeholder": "Search for a place"+ "search-placeholder": "Search for a place",+ "search-online": "Search online for \"{{- query}}\"",+ "searching-online": "Searching online…",+ "no-places-found": "No places found",+ "search-failed": "Couldn't search for places"},"embedded_note": {"open-note": "Open note",diff --git a/apps/client/src/widgets/collections/geomap/SearchBox.css b/apps/client/src/widgets/collections/geomap/SearchBox.cssindex 4ea7a7b699..2a566bed77 100644--- a/apps/client/src/widgets/collections/geomap/SearchBox.css+++ b/apps/client/src/widgets/collections/geomap/SearchBox.css@@ -38,3 +38,31 @@ body.mobile .geo-map-container .geo-search-toolbar {color: inherit;opacity: 0.6;}++/* Every row reads as icon + label, whether it offers a marker, a place, the geocoder or a report on+ it. Scoped to the portalled dropdown, which is a body child rather than part of the map. */+.form-autocomplete-dropdown .geo-search-entry {+ display: flex;+ align-items: center;+ gap: 6px;+ overflow: hidden;+}++.form-autocomplete-dropdown .geo-search-entry .tn-icon {+ flex-shrink: 0;+ opacity: 0.7;+}++/* The geocoder row and its reports are set apart from the places they produce. */+.form-autocomplete-dropdown .geo-search-entry-geocode,+.form-autocomplete-dropdown .geo-search-entry-info {+ font-style: italic;+ opacity: 0.85;+}++/* A report is not a choice, so it neither highlights nor takes a pointer. */+.form-autocomplete-dropdown .form-autocomplete-item:has(.geo-search-entry-info) {+ cursor: default;+ background: none;+ color: inherit;+}diff --git a/apps/client/src/widgets/collections/geomap/SearchBox.spec.tsx b/apps/client/src/widgets/collections/geomap/SearchBox.spec.tsxindex 08d37027e8..59b0f1ab92 100644--- a/apps/client/src/widgets/collections/geomap/SearchBox.spec.tsx+++ b/apps/client/src/widgets/collections/geomap/SearchBox.spec.tsx@@ -1,26 +1,46 @@/**- * The geo map search bar: which results a query produces, and what picking one does to the map.- * Results come from the dummy provider, so these cover the bar rather than any one geocoder.+ * The geo map search bar: what a query lists, when the geocoder is allowed to run, and what picking+ * a row does to the map. The geocoder is the dummy provider, so these cover the bar rather than any+ * one geocoder.*/import type { Map as MapLibreGLMap } from "maplibre-gl";import { act } from "preact/test-utils";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import type FNote from "../../../entities/fnote";+import { buildNote } from "../../../test/easy-froca";import { renderInto } from "../../../test/render";+import { GEOCODING_PROVIDERS } from "./geocoding";import { ParentMap } from "./map";import SearchBox from "./SearchBox";+// i18next is not initialized under test, so t() returns "". The key stands in for the text, with any+// interpolated values after it, which is enough to tell the rows apart.+vi.mock("../../../services/i18n", () => ({+ t: (key: string, vars?: Record<string, unknown>) => (vars ? `${key}(${Object.values(vars).join(",")})` : key)+}));+/** A map that can be flown somewhere, which is all the bar uses. */function fakeMap() {return { flyTo: vi.fn() };}-function renderSearchBox(map: ReturnType<typeof fakeMap> | null) {+/** Two notes on the map and one that is only in its subtree, having no location to be drawn at. */+function mapNotes(): FNote[] {+ return [+ buildNote({ title: "London hotel", "#geolocation": "51.5,-0.12" }),+ buildNote({ title: "London office", "#geolocation": "51.6,-0.1" }),+ buildNote({ title: "London packing list" }),+ buildNote({ title: "Tokyo trip", "#geolocation": "35.6,139.7" })+ ];+}++function renderSearchBox(map: ReturnType<typeof fakeMap> | null, notes: FNote[] = []) {let container: HTMLElement | undefined;act(() => {container = renderInto(<ParentMap.Provider value={map as unknown as MapLibreGLMap}>- <SearchBox />+ <SearchBox notes={notes} /></ParentMap.Provider>);});@@ -34,6 +54,11 @@ function field(container: HTMLElement) {return input;}+/** Lets the debounced lookup and anything it started run to completion. */+async function settle() {+ await act(async () => { await vi.advanceTimersByTimeAsync(300); });+}+/** Types into the field and runs out the debounced lookup. */async function type(container: HTMLElement, text: string) {const input = field(container);@@ -43,7 +68,7 @@ async function type(container: HTMLElement, text: string) {input.value = text;input.dispatchEvent(new Event("input", { bubbles: true }));});- await act(async () => { await vi.advanceTimersByTimeAsync(300); });+ await settle();}/** The dropdown is portalled to the body, so look for it there rather than in the container. */@@ -51,37 +76,111 @@ function entries() {return [ ...document.querySelectorAll<HTMLElement>(".form-autocomplete-dropdown .form-autocomplete-item") ];}+function labels() {+ return entries().map((entry) => entry.textContent);+}++/** Picks a row and lets whatever it started settle. */+async function pick(index: number) {+ await act(async () => { entries()[index].click(); });+ await settle();+}+beforeEach(() => vi.useFakeTimers());-afterEach(() => vi.useRealTimers());+afterEach(() => {+ vi.useRealTimers();+ vi.restoreAllMocks();+});describe("geo map SearchBox", () => {- it("lists the places a query matches, and nothing for a query below the minimum length", async () => {- const container = renderSearchBox(fakeMap());+ it("lists the map's own markers by title, skipping notes with nowhere to fly to", async () => {+ const container = renderSearchBox(fakeMap(), mapNotes());++ await type(container, "london");++ // "London packing list" has no location, so it has no marker to offer.+ expect(labels()).toEqual([+ "London hotel",+ "London office",+ "geo-map.search-online(london)"+ ]);+ });++ it("offers nothing for a query below the minimum length", async () => {+ const container = renderSearchBox(fakeMap(), mapNotes());await type(container, "l");+expect(entries()).toHaveLength(0);+ });- await type(container, "london");- // Two places of the same name, distinguished by the rest of the label.- expect(entries().map((entry) => entry.textContent)).toEqual([- "London, England, United Kingdom",- "London, Ontario, Canada"- ]);+ it("does not reach the geocoder until its row is picked", async () => {+ const search = vi.spyOn(GEOCODING_PROVIDERS.dummy, "search");+ const container = renderSearchBox(fakeMap(), mapNotes());++ await type(container, "tokyo");+ expect(search).not.toHaveBeenCalled();+ expect(labels()).toEqual([ "Tokyo trip", "geo-map.search-online(tokyo)" ]);++ await pick(1);++ expect(search).toHaveBeenCalledWith("tokyo");+ // The row is replaced by what it found, the markers still standing above it.+ expect(labels()).toEqual([ "Tokyo trip", "Tokyo, Japan" ]);+ });++ it("flies to a marker, and closes the list rather than leaving it open over the map", async () => {+ const map = fakeMap();+ const container = renderSearchBox(map, mapNotes());++ await type(container, "tokyo trip");+ await pick(0);- await type(container, "nowhere at all");+ expect(map.flyTo).toHaveBeenCalledWith({ center: [ 139.7, 35.6 ], zoom: expect.any(Number) });+ expect(field(container).value).toBe("Tokyo trip");expect(entries()).toHaveLength(0);});- it("flies the map to the place picked and puts its name in the field", async () => {+ it("flies to a geocoded place, further out than to a marker", async () => {const map = fakeMap();- const container = renderSearchBox(map);+ const container = renderSearchBox(map, mapNotes());await type(container, "tokyo");- await act(async () => { entries()[0].click(); });+ await pick(1);+ await pick(1);expect(map.flyTo).toHaveBeenCalledWith({ center: [ 139.6503, 35.6762 ], zoom: expect.any(Number) });expect(field(container).value).toBe("Tokyo, Japan");- expect(entries()).toHaveLength(0);++ const [ { zoom: placeZoom } ] = map.flyTo.mock.calls[0];+ await type(container, "tokyo trip");+ await pick(0);+ const [ { zoom: markerZoom } ] = map.flyTo.mock.calls[1];+ expect(markerZoom).toBeGreaterThan(placeZoom);+ });++ it("says so when the geocoder finds nothing, and when it cannot be reached", async () => {+ const container = renderSearchBox(fakeMap(), []);++ await type(container, "atlantis");+ await pick(0);+ expect(labels()).toEqual([ "geo-map.no-places-found" ]);++ vi.spyOn(GEOCODING_PROVIDERS.dummy, "search").mockRejectedValue(new Error("rate limited"));+ await type(container, "berlin");+ await pick(0);+ expect(labels()).toEqual([ "geo-map.search-failed" ]);+ });++ it("offers the geocoder again once the query moves on from what it answered", async () => {+ const container = renderSearchBox(fakeMap(), []);++ await type(container, "berlin");+ await pick(0);+ expect(labels()).toEqual([ "Berlin, Germany" ]);++ await type(container, "berlin de");+ expect(labels()).toEqual([ "geo-map.search-online(berlin de)" ]);});it("renders nothing when the map failed to initialize", () => {diff --git a/apps/client/src/widgets/collections/geomap/SearchBox.tsx b/apps/client/src/widgets/collections/geomap/SearchBox.tsxindex bf23b7a0d7..adf0ae8a19 100644--- a/apps/client/src/widgets/collections/geomap/SearchBox.tsx+++ b/apps/client/src/widgets/collections/geomap/SearchBox.tsx@@ -2,54 +2,140 @@ import "./SearchBox.css";import { useCallback, useContext, useRef, useState } from "preact/hooks";+import type FNote from "../../../entities/fnote";import { t } from "../../../services/i18n";+import { logError } from "../../../services/ws";import FormAutocomplete from "../../react/FormAutocomplete";import Icon from "../../react/Icon";import OverlayToolbar from "../../react/OverlayToolbar";import { DEFAULT_GEOCODING_PROVIDER_NAME, GEOCODING_PROVIDERS, type GeoSearchResult } from "./geocoding";import { ParentMap } from "./map";+import { LOCATION_ATTRIBUTE, parseLocation } from "./Markers";-/** Shorter queries are not looked up. */+/** Shorter queries are not searched. */const MIN_QUERY_LENGTH = 2;-/** The zoom level a picked result is shown at. */-const RESULT_ZOOM = 12;+/** The zoom level a place from the geocoder is shown at. */+const PLACE_ZOOM = 12;++/** The zoom level a marker is shown at, closer in since a note marks a spot rather than an area. */+const MARKER_ZOOM = 15;++/** Caps how many of the map's own notes the list offers. */+const MAX_MARKER_RESULTS = 8;++/** The key of the row reporting on a geocoder run, which only ever appears once. */+const STATUS_KEY = "geocode-status";++interface SearchBoxProps {+ /** The notes on the map, searched by title. */+ notes: FNote[];+}/**- * A search bar over a geo map that flies the map to the place picked from it.+ * One row of the result list. `key` identifies it, since `FormAutocomplete` items are strings and two+ * rows can read the same.+ */+type SearchEntry = {+ key: string;+ label: string;+ /** A boxicons class, as `FNote.getIcon()` gives it. */+ icon: string;+} & (+ | { kind: "marker" | "place"; center: [number, number] }+ /** Runs the geocoder for `query`. */+ | { kind: "geocode"; query: string }+ /** Reports on a geocoder run; picking it does nothing. */+ | { kind: "info" }+);++/** A geocoder run, kept so its results can be listed under the query they answer. */+interface GeocodeRun {+ query: string;+ status: "loading" | "done" | "failed";+ results: SearchEntry[];+}++/**+ * A search bar over a geo map, which flies the map to the marker or place picked from it.*- * Results come from a `GeocodingProvider`, currently the dummy one.+ * The map's own notes are matched by title as the user types. The geocoder is not: it costs a request+ * to a third party, and providers rate-limit and discourage per-keystroke lookups, so it sits at the+ * bottom of the list as a row that runs it when picked.** `FormAutocomplete` handles the debounce, the stale-response guard, keyboard navigation and a- * dropdown portalled out of the map's scrolling container. Its items are strings, so `resultsByLabel`- * holds the matching `GeoSearchResult` to look up on pick. Results with the same label collapse to- * one, since they read identically in the list.+ * dropdown portalled out of the map's scrolling container. `keepOpenOnPick` keeps the list up so the+ * geocoder row can replace itself with results, so closing it after a marker or place is taken is+ * this component's job — see `dismissed`.*/-export default function SearchBox() {+export default function SearchBox({ notes }: SearchBoxProps) {const map = useContext(ParentMap);const [ query, setQuery ] = useState("");- const resultsByLabel = useRef(new Map<string, GeoSearchResult>());+ const [ geocodeRun, setGeocodeRun ] = useState<GeocodeRun>();+ // Empties the list once a marker or place has been taken, which is what closes the dropdown under+ // `keepOpenOnPick`. Typing again clears it.+ const [ dismissed, setDismissed ] = useState(false);+ const entriesByKey = useRef(new Map<string, SearchEntry>());+ // Discards a run superseded by a later one, since each reports through the same state.+ const latestRun = useRef(0);const provider = GEOCODING_PROVIDERS[DEFAULT_GEOCODING_PROVIDER_NAME];const source = useCallback(async (currentQuery: string) => {const trimmed = currentQuery.trim();- if (trimmed.length < MIN_QUERY_LENGTH) {- resultsByLabel.current = new Map();+ if (dismissed || trimmed.length < MIN_QUERY_LENGTH) {+ entriesByKey.current = new Map();return [];}- const results = await provider.search(trimmed);- resultsByLabel.current = new Map(results.map((result) => [ result.label, result ]));- return [ ...resultsByLabel.current.keys() ];+ const entries = [ ...matchMarkers(notes, trimmed), ...geocodeEntries(geocodeRun, trimmed) ];+ entriesByKey.current = new Map(entries.map((entry) => [ entry.key, entry ]));+ return entries.map((entry) => entry.key);+ }, [ notes, geocodeRun, dismissed ]);++ const changeQuery = useCallback((newQuery: string) => {+ setDismissed(false);+ setQuery(newQuery);+ }, []);++ const runGeocoder = useCallback(async (searchQuery: string) => {+ const runId = ++latestRun.current;+ setGeocodeRun({ query: searchQuery, status: "loading", results: [] });++ try {+ const results = await provider.search(searchQuery);+ if (latestRun.current !== runId) return;+ setGeocodeRun({ query: searchQuery, status: "done", results: results.map(placeEntry) });+ } catch (e) {+ logError(`Geocoding with "${provider.name}" failed: ${e}`);+ if (latestRun.current !== runId) return;+ setGeocodeRun({ query: searchQuery, status: "failed", results: [] });+ }}, [ provider ]);- const flyToResult = useCallback((label: string) => {- const result = resultsByLabel.current.get(label);- if (!result || !map) return;+ const pickEntry = useCallback((key: string) => {+ const entry = entriesByKey.current.get(key);+ if (!entry || !map) return;++ if (entry.kind === "geocode") {+ runGeocoder(entry.query);+ } else if (entry.kind === "marker" || entry.kind === "place") {+ setQuery(entry.label);+ setDismissed(true);+ map.flyTo({ center: entry.center, zoom: entry.kind === "marker" ? MARKER_ZOOM : PLACE_ZOOM });+ }+ }, [ map, runGeocoder ]);++ const renderEntry = useCallback((key: string) => {+ const entry = entriesByKey.current.get(key);+ if (!entry) return key;- setQuery(label);- map.flyTo({ center: [ result.lng, result.lat ], zoom: RESULT_ZOOM });- }, [ map ]);+ return (+ <span className={`geo-search-entry geo-search-entry-${entry.kind}`}>+ <Icon icon={entry.icon} />+ {entry.label}+ </span>+ );+ }, []);// The map failed to initialize, e.g. WebGL is unavailable (see map.tsx).if (!map) return null;@@ -60,12 +146,83 @@ export default function SearchBox() {<FormAutocompleteclassName="geo-search-input"currentValue={query}- onChange={setQuery}- onPick={flyToResult}+ onChange={changeQuery}+ onPick={pickEntry}source={source}+ renderItem={renderEntry}+ keepOpenOnPickplaceholder={t("geo-map.search-placeholder")}aria-label={t("geo-map.search")}/></OverlayToolbar>);}++/** The notes on the map whose title contains the query and that are drawn somewhere. */+function matchMarkers(notes: FNote[], query: string): SearchEntry[] {+ const needle = query.toLowerCase();+ const matches: SearchEntry[] = [];++ for (const note of notes) {+ if (matches.length >= MAX_MARKER_RESULTS) break;+ if (!note.title.toLowerCase().includes(needle)) continue;++ // A note without a readable location has no marker to fly to. GPX tracks are skipped for the+ // same reason: their route is in the file rather than on a label.+ const center = parseLocation(note.getLabelValue(LOCATION_ATTRIBUTE));+ if (!center) continue;++ matches.push({+ kind: "marker",+ key: `marker:${note.noteId}`,+ label: note.title,+ icon: note.getIcon(),+ center+ });+ }++ return matches;+}++/**+ * What the list offers below the markers: the results of a run answering this query, or the row that+ * starts one. Typing never reaches the provider — picking that row is the only thing that does.+ */+function geocodeEntries(run: GeocodeRun | undefined, query: string): SearchEntry[] {+ if (run?.query !== query) {+ return [ {+ kind: "geocode",+ key: "geocode",+ label: t("geo-map.search-online", { query }),+ icon: "bx bx-search-alt",+ query+ } ];+ }++ if (run.status === "loading") {+ return [ infoEntry(t("geo-map.searching-online"), "bx bx-loader-alt") ];+ }+ if (run.status === "failed") {+ return [ infoEntry(t("geo-map.search-failed"), "bx bx-error-circle") ];+ }+ if (!run.results.length) {+ return [ infoEntry(t("geo-map.no-places-found"), "bx bx-info-circle") ];+ }++ return run.results;+}++function placeEntry(result: GeoSearchResult): SearchEntry {+ return {+ kind: "place",+ key: `place:${result.id}`,+ label: result.label,+ icon: "bx bx-map-pin",+ center: [ result.lng, result.lat ]+ };+}++/** A row that reports on the geocoder rather than offering a place. */+function infoEntry(label: string, icon: string): SearchEntry {+ return { kind: "info", key: STATUS_KEY, label, icon };+}diff --git a/apps/client/src/widgets/collections/geomap/index.tsx b/apps/client/src/widgets/collections/geomap/index.tsxindex 13822f5637..e081455017 100644--- a/apps/client/src/widgets/collections/geomap/index.tsx+++ b/apps/client/src/widgets/collections/geomap/index.tsx@@ -244,7 +244,7 @@ export default function GeoView({ note, noteIds, viewConfig, saveConfig }: ViewMonClick={onClick}scale={hasScale}>- <SearchBox />+ <SearchBox notes={notes} /><MapToolbar /><EditToolbarisReadOnly={isReadOnly}