Skip to content

Commit

Permalink
Fix exceptions related to getComputedStyle throwing away gl-draw layers
Browse files Browse the repository at this point in the history
  • Loading branch information
underbluewaters committed Dec 19, 2023
1 parent ca7851e commit f2ef36c
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion packages/client/src/dataLayers/MapContextManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import mapboxgl, {
AnySourceData,
AnyLayer,
Sources,
GeoJSONSource,
} from "mapbox-gl";
import {
createContext,
Expand Down Expand Up @@ -1072,6 +1073,28 @@ class MapContextManager extends EventEmitter {
sprites: SpriteDetailsFragment[];
}> {
this.resetLayersByZIndex();
// get mapbox-gl-draw related layers and sources and make sure to include
// them in the end. gl-draw has some magic to avoid their layers getting
// removed but I'm seeing errors like this when the style is updated:
//
// Uncaught TypeError: Cannot read properties of undefined (reading 'get')
// at new Ut (mapbox-gl.js:36:1)
let glDrawLayers: AnyLayer[] = [];
let glDrawSources: { [id: string]: GeoJSONSource } = {};
const existingStyle = this.map?.getStyle();
if (existingStyle) {
glDrawLayers =
existingStyle.layers?.filter((l) => l.id.indexOf("gl-draw") === 0) ||
[];
// @ts-ignore
const relatedSourceIds = glDrawLayers.map((l) => l.source || "");
for (const key in existingStyle.sources) {
if (relatedSourceIds.indexOf(key) > -1) {
glDrawSources[key] = existingStyle.sources[key] as GeoJSONSource;
}
}
}

let sprites: SpriteDetailsFragment[] = [];
const basemap = this.basemaps[this.internalState.selectedBasemap || ""] as
| BasemapDetailsFragment
Expand Down Expand Up @@ -1488,9 +1511,15 @@ class MapContextManager extends EventEmitter {
baseStyle.sources = {
...baseStyle.sources,
...this.dynamicDataSources,
...glDrawSources,
};

baseStyle.layers = [...underLabels, ...overLabels, ...this.dynamicLayers];
baseStyle.layers = [
...underLabels,
...overLabels,
...this.dynamicLayers,
...glDrawLayers,
];

// Evaluate any basemap optional layers
// value is whether to toggle
Expand Down

0 comments on commit f2ef36c

Please sign in to comment.