maplibre-gl-layer-control 0.19.0 → 0.19.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1379,6 +1379,8 @@ class LayerControl {
1379
1379
  __publicField(this, "basemapLayerIdsProvided", false);
1380
1380
  /** Set once onAdd has listed the map's layers (after any basemapStyleUrl fetch). */
1381
1381
  __publicField(this, "layersListed", false);
1382
+ /** setLayerState calls made before the layers were listed, applied once they are. */
1383
+ __publicField(this, "pendingLayerStates", /* @__PURE__ */ new Map());
1382
1384
  /** Panel order set through setLayerOrder, used to order layers with nothing on the map. */
1383
1385
  __publicField(this, "layerOrderHint", null);
1384
1386
  __publicField(this, "widthFrame", null);
@@ -1491,6 +1493,7 @@ class LayerControl {
1491
1493
  this.map = map;
1492
1494
  this.mapContainer = map.getContainer();
1493
1495
  this.layersListed = false;
1496
+ this.pendingLayerStates.clear();
1494
1497
  const style = this.map.getStyle();
1495
1498
  if (style && style.sources) {
1496
1499
  this.initialSourceIds = new Set(Object.keys(style.sources));
@@ -1518,7 +1521,7 @@ class LayerControl {
1518
1521
  this.autoDetectLayers();
1519
1522
  }
1520
1523
  this.buildLayerItems();
1521
- this.layersListed = true;
1524
+ this.finishListing();
1522
1525
  }).catch((error) => {
1523
1526
  console.warn(
1524
1527
  "Failed to fetch basemap style, falling back to heuristic detection:",
@@ -1528,14 +1531,14 @@ class LayerControl {
1528
1531
  this.autoDetectLayers();
1529
1532
  }
1530
1533
  this.buildLayerItems();
1531
- this.layersListed = true;
1534
+ this.finishListing();
1532
1535
  });
1533
1536
  } else {
1534
1537
  if (Object.keys(this.state.layerStates).length === 0) {
1535
1538
  this.autoDetectLayers();
1536
1539
  }
1537
1540
  this.buildLayerItems();
1538
- this.layersListed = true;
1541
+ this.finishListing();
1539
1542
  }
1540
1543
  if (!this.state.collapsed) {
1541
1544
  requestAnimationFrame(() => {
@@ -2303,17 +2306,28 @@ class LayerControl {
2303
2306
  * Only layers the control already lists are updated, with one exception:
2304
2307
  * `"Background"` adds the Background row when the control has none (a style
2305
2308
  * whose basemap lives in style imports has no root basemap layers to detect,
2306
- * so the control would otherwise show no Background row). It returns false
2307
- * until the control has listed the map's layers (after `onAdd`, and after a
2308
- * `basemapStyleUrl` fetch), since seeding state earlier would stop the
2309
- * control from detecting layers on its own.
2309
+ * so the control would otherwise show no Background row).
2310
+ *
2311
+ * Until the control has listed the map's layers (during `onAdd`, and while a
2312
+ * `basemapStyleUrl` fetch is in flight) the update is queued and applied
2313
+ * once they are listed, since seeding state earlier would stop the control
2314
+ * from detecting layers on its own.
2310
2315
  *
2311
2316
  * @param layerId The layer ID, or `"Background"` for the basemap row
2312
2317
  * @param state The fields to change; omitted fields keep their value
2313
- * @returns True if a layer state was updated or created
2318
+ * @returns True if a layer state was updated, created or queued; false
2319
+ * before the control is added to a map or for a layer it does not list
2314
2320
  */
2315
2321
  setLayerState(layerId, state) {
2316
- if (!this.panel || !this.layersListed) return false;
2322
+ if (!this.panel) return false;
2323
+ if (!this.layersListed) {
2324
+ const queued = { ...this.pendingLayerStates.get(layerId) };
2325
+ if (state.visible !== void 0) queued.visible = state.visible;
2326
+ if (state.opacity !== void 0) queued.opacity = state.opacity;
2327
+ if (state.name !== void 0) queued.name = state.name;
2328
+ this.pendingLayerStates.set(layerId, queued);
2329
+ return true;
2330
+ }
2317
2331
  let current = this.state.layerStates[layerId];
2318
2332
  if (!current) {
2319
2333
  if (layerId !== "Background") return false;
@@ -2354,6 +2368,16 @@ class LayerControl {
2354
2368
  }
2355
2369
  return true;
2356
2370
  }
2371
+ /**
2372
+ * Mark the map's layers as listed and apply setLayerState calls queued
2373
+ * before then.
2374
+ */
2375
+ finishListing() {
2376
+ this.layersListed = true;
2377
+ const pending = [...this.pendingLayerStates];
2378
+ this.pendingLayerStates.clear();
2379
+ for (const [layerId, state] of pending) this.setLayerState(layerId, state);
2380
+ }
2357
2381
  /**
2358
2382
  * Set the order the panel lists layers in, then redraw the rows.
2359
2383
  *
@@ -6249,4 +6273,4 @@ export {
6249
6273
  rgbToHex as r,
6250
6274
  setLayerOpacity as s
6251
6275
  };
6252
- //# sourceMappingURL=LayerControl-CvH3r0T2.js.map
6276
+ //# sourceMappingURL=LayerControl-Ccyj0y-D.js.map