Skip to content

Build your personal knowledge base with Trilium Notes

feat(geomap): give the map's own notes a heading of their own

Elian Doran705ba219d345
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 32c763dc2b..7d09ef476d 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2965,7 +2965,8 @@
"add-place-as-marker": "Add as marker",
"previous-result": "Previous result",
"next-result": "Next result",
- "result-position": "Result {{index}} of {{total}}, click to return to it"
+ "result-position": "Result {{index}} of {{total}}, click to return to it",
+ "results-on-map": "On this map"
},
"embedded_note": {
"open-note": "Open note",
diff --git a/apps/client/src/widgets/collections/geomap/SearchBox.spec.tsx b/apps/client/src/widgets/collections/geomap/SearchBox.spec.tsx
index 902e6eeac8..c8b707451d 100644
--- a/apps/client/src/widgets/collections/geomap/SearchBox.spec.tsx
+++ b/apps/client/src/widgets/collections/geomap/SearchBox.spec.tsx
@@ -217,8 +217,12 @@ describe("geo map SearchBox", () => {
// answered with the one at hand.
viewport: [ [ VIEWPORT.west, VIEWPORT.south ], [ VIEWPORT.east, VIEWPORT.north ] ]
});
- // The row is replaced by what it found, standing among the map's own notes.
- expect([ ...labels() ].sort()).toEqual([ "Tokyo", "Tokyo trip" ]);
+ // The row is replaced by what it found, under a heading of its own: a place the geocoder
+ // turned up is a different answer from a note already standing on the map.
+ expect(labels()).toEqual([
+ "geo-map.results-on-map", "Tokyo trip",
+ "geo-map.results-far", "Tokyo"
+ ]);
});
it("flies to a marker, and closes the list rather than leaving it open over the map", async () => {
@@ -395,7 +399,7 @@ describe("geo map SearchBox", () => {
expect(field(container).value).toBe("tokyo");
});
- it("puts what is at hand under one heading and what is far off under another, nearest first", async () => {
+ it("puts the map's own notes, what is at hand and what is far off each under their own heading", async () => {
// Half of Corfu away, a good way up the coast, and another country entirely.
mockGeocoder([
{ id: "near", label: "Jumbo, Corfu", name: "Jumbo", lat: CENTRE.lat + 0.09, lng: CENTRE.lng },
@@ -409,12 +413,10 @@ describe("geo map SearchBox", () => {
await pickNamed("geo-map.search-online(jumbo)");
expect(labels()).toEqual([
- "geo-map.results-nearby",
- // The note is nearer than the shop, and the shop nearer than the one across the ocean.
- "Jumbo receipt",
- "Jumbo",
- "geo-map.results-far",
- "Jumbo"
+ // The map's own first, whatever the distances say.
+ "geo-map.results-on-map", "Jumbo receipt",
+ "geo-map.results-nearby", "Jumbo",
+ "geo-map.results-far", "Jumbo"
]);
// A heading names the rows below it rather than offering anything, so it is not among the
// rows that can be taken.
diff --git a/apps/client/src/widgets/collections/geomap/SearchBox.tsx b/apps/client/src/widgets/collections/geomap/SearchBox.tsx
index 5a20b8587f..5e6fff42ac 100644
--- a/apps/client/src/widgets/collections/geomap/SearchBox.tsx
+++ b/apps/client/src/widgets/collections/geomap/SearchBox.tsx
@@ -142,7 +142,7 @@ export default function SearchBox({ notes, onPickResult }: SearchBoxProps) {
const { places, notice } = geocodeEntries(geocodeRun, trimmed);
const found = [ ...matchMarkers(notes, trimmed), ...places ]
.map((entry) => withDistance(entry, origin));
- const entries = [ ...byDistance(found), ...(notice ? [ notice ] : []) ];
+ const entries = [ ...grouped(found), ...(notice ? [ notice ] : []) ];
entriesByKey.current = new Map(entries.map((entry) => [ entry.key, entry ]));
return entries.map((entry) => entry.key);
}, [ notes, geocodeRun, dismissed, map ]);
@@ -406,28 +406,32 @@ function geocodeEntries(run: GeocodeRun | undefined, query: string): {
}
/**
- * The places found, nearest first, under headings that say which are at hand and which are not — a
- * shop down the road and one on another continent are both answers to the same name, and the list
- * used to run them together.
+ * What was found, under headings naming what each run of rows is: the map's own notes first, then
+ * the geocoder's places, split by whether they are at hand.
*
- * Headings only where there is something on both sides of {@link SEARCH_RADIUS_M}: one name over one
- * run of rows distinguishes it from nothing.
+ * Nearest first within each. A note already standing on the map, a shop down the road and one on
+ * another continent are three different answers to the same name, and the list used to run them
+ * together.
+ *
+ * Headings only where more than one of the three was found: one name over the whole list
+ * distinguishes it from nothing.
*/
-function byDistance(entries: SearchEntry[]): SearchEntry[] {
+function grouped(entries: SearchEntry[]): SearchEntry[] {
const sorted = [ ...entries ].sort((a, b) => (a.distance ?? Infinity) - (b.distance ?? Infinity));
- const atHand = sorted.filter((entry) => (entry.distance ?? Infinity) <= SEARCH_RADIUS_M);
- const further = sorted.slice(atHand.length);
+ const places = sorted.filter((entry) => entry.kind === "place");
+ const isAtHand = (entry: SearchEntry) => (entry.distance ?? Infinity) <= SEARCH_RADIUS_M;
+
+ const groups: { key: string; label: string; rows: SearchEntry[] }[] = [
+ { key: "on-map", label: t("geo-map.results-on-map"), rows: sorted.filter((entry) => entry.kind === "marker") },
+ { key: "nearby", label: t("geo-map.results-nearby"), rows: places.filter(isAtHand) },
+ { key: "far", label: t("geo-map.results-far"), rows: places.filter((entry) => !isAtHand(entry)) }
+ ].filter((group) => group.rows.length);
- if (!atHand.length || !further.length) {
- return sorted;
+ if (groups.length < 2) {
+ return groups.flatMap((group) => group.rows);
}
- return [
- headingEntry("nearby", t("geo-map.results-nearby")),
- ...atHand,
- headingEntry("far", t("geo-map.results-far")),
- ...further
- ];
+ return groups.flatMap((group) => [ headingEntry(group.key, group.label), ...group.rows ]);
}
function headingEntry(key: string, label: string): SearchEntry {