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): drop an addition whose source is the style that is leaving
Elian Doranb711113697e2
diff --git a/apps/client/src/widgets/collections/geomap/Buildings.spec.tsx b/apps/client/src/widgets/collections/geomap/Buildings.spec.tsxindex e76f72a6cd..265a34664f 100644--- a/apps/client/src/widgets/collections/geomap/Buildings.spec.tsx+++ b/apps/client/src/widgets/collections/geomap/Buildings.spec.tsx@@ -215,6 +215,22 @@ describe("geo map Buildings", () => {expect(extrusion(map)?.paint?.["fill-extrusion-color"]).not.toBe(overLightMap);});+ it("takes them down on a style that arrives with nothing to stand up", () => {+ // Switching to the raster map while the view is leaned over. `keepAdditions` now leaves the+ // layer behind rather than handing it on without its source (see map.spec), but this does+ // not lean on that: whatever a style load brings, the layer is left standing only where+ // there is something under it to draw from.+ const map = fakeMap();+ renderBuildings(map);+ act(() => map.tiltTo(45));+ const carriedOver = extrusion(map);+ if (!carriedOver) throw new Error("nothing to carry over");++ act(() => map.loadStyle([ ...rasterStyleLayers(), carriedOver ]));++ expect(extrusion(map)).toBeUndefined();+ });+it("stops listening to a map it is taken off, giving the flat buildings back", () => {const map = fakeMap();const container = renderBuildings(map);diff --git a/apps/client/src/widgets/collections/geomap/Buildings.tsx b/apps/client/src/widgets/collections/geomap/Buildings.tsxindex c2db3db15f..5401592d1c 100644--- a/apps/client/src/widgets/collections/geomap/Buildings.tsx+++ b/apps/client/src/widgets/collections/geomap/Buildings.tsx@@ -83,11 +83,13 @@ export default function Buildings({ isDarkTheme }: { isDarkTheme: boolean }) {* for the wrong map. Adding it afresh settles both without a separate path for each.*/function install(map: MapLibreGLMap, isDarkTheme: boolean) {- if (!map.getSource(SHORTBREAD_SOURCE)) return;-+ // Taken down before the style is asked whether it has anything to put up, so that what this+ // leaves behind is the same either way: the layer stands only where there is a source under it.if (map.getLayer(BUILDINGS_LAYER)) {map.removeLayer(BUILDINGS_LAYER);}++ if (!map.getSource(SHORTBREAD_SOURCE)) return;// Under the style's own labels rather than over them: a place name buried behind a tower reads// as a missing label, and every 3D map worth copying puts its names on top. Under the markers// too, as a consequence — they are symbol layers added after the style's own.diff --git a/apps/client/src/widgets/collections/geomap/map.spec.ts b/apps/client/src/widgets/collections/geomap/map.spec.tsindex 542ca06f9f..bd1c1133cf 100644--- a/apps/client/src/widgets/collections/geomap/map.spec.ts+++ b/apps/client/src/widgets/collections/geomap/map.spec.ts@@ -74,6 +74,40 @@ describe("keepAdditions", () => {expect(transformed.layers).toHaveLength(1);});+ /**+ * The 3D buildings, which draw from the vector style's own source rather than from one of their+ * own (see Buildings). Switching to the raster map used to carry the layer across and leave the+ * source behind, and MapLibre validates a whole style before applying any of it: the switch was+ * refused outright with `source "versatiles-shortbread" not found`, no `style.load` fired, and+ * the map stayed on the very style the reader had just asked to leave.+ */+ it("leaves behind an addition whose source belongs to the style that is going", () => {+ const buildings3d = {+ "id": "buildings-3d",+ "type": "fill-extrusion",+ "source": "openmaptiles",+ "source-layer": "buildings"+ } as LayerSpecification;+ const withBuildings: StyleSpecification = {+ ...applied,+ layers: [ ...applied.layers, buildings3d ]+ };++ const transformed = keepAdditions(styleContents(applied))(withBuildings, next);++ expect(transformed.layers.map((layer) => layer.id)).toEqual([ "land", "roads" ]);+ expect(Object.keys(transformed.sources)).toEqual([ "versatiles" ]);+ });++ it("still carries an addition that brought its own source along", () => {+ // The markers and the tracks, which are not affected by the rule above: their source is+ // carried across with them, so the layer naming it is satisfied in the incoming style.+ const transformed = keepAdditions(styleContents(applied))(withMarkers(applied), next);++ expect(transformed.layers.map((layer) => layer.id)).toContain("points-layer");+ expect(Object.keys(transformed.sources)).toContain("points");+ });+/*** 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`,diff --git a/apps/client/src/widgets/collections/geomap/map.tsx b/apps/client/src/widgets/collections/geomap/map.tsxindex ada546aded..769140f33d 100644--- a/apps/client/src/widgets/collections/geomap/map.tsx+++ b/apps/client/src/widgets/collections/geomap/map.tsx@@ -496,6 +496,9 @@ export function styleContents(style: StyleSpecification): StyleContents {* 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.*+ * An addition that drew from the outgoing style's *own* source is the exception, and is dropped+ * rather than handed on: there is nothing to hand it: see the note where that is decided below.+ ** 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@@ -519,9 +522,17 @@ export function keepAdditions(applied: StyleContents | undefined): TransformStylconst 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);- }+ if (applied.layers.has(layer.id) || nextLayers.has(layer.id)) continue;+ // An addition drawing from a source that is not in the incoming style and was not+ // carried across either — which means it drew from the outgoing style's own, as the 3D+ // buildings do (see Buildings). Nothing can be done for it here: the source is the map+ // that has just gone, and carrying that over would leave the old map drawn on top of+ // the new one. It has to be left behind rather than handed on broken, because MapLibre+ // validates the whole style before applying any of it — one layer naming a source that+ // is not there and the switch is refused outright, leaving the map on the style the+ // reader has just asked to leave.+ if ("source" in layer && layer.source && !(layer.source in sources)) continue;+ layers.push(layer);}return { ...next, sources, layers };