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): find a drawn shape in the map's search bar by its title
Elian Doran1ec3aa4f6ac5
diff --git a/apps/client/src/widgets/collections/geomap/SearchBox.spec.tsx b/apps/client/src/widgets/collections/geomap/SearchBox.spec.tsxindex bcbf4c7c9f..04a4a0992f 100644--- a/apps/client/src/widgets/collections/geomap/SearchBox.spec.tsx+++ b/apps/client/src/widgets/collections/geomap/SearchBox.spec.tsx@@ -247,6 +247,27 @@ describe("geo map SearchBox", () => {expect(map.fitBounds).not.toHaveBeenCalled();});+ it("offers a drawn shape by its title, framed on the ground it covers", async () => {+ mockGeocoder([]);+ const map = fakeMap();+ const shape = buildNote({ title: "Old town walls", "#geoShape": "polygon:39.5,19.5 39.75,19.5 39.75,20" });+ const container = renderSearchBox(map, [ ...mapNotes(), shape ]);++ await type(container, "walls");+ expect(labels()[0]).toBe("Old town walls");++ await pick(0);++ // Framed on its box rather than flown to one of its corners, and measured from the middle of+ // that box, so it is ordered among the markers by how far off it stands.+ const bounds = [ [ 19.5, 39.5 ], [ 20, 39.75 ] ];+ expect(picked.at(-1)?.results[0]).toEqual({+ kind: "note", noteId: shape.noteId, center: [ 19.75, 39.625 ], bounds+ });+ expect(map.fitBounds).toHaveBeenCalledWith(bounds, expect.anything());+ expect(map.flyTo).not.toHaveBeenCalled();+ });+it("offers a point named outright, above whatever was searched for", async () => {mockGeocoder([]);const map = fakeMap();diff --git a/apps/client/src/widgets/collections/geomap/SearchBox.tsx b/apps/client/src/widgets/collections/geomap/SearchBox.tsxindex 7c33245d65..45121e6eb4 100644--- a/apps/client/src/widgets/collections/geomap/SearchBox.tsx+++ b/apps/client/src/widgets/collections/geomap/SearchBox.tsx@@ -14,32 +14,18 @@ import { filterTokens, matchesFilter } from "../../react/filter";import FormAutocomplete from "../../react/FormAutocomplete";import Icon from "../../react/Icon";import OverlayToolbar, { OverlayToolbarButton } from "../../react/OverlayToolbar";-import { parseCoordinates, pointPlace } from "./coordinates";+import { type Bounds, parseCoordinates, pointPlace } from "./coordinates";import { DEFAULT_GEOCODING_PROVIDER_NAME, DEFAULT_PLACE_ICON, type GeoBounds, GEOCODING_PROVIDERS, type GeoSearchResult, SEARCH_RADIUS_M } from "./geocoding";import { GPX_MIME } from "./GpxTrack";import { ParentMap } from "./map";import { describePlace } from "./place_address";import { LOCATION_ATTRIBUTE, parseLocation } from "./Markers";import { frameResult, type SearchResult } from "./results";+import { geoShapeBounds, parseGeoShape, SHAPE_ATTRIBUTE } from "./shapes";/** Shorter queries are not searched. */const MIN_QUERY_LENGTH = 2;-/** The zoom level a place is shown at where the geocoder does not say how much ground it covers. */-const PLACE_ZOOM = 12;--/**- * How close a place is framed at most. A house's extent is a few metres across, which on its own- * would fill the screen with the roof.- */-const PLACE_MAX_ZOOM = 17;--/** The room kept around a framed place, so its pin and name do not sit against the map's edge. */-const PLACE_PADDING = 60;--/** The zoom level a marker is shown at, closer in since a note marks a spot rather than an area. */-const MARKER_ZOOM = 15;-/** The mean radius of the Earth, which is what a great-circle distance is measured on. */const EARTH_RADIUS_M = 6_371_008.8;@@ -81,8 +67,11 @@ type SearchEntry = {/** A second line under the first: the address that places a place, or whose answer a row is. */detail?: string;} & (- /** A note of the map's own. `center` is absent for a GPX track, which stands on no one point. */- | { kind: "marker"; center?: [number, number]; noteId: string }+ /**+ * A note of the map's own. `center` is where it is measured from, absent for a GPX track, which+ * stands on no one point; `bounds` is the ground a drawn shape covers (see {@link frameResult}).+ */+ | { kind: "marker"; center?: [number, number]; bounds?: Bounds; noteId: string }/** A place from the geocoder, carried whole for whoever the pick is reported to. */| { kind: "place"; center: [number, number]; result: GeoSearchResult }/**@@ -314,7 +303,7 @@ function walkableResults(entries: Map<string, SearchEntry>): SearchResult[] {for (const entry of entries.values()) {if (entry.kind === "marker") {- results.push({ kind: "note", noteId: entry.noteId, center: entry.center });+ results.push({ kind: "note", noteId: entry.noteId, center: entry.center, bounds: entry.bounds });} else if (entry.kind === "place" || entry.kind === "point") {results.push({ kind: "place", place: entry.result });}@@ -375,11 +364,13 @@ function matchMarkers(notes: FNote[], query: string): SearchEntry[] {for (const note of notes) {if (!matchesFilter(tokens, note.title)) continue;- // A note without a readable location has no marker to fly to. A GPX track has no location- // either — its route is in the file — but it is drawn on the map all the same, and the pane- // fits the whole of it (see DetailPane).+ // The three ways a note reaches the map, in the order DetailPane reads them: a marker's+ // location, a drawn shape's geometry, and a GPX track's file. A track's route is in the+ // file rather than on a label, so it stands on no point the list can measure from, and the+ // pane fits the whole of it (see DetailPane).const center = parseLocation(note.getLabelValue(LOCATION_ATTRIBUTE));- if (!center && note.mime !== GPX_MIME) continue;+ const bounds = center ? null : shapeBounds(note);+ if (!center && !bounds && note.mime !== GPX_MIME) continue;matches.push({kind: "marker",@@ -387,13 +378,29 @@ function matchMarkers(notes: FNote[], query: string): SearchEntry[] {noteId: note.noteId,label: note.title,icon: note.getIcon(),- center: center ?? undefined+ // A shape is measured from the middle of the ground it covers, so that it is ordered+ // among the markers by how far off it is rather than sorted to the end of them.+ center: center ?? (bounds ? boundsCenter(bounds) : undefined),+ bounds: bounds ?? undefined});}return matches;}+/** The box a note's drawn shape covers, or `null` where the note carries no readable one. */+function shapeBounds(note: FNote): Bounds | null {+ const value = note.getLabelValue(SHAPE_ATTRIBUTE);+ const shape = value ? parseGeoShape(value) : null;+ return shape ? geoShapeBounds(shape) : null;+}++/** The middle of a box. A shape crossing ±180° is framed on longitudes past 180° (see `boundsOf`),+ * which {@link metresBetween} reads as the same meridian either way. */+function boundsCenter([ [ west, south ], [ east, north ] ]: Bounds): [number, number] {+ return [ (west + east) / 2, (south + north) / 2 ];+}+/*** What the geocoder has to offer for this query: the places a run answering it found, and whatever* has to be said about the run — the row that starts one, or a word on how the last one went.diff --git a/apps/client/src/widgets/collections/geomap/results.ts b/apps/client/src/widgets/collections/geomap/results.tsindex aaf3f9ca15..2eef652d59 100644--- a/apps/client/src/widgets/collections/geomap/results.ts+++ b/apps/client/src/widgets/collections/geomap/results.ts@@ -1,11 +1,16 @@import type { Map as MapLibreGLMap } from "maplibre-gl";+import type { Bounds } from "./coordinates";import type { GeoSearchResult } from "./geocoding";/** One of the things a search turned up: a note of the map's own, or a place from the geocoder. */export type SearchResult =- /** `center` is absent for a GPX track, whose route is in the file rather than on a label. */- | { kind: "note"; noteId: string; center?: [number, number] }+ /**+ * `center` is where a marker stands, absent for a GPX track, whose route is in the file rather+ * than on a label. `bounds` is the ground a drawn shape covers, which is framed rather than+ * flown to.+ */+ | { kind: "note"; noteId: string; center?: [number, number]; bounds?: Bounds }| { kind: "place"; place: GeoSearchResult };/** The zoom level a place is shown at where the geocoder does not say how much ground it covers. */@@ -15,35 +20,39 @@ const PLACE_ZOOM = 12;export const NOTE_ZOOM = 15;/**- * How close a place is framed at most. A house's extent is a few metres across, which on its own+ * How close an extent is framed at most. A house's extent is a few metres across, which on its own* would fill the screen with the roof.*/-const PLACE_MAX_ZOOM = 17;+const FIT_MAX_ZOOM = 17;-/** The room kept around a framed place, so its pin and name do not sit against the map's edge. */-const PLACE_PADDING = 60;+/** The room kept around a framed extent, so its pin and name do not sit against the map's edge. */+const FIT_PADDING = 60;/*** Points the map at one of a search's results.** A place is framed on the ground it covers, where the geocoder says how much that is: one zoom- * level suited to a city shows a street as the city around it. A note marks a spot and has no extent- * to frame, and neither has a place the geocoder gave no bounding box for.+ * level suited to a city shows a street as the city around it. A drawn shape is framed the same way,+ * on the box its `#geoShape` label spells out. A marker stands on a spot and has no extent to frame,+ * and neither has a place the geocoder gave no bounding box for.** A GPX track is left to the pane, which fits its whole route once the line is on the map (see- * DetailPane) — a route is a shape, and there is no one point to fly to.+ * DetailPane) — a route's extent is in the file rather than on a label, and there is no one point to+ * fly to.** A place asking for a zoom of its own is shown at it, which is how a point named by its* coordinates is shown as closely as it was meant rather than at the level a town is.*/export function frameResult(map: MapLibreGLMap, result: SearchResult) {if (result.kind === "place" && result.place.bounds) {- map.fitBounds(result.place.bounds, { padding: PLACE_PADDING, maxZoom: PLACE_MAX_ZOOM });+ map.fitBounds(result.place.bounds, { padding: FIT_PADDING, maxZoom: FIT_MAX_ZOOM });return;}if (result.kind === "note") {- if (result.center) {+ if (result.bounds) {+ map.fitBounds(result.bounds, { padding: FIT_PADDING, maxZoom: FIT_MAX_ZOOM });+ } else if (result.center) {map.flyTo({ center: result.center, zoom: NOTE_ZOOM });}return;diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Geo Map.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Geo Map.htmlindex 1bcd630afa..555c219404 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Geo Map.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Geo Map.html@@ -111,9 +111,9 @@titles, so <code spellcheck="false">zurich</code> finds <em>Zürich Hauptbahnhof</em>.Each word is searched for on its own, so <code spellcheck="false">hotel paris</code> finds <em>Paris Hotel</em>.Notes without a <code spellcheck="false">#geolocation</code> attribute are- not offered, since there would be nowhere to go. GPS tracks are offered,- even though they carry no such attribute: selecting one brings its whole- route into view.</p>+ not offered, since there would be nowhere to go. Drawn shapes and GPS tracks+ are offered, even though they carry no such attribute: selecting one brings+ the whole shape or route into view.</p><p>If more notes match than the list can show, you are offered the ones closestto the area you are viewing.</p><h3>Searching for places online</h3>diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Geo Map/Drawing shapes.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Geo Map/Drawing shapes.htmlindex 853c807527..cea2336f07 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Geo Map/Drawing shapes.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Geo Map/Drawing shapes.html@@ -47,6 +47,10 @@<p>Right-clicking a shape opens a contextual menu similar to the one formarkers, which allows opening the corresponding note or removing the shape.</p></li>+ <li>+ <p>Typing in the map's search bar finds shapes by their title, as it does+ markers. Selecting one brings the whole shape into view.</p>+ </li></ul><h2>How shapes are represented</h2><p>Shapes are represented quite similarly to markers, which means each shapediff --git a/docs/User Guide/User Guide/Collections/Geo Map.md b/docs/User Guide/User Guide/Collections/Geo Map.mdindex b5815527e7..1d0986a092 100644--- a/docs/User Guide/User Guide/Collections/Geo Map.md+++ b/docs/User Guide/User Guide/Collections/Geo Map.md@@ -71,7 +71,7 @@ The search bar at the top-left of the map searches in two places. It always sear### Searching the notes already on the map-Type in the search bar, and the notes on the map are matched by their title as you type. Accents are ignored, both in what you type and in the titles, so `zurich` finds _Zürich Hauptbahnhof_. Each word is searched for on its own, so `hotel paris` finds _Paris Hotel_. Notes without a `#geolocation` attribute are not offered, since there would be nowhere to go. GPS tracks are offered, even though they carry no such attribute: selecting one brings its whole route into view.+Type in the search bar, and the notes on the map are matched by their title as you type. Accents are ignored, both in what you type and in the titles, so `zurich` finds _Zürich Hauptbahnhof_. Each word is searched for on its own, so `hotel paris` finds _Paris Hotel_. Notes without a `#geolocation` attribute are not offered, since there would be nowhere to go. Drawn shapes and GPS tracks are offered, even though they carry no such attribute: selecting one brings the whole shape or route into view.If more notes match than the list can show, you are offered the ones closest to the area you are viewing.diff --git a/docs/User Guide/User Guide/Collections/Geo Map/Drawing shapes.md b/docs/User Guide/User Guide/Collections/Geo Map/Drawing shapes.mdindex 77da8682b8..592821658d 100644--- a/docs/User Guide/User Guide/Collections/Geo Map/Drawing shapes.md+++ b/docs/User Guide/User Guide/Collections/Geo Map/Drawing shapes.md@@ -37,6 +37,7 @@ Once a shape is created:* When clicking, a marker takes priority over the shape.* Right-clicking a shape opens a contextual menu similar to the one for markers, which allows opening the corresponding note or removing the shape.+* Typing in the map's search bar finds shapes by their title, as it does markers. Selecting one brings the whole shape into view.## How shapes are represented