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
fix(geomap): keep the markers on the map through a style switch
Elian Doran069d83eea1eb
diff --git a/apps/client/src/widgets/collections/geomap/map.spec.ts b/apps/client/src/widgets/collections/geomap/map.spec.tsnew file mode 100644index 0000000000..63cb5af9a0--- /dev/null+++ b/apps/client/src/widgets/collections/geomap/map.spec.ts@@ -0,0 +1,87 @@+/**+ * The style switch, which used to take the markers with it.+ *+ * Everything a child of the map draws — the note markers, a GPX track — is a source and a layer of+ * the map's style, since a map has nowhere else to put them. Switching the style therefore dropped+ * them, and they were added again only once the new style had loaded: on a slow network the map+ * stood empty for as long as that took. They are handed to the incoming style instead.+ */+import type { LayerSpecification, StyleSpecification } from "maplibre-gl";+import { describe, expect, it } from "vitest";++import { keepAdditions, styleContents } from "./map";++/** A style with the given sources and layers, as MapLibre serializes one. */+function style(sources: string[], layers: string[]): StyleSpecification {+ return {+ version: 8,+ sources: Object.fromEntries(sources.map((id) => [ id, { type: "geojson", data: { type: "FeatureCollection", features: [] } } ])),+ layers: layers.map((id) => ({ id, type: "background" } as LayerSpecification))+ };+}++/** The style with the markers on it, as the map stands once a child has added them. */+function withMarkers(base: StyleSpecification): StyleSpecification {+ return {+ ...base,+ sources: { ...base.sources, points: { type: "geojson", data: { type: "FeatureCollection", features: [] } } },+ layers: [ ...base.layers, { id: "points-layer", type: "symbol", source: "points" } as LayerSpecification ]+ };+}++describe("keepAdditions", () => {+ const applied = style([ "openmaptiles" ], [ "background", "water" ]);+ const next = style([ "versatiles" ], [ "land", "roads" ]);++ it("carries what a child added over, and leaves the old map behind", () => {+ const transformed = keepAdditions(styleContents(applied))(withMarkers(applied), next);++ // The markers came across...+ expect(Object.keys(transformed.sources)).toContain("points");+ expect(transformed.layers.map((layer) => layer.id)).toContain("points-layer");+ // ...and the style they were sitting on did not, or it would still be drawn underneath.+ expect(Object.keys(transformed.sources)).not.toContain("openmaptiles");+ expect(transformed.layers.map((layer) => layer.id)).not.toContain("water");+ // The incoming style is otherwise untouched, and what was added is drawn over it.+ expect(transformed.layers.map((layer) => layer.id)).toEqual([ "land", "roads", "points-layer" ]);+ expect(Object.keys(transformed.sources)).toContain("versatiles");+ });++ it("hands the source over as it now stands, not as it was added", () => {+ const previous = withMarkers(applied);+ const features = [ { type: "Feature", geometry: { type: "Point", coordinates: [ 1, 2 ] }, properties: {} } ];+ previous.sources.points = { type: "geojson", data: { type: "FeatureCollection", features } as never };++ const transformed = keepAdditions(styleContents(applied))(previous, next);++ // The markers survive with the notes on them: an empty source carried across would blink+ // every marker off until the next build filled it again, which is the point of carrying it.+ expect(transformed.sources.points).toEqual(previous.sources.points);+ });++ it("leaves a style alone when there is nothing on it but itself", () => {+ const transformed = keepAdditions(styleContents(applied))(applied, next);++ expect(transformed.layers.map((layer) => layer.id)).toEqual([ "land", "roads" ]);+ expect(Object.keys(transformed.sources)).toEqual([ "versatiles" ]);+ });++ it("keeps the incoming style's own when an addition shares its name", () => {+ const clashing = style([ "points" ], [ "points-layer" ]);+ const transformed = keepAdditions(styleContents(applied))(withMarkers(applied), clashing);++ expect(transformed.sources.points).toEqual(clashing.sources.points);+ expect(transformed.layers).toHaveLength(1);+ });++ /**+ * A style named by URL is fetched by MapLibre, so we never see what is in it and cannot tell its+ * sources from a child's. Carrying nothing leaves the markers to be added again on `style.load`,+ * which is what happened before any of this.+ */+ it("carries nothing when the style it is switching from is not known", () => {+ const transformed = keepAdditions(undefined)(withMarkers(applied), next);++ expect(transformed).toBe(next);+ });+});diff --git a/apps/client/src/widgets/collections/geomap/map.tsx b/apps/client/src/widgets/collections/geomap/map.tsxindex faff4c005c..1354233b87 100644--- a/apps/client/src/widgets/collections/geomap/map.tsx+++ b/apps/client/src/widgets/collections/geomap/map.tsx@@ -1,8 +1,8 @@import "maplibre-gl/dist/maplibre-gl.css";-import { Map as MapLibreGLMap, MapMouseEvent, NavigationControl, type Point, ScaleControl, type StyleSpecification } from "maplibre-gl";+import { Map as MapLibreGLMap, MapMouseEvent, NavigationControl, type Point, ScaleControl, type StyleSpecification, type TransformStyleFunction } from "maplibre-gl";import { ComponentChildren, createContext, RefObject } from "preact";-import { useEffect, useImperativeHandle, useState } from "preact/hooks";+import { useEffect, useImperativeHandle, useRef, useState } from "preact/hooks";import { getMeasurementSystem } from "../../../utils/formatters";import { useElementSize, useSyncedRef, useTriliumOption } from "../../react/hooks";@@ -79,14 +79,20 @@ export default function Map({ coordinates, zoom, layerData, viewportChanged, chiconst containerRef = useSyncedRef<HTMLDivElement>(_containerRef);useImperativeHandle(apiRef ?? null, () => map);+ // What the style this map was last given was made of, which is how the sources and layers on it+ // that are its own are told from the ones a child added. See `keepAdditions`.+ const appliedStyle = useRef<StyleContents>();// Initialize the map.useEffect(() => {if (!containerRef.current) return;+ const initialStyle = buildSyncStyle(layerData);+ appliedStyle.current = typeof initialStyle === "string" ? undefined : styleContents(initialStyle);+const mapInstance = new MapLibreGLMap({container: containerRef.current,- style: buildSyncStyle(layerData),+ style: initialStyle,center: toCenter(coordinates),zoom,minZoom: 2,@@ -123,15 +129,21 @@ export default function Map({ coordinates, zoom, layerData, viewportChanged, chilet cancelled = false;+ /** Puts a style on the map, carrying what the children added to the last one across. */+ function apply(style: StyleSpecification | string) {+ map?.setStyle(style, { transformStyle: keepAdditions(appliedStyle.current) });+ appliedStyle.current = typeof style === "string" ? undefined : styleContents(style);+ }+if (layerData.type === "vector" && typeof layerData.style !== "string") {layerData.style().then(asyncStyle => {// Guard against the layer changing again or the map being torn down while the// style was still loading.if (cancelled) return;- map.setStyle(asyncStyle);+ apply(asyncStyle);});} else {- map.setStyle(buildSyncStyle(layerData));+ apply(buildSyncStyle(layerData));}return () => {@@ -196,3 +208,70 @@ export default function Map({ coordinates, zoom, layerData, viewportChanged, chi</div>);}++/** What a style is made of, by name — all {@link keepAdditions} needs of the one it is given. */+export interface StyleContents {+ sources: Set<string>;+ layers: Set<string>;+}++/**+ * The names of everything in a style, taken now rather than held onto.+ *+ * MapLibre shallow-copies the style it is handed and goes on to change what it made of it, so a+ * style kept as an object is not necessarily the style that was applied by the time it is read back.+ * The names are all that is wanted anyway.+ */+export function styleContents(style: StyleSpecification): StyleContents {+ return {+ sources: new Set(Object.keys(style.sources)),+ layers: new Set(style.layers.map((layer) => layer.id))+ };+}++/**+ * Carries whatever was put on the outgoing style over to the incoming one.+ *+ * A style is a world of its own, and switching one takes every source and layer on the map with it —+ * including those that were never the style's to begin with. The markers and the GPX tracks live on+ * the style because a map has nowhere else to put them, so switching between a light and a dark map+ * took every marker off it and put them back only once the new style had loaded, which is a network+ * away. Handed over here instead, they are part of the incoming style before it is ever applied, and+ * so are never off the map at all.+ *+ * What was added is what the outgoing style has and the style we applied did not, which is why the+ * latter has to be known: everything else in the outgoing style is the old map itself, and carrying+ * that over would leave it drawn on top of the new one. The additions go last, so they stay above+ * the map rather than under it.+ *+ * Note that the images a layer draws with are not part of a style and do not come across (see+ * `Markers`, which puts them back on `style.load`) — and that MapLibre applies this whether it can+ * turn one style into the other or has to build the new one from scratch, so nothing here depends on+ * which of the two it chooses.+ *+ * @param applied the style this map was last given, or `undefined` where it is not known — a style+ * named by URL, whose contents we never see. Nothing is carried over then, and the+ * additions are put back on `style.load` as they were before.+ */+export function keepAdditions(applied: StyleContents | undefined): TransformStyleFunction {+ return (previous, next) => {+ if (!applied || !previous) return next;++ const sources = { ...next.sources };+ for (const [ id, source ] of Object.entries(previous.sources)) {+ if (!applied.sources.has(id) && !(id in sources)) {+ sources[id] = source;+ }+ }++ const nextLayers = new Set(next.layers.map((layer) => layer.id));+ const layers = [ ...next.layers ];+ for (const layer of previous.layers) {+ if (!applied.layers.has(layer.id) && !nextLayers.has(layer.id)) {+ layers.push(layer);+ }+ }++ return { ...next, sources, layers };+ };+}