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.
package/README.md CHANGED
@@ -599,7 +599,7 @@ Applications that keep layer state in their own store (and drive the map from it
599
599
 
600
600
  | Method | Description |
601
601
  |--------|-------------|
602
- | `setLayerState(layerId, { visible?, opacity?, name? })` | Update a row's checkbox, opacity slider and name in place, without touching the map or firing callbacks. Only listed layers are updated, except that `"Background"` adds the Background row when there is none (a style whose basemap lives in style imports). Returns `false` (and does nothing) until the control has listed the map's layers, which is after `onAdd` and after any `basemapStyleUrl` fetch; otherwise `true` when a state was updated or created. |
602
+ | `setLayerState(layerId, { visible?, opacity?, name? })` | Update a row's checkbox, opacity slider and name in place, without touching the map or firing callbacks. Only listed layers are updated, except that `"Background"` adds the Background row when there is none (a style whose basemap lives in style imports). Calls made before the control has listed the map's layers (while a `basemapStyleUrl` fetch is in flight) are queued and applied once it has. Returns `false` before the control is added to a map or for a layer it does not list. |
603
603
  | `setLayerOrder(layerIds)` | Set the panel order (top first) for layers with nothing on the map to sort them by, then redraw the rows. Use it to put the panel back after refusing a drag or context-menu move reported through `onLayerReorder`. A hidden custom layer whose adapter has other layers on the map keeps its slot beside them in the adapter's list. |
604
604
  | `isCollapsed()` / `expand()` / `collapse()` | Read or change whether the panel is open, for example to remount a control in the state the user left it. |
605
605
 
@@ -1380,6 +1380,8 @@ class LayerControl {
1380
1380
  __publicField(this, "basemapLayerIdsProvided", false);
1381
1381
  /** Set once onAdd has listed the map's layers (after any basemapStyleUrl fetch). */
1382
1382
  __publicField(this, "layersListed", false);
1383
+ /** setLayerState calls made before the layers were listed, applied once they are. */
1384
+ __publicField(this, "pendingLayerStates", /* @__PURE__ */ new Map());
1383
1385
  /** Panel order set through setLayerOrder, used to order layers with nothing on the map. */
1384
1386
  __publicField(this, "layerOrderHint", null);
1385
1387
  __publicField(this, "widthFrame", null);
@@ -1492,6 +1494,7 @@ class LayerControl {
1492
1494
  this.map = map;
1493
1495
  this.mapContainer = map.getContainer();
1494
1496
  this.layersListed = false;
1497
+ this.pendingLayerStates.clear();
1495
1498
  const style = this.map.getStyle();
1496
1499
  if (style && style.sources) {
1497
1500
  this.initialSourceIds = new Set(Object.keys(style.sources));
@@ -1519,7 +1522,7 @@ class LayerControl {
1519
1522
  this.autoDetectLayers();
1520
1523
  }
1521
1524
  this.buildLayerItems();
1522
- this.layersListed = true;
1525
+ this.finishListing();
1523
1526
  }).catch((error) => {
1524
1527
  console.warn(
1525
1528
  "Failed to fetch basemap style, falling back to heuristic detection:",
@@ -1529,14 +1532,14 @@ class LayerControl {
1529
1532
  this.autoDetectLayers();
1530
1533
  }
1531
1534
  this.buildLayerItems();
1532
- this.layersListed = true;
1535
+ this.finishListing();
1533
1536
  });
1534
1537
  } else {
1535
1538
  if (Object.keys(this.state.layerStates).length === 0) {
1536
1539
  this.autoDetectLayers();
1537
1540
  }
1538
1541
  this.buildLayerItems();
1539
- this.layersListed = true;
1542
+ this.finishListing();
1540
1543
  }
1541
1544
  if (!this.state.collapsed) {
1542
1545
  requestAnimationFrame(() => {
@@ -2304,17 +2307,28 @@ class LayerControl {
2304
2307
  * Only layers the control already lists are updated, with one exception:
2305
2308
  * `"Background"` adds the Background row when the control has none (a style
2306
2309
  * whose basemap lives in style imports has no root basemap layers to detect,
2307
- * so the control would otherwise show no Background row). It returns false
2308
- * until the control has listed the map's layers (after `onAdd`, and after a
2309
- * `basemapStyleUrl` fetch), since seeding state earlier would stop the
2310
- * control from detecting layers on its own.
2310
+ * so the control would otherwise show no Background row).
2311
+ *
2312
+ * Until the control has listed the map's layers (during `onAdd`, and while a
2313
+ * `basemapStyleUrl` fetch is in flight) the update is queued and applied
2314
+ * once they are listed, since seeding state earlier would stop the control
2315
+ * from detecting layers on its own.
2311
2316
  *
2312
2317
  * @param layerId The layer ID, or `"Background"` for the basemap row
2313
2318
  * @param state The fields to change; omitted fields keep their value
2314
- * @returns True if a layer state was updated or created
2319
+ * @returns True if a layer state was updated, created or queued; false
2320
+ * before the control is added to a map or for a layer it does not list
2315
2321
  */
2316
2322
  setLayerState(layerId, state) {
2317
- if (!this.panel || !this.layersListed) return false;
2323
+ if (!this.panel) return false;
2324
+ if (!this.layersListed) {
2325
+ const queued = { ...this.pendingLayerStates.get(layerId) };
2326
+ if (state.visible !== void 0) queued.visible = state.visible;
2327
+ if (state.opacity !== void 0) queued.opacity = state.opacity;
2328
+ if (state.name !== void 0) queued.name = state.name;
2329
+ this.pendingLayerStates.set(layerId, queued);
2330
+ return true;
2331
+ }
2318
2332
  let current = this.state.layerStates[layerId];
2319
2333
  if (!current) {
2320
2334
  if (layerId !== "Background") return false;
@@ -2355,6 +2369,16 @@ class LayerControl {
2355
2369
  }
2356
2370
  return true;
2357
2371
  }
2372
+ /**
2373
+ * Mark the map's layers as listed and apply setLayerState calls queued
2374
+ * before then.
2375
+ */
2376
+ finishListing() {
2377
+ this.layersListed = true;
2378
+ const pending = [...this.pendingLayerStates];
2379
+ this.pendingLayerStates.clear();
2380
+ for (const [layerId, state] of pending) this.setLayerState(layerId, state);
2381
+ }
2358
2382
  /**
2359
2383
  * Set the order the panel lists layers in, then redraw the rows.
2360
2384
  *
@@ -6248,4 +6272,4 @@ exports.normalizeColor = normalizeColor;
6248
6272
  exports.restoreOriginalStyle = restoreOriginalStyle;
6249
6273
  exports.rgbToHex = rgbToHex;
6250
6274
  exports.setLayerOpacity = setLayerOpacity;
6251
- //# sourceMappingURL=LayerControl-BxSfUXK6.cjs.map
6275
+ //# sourceMappingURL=LayerControl-2Gi-t4dS.cjs.map