Skip to content

Build your personal knowledge base with Trilium Notes

fix(geomap): image map crashing with a NaN camera when first opened

Elian Doran88ed611e0d58
diff --git a/apps/client/src/widgets/collections/geomap/Markers.spec.tsx b/apps/client/src/widgets/collections/geomap/Markers.spec.tsx
index 3b55cb6288..cebf15fef8 100644
--- a/apps/client/src/widgets/collections/geomap/Markers.spec.tsx
+++ b/apps/client/src/widgets/collections/geomap/Markers.spec.tsx
@@ -22,6 +22,7 @@ import LoadResults from "../../../services/load_results";
import { buildNote } from "../../../test/easy-froca";
import { ParentComponent } from "../../react/react_utils";
import { CLUSTER_COUNT_LAYER, CLUSTER_LAYER } from "./clusters";
+import type { Bounds } from "./coordinates";
import { MapStyleLoaded, ParentMap } from "./map";
import Markers, { FitToNotes, MARKER_LAYER, MARKER_SOURCE, SELECTION_LAYER } from "./Markers";
import { formatLocation, parseLocation } from "./space";
@@ -718,11 +719,11 @@ describe("framing a map around its notes", () => {
container.remove();
});
- function mountFit(notes: FNote[], map: ReturnType<typeof fakeMap>, enabled = true) {
+ function mountFit(notes: FNote[], map: ReturnType<typeof fakeMap>, enabled = true, bounds?: Bounds) {
return act(async () => {
render(
<ParentMap.Provider value={map as never}>
- <FitToNotes notes={notes} enabled={enabled} />
+ <FitToNotes notes={notes} enabled={enabled} bounds={bounds} />
</ParentMap.Provider>,
container
);
@@ -745,6 +746,20 @@ describe("framing a map around its notes", () => {
expect(map.fits[0].options).toMatchObject({ maxZoom: expect.any(Number), animate: false });
});
+ it("frames the bounds it is given instead of the notes, passing no option as undefined", async () => {
+ const map = fakeMap();
+ const image: Bounds = [ [ -90, -40 ], [ 90, 40 ] ];
+
+ await mountFit([ PARIS ], map, true, image);
+
+ expect(map.fits).toHaveLength(1);
+ expect(map.fits[0].bounds).toEqual(image);
+ // MapLibre merges the options over its defaults key by key, so an `undefined` replaces the
+ // default rather than leaving it be: `maxZoom: undefined` made the zoom, and so the centre, NaN.
+ const options = map.fits[0].options as Record<string, unknown>;
+ expect(Object.keys(options).filter((key) => options[key] === undefined)).toEqual([]);
+ });
+
it("leaves a map that has a saved view exactly where the reader put it", async () => {
const map = fakeMap();
diff --git a/apps/client/src/widgets/collections/geomap/Markers.tsx b/apps/client/src/widgets/collections/geomap/Markers.tsx
index 69cb573180..0d5ce079c5 100644
--- a/apps/client/src/widgets/collections/geomap/Markers.tsx
+++ b/apps/client/src/widgets/collections/geomap/Markers.tsx
@@ -497,7 +497,13 @@ export function FitToNotes({ notes, enabled, bounds: fixedBounds }: { notes: FNo
//
// Not animated: this is where the map opens, not somewhere it is taken. Flying would show
// the stock view first and swoop off it every time a map is opened for the first time.
- map.fitBounds(bounds, { padding: FIT_PADDING, maxZoom: fixedBounds ? undefined : NOTE_ZOOM, animate: false });
+ // No `maxZoom` key at all for fixed bounds: MapLibre merges an `undefined` over its own
+ // default, and the fit then comes out NaN.
+ map.fitBounds(bounds, {
+ padding: FIT_PADDING,
+ animate: false,
+ ...(fixedBounds ? {} : { maxZoom: NOTE_ZOOM })
+ });
}, [ map, notes, space, enabled, fixedBounds ]);
return null;