maplibre-gl-layer-control 0.18.3 → 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 +21 -0
- package/dist/{LayerControl-BVcYNnJ3.cjs → LayerControl-2Gi-t4dS.cjs} +165 -7
- package/dist/LayerControl-2Gi-t4dS.cjs.map +1 -0
- package/dist/{LayerControl-dS3dARlw.js → LayerControl-Ccyj0y-D.js} +165 -7
- package/dist/LayerControl-Ccyj0y-D.js.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/react.cjs +1 -1
- package/dist/react.mjs +1 -1
- package/dist/types/index.d.ts +78 -4
- package/dist/types/react.d.ts +9 -0
- package/package.json +1 -1
- package/dist/LayerControl-BVcYNnJ3.cjs.map +0 -1
- package/dist/LayerControl-dS3dARlw.js.map +0 -1
package/README.md
CHANGED
|
@@ -241,6 +241,7 @@ function MapComponent() {
|
|
|
241
241
|
| `excludeLayers` | `string[]` | `undefined` | Array of wildcard patterns to exclude layers by name (e.g., `['*-temp-*', 'debug-*']`) |
|
|
242
242
|
| `customLayerAdapters` | `CustomLayerAdapter[]` | `undefined` | Adapters for non-MapLibre layers (deck.gl, Zarr, etc.) |
|
|
243
243
|
| `basemapStyleUrl` | `string` | `undefined` | URL of basemap style JSON for reliable layer detection (see below) |
|
|
244
|
+
| `basemapLayerIds` | `string[]` | `undefined` | Basemap layer IDs, when you already know them; takes precedence over `basemapStyleUrl`, and an empty array means "no basemap layers" (see below) |
|
|
244
245
|
| `enableContextMenu` | `boolean` | `true` | Enable the right-click context menu on layers (Rename, Zoom to Layer, Move Up/Down/to Top/to Bottom, Remove Layer) |
|
|
245
246
|
| `enableDragAndDrop` | `boolean` | `true` | Enable drag-and-drop reordering of layers in the panel |
|
|
246
247
|
| `onLayerRename` | `(layerId: string, oldName: string, newName: string) => void` | `undefined` | Called when a layer is renamed through the context menu |
|
|
@@ -303,6 +304,16 @@ When `basemapStyleUrl` is provided:
|
|
|
303
304
|
- All other layers (user-added) are shown individually in the control
|
|
304
305
|
- New layers added later are automatically detected as user layers
|
|
305
306
|
|
|
307
|
+
### Explicit basemap layer IDs
|
|
308
|
+
|
|
309
|
+
When the application already knows which layers are the basemap (for example, a style built in code with no URL to fetch), pass them directly. Nothing is fetched, and an empty array is honoured as "no basemap layers" instead of falling back to the heuristics below:
|
|
310
|
+
|
|
311
|
+
```javascript
|
|
312
|
+
const layerControl = new LayerControl({
|
|
313
|
+
basemapLayerIds: map.getStyle().layers.map((layer) => layer.id),
|
|
314
|
+
});
|
|
315
|
+
```
|
|
316
|
+
|
|
306
317
|
### Automatic Detection Without basemapStyleUrl
|
|
307
318
|
|
|
308
319
|
Even without `basemapStyleUrl`, the control uses source-based heuristics to detect user-added layers. Custom MapLibre layers (using `map.addLayer()`) are automatically detected whether they are added **before** or **after** the layer control - no custom adapter is needed for standard MapLibre layer types!
|
|
@@ -582,6 +593,16 @@ interface CustomLayerAdapter {
|
|
|
582
593
|
- **Show All** / **Hide All** also show or hide every group.
|
|
583
594
|
- When groups change outside the control, call `layerControl.refreshGroups()`. It rebuilds the panel when a group was added, removed, or re-parented, or a layer changed group, and otherwise updates the group rows in place.
|
|
584
595
|
|
|
596
|
+
### Driving the panel from outside
|
|
597
|
+
|
|
598
|
+
Applications that keep layer state in their own store (and drive the map from it) can mirror that state into the panel without rebuilding it:
|
|
599
|
+
|
|
600
|
+
| Method | Description |
|
|
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). 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
|
+
| `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
|
+
| `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
|
+
|
|
585
606
|
#### Limitations
|
|
586
607
|
|
|
587
608
|
- **Style Editor**: The style editor (gear icon) is not available for custom layers since they don't use MapLibre's paint properties. Clicking the gear icon will show an info panel explaining this.
|
|
@@ -1376,6 +1376,14 @@ class LayerControl {
|
|
|
1376
1376
|
__publicField(this, "groupStructureKey", "");
|
|
1377
1377
|
__publicField(this, "basemapStyleUrl", null);
|
|
1378
1378
|
__publicField(this, "basemapLayerIds", null);
|
|
1379
|
+
/** True when `basemapLayerIds` came from the option, which is authoritative even when empty. */
|
|
1380
|
+
__publicField(this, "basemapLayerIdsProvided", false);
|
|
1381
|
+
/** Set once onAdd has listed the map's layers (after any basemapStyleUrl fetch). */
|
|
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());
|
|
1385
|
+
/** Panel order set through setLayerOrder, used to order layers with nothing on the map. */
|
|
1386
|
+
__publicField(this, "layerOrderHint", null);
|
|
1379
1387
|
__publicField(this, "widthFrame", null);
|
|
1380
1388
|
// Exact-opacity input popup
|
|
1381
1389
|
__publicField(this, "opacityInputEl", null);
|
|
@@ -1465,6 +1473,19 @@ class LayerControl {
|
|
|
1465
1473
|
});
|
|
1466
1474
|
}
|
|
1467
1475
|
this.basemapStyleUrl = options.basemapStyleUrl || null;
|
|
1476
|
+
if (options.basemapLayerIds) {
|
|
1477
|
+
this.basemapLayerIds = new Set(options.basemapLayerIds);
|
|
1478
|
+
this.basemapLayerIdsProvided = true;
|
|
1479
|
+
}
|
|
1480
|
+
}
|
|
1481
|
+
/**
|
|
1482
|
+
* Whether basemap membership is decided by a known list of basemap layer IDs
|
|
1483
|
+
* (the `basemapLayerIds` option, or the layers of `basemapStyleUrl`) rather
|
|
1484
|
+
* than by heuristics. A fetched style only counts when it had layers; an
|
|
1485
|
+
* explicit list counts even when empty.
|
|
1486
|
+
*/
|
|
1487
|
+
hasBasemapLayerIds() {
|
|
1488
|
+
return this.basemapLayerIds !== null && (this.basemapLayerIdsProvided || this.basemapLayerIds.size > 0);
|
|
1468
1489
|
}
|
|
1469
1490
|
/**
|
|
1470
1491
|
* Called when the control is added to the map
|
|
@@ -1472,6 +1493,8 @@ class LayerControl {
|
|
|
1472
1493
|
onAdd(map) {
|
|
1473
1494
|
this.map = map;
|
|
1474
1495
|
this.mapContainer = map.getContainer();
|
|
1496
|
+
this.layersListed = false;
|
|
1497
|
+
this.pendingLayerStates.clear();
|
|
1475
1498
|
const style = this.map.getStyle();
|
|
1476
1499
|
if (style && style.sources) {
|
|
1477
1500
|
this.initialSourceIds = new Set(Object.keys(style.sources));
|
|
@@ -1499,6 +1522,7 @@ class LayerControl {
|
|
|
1499
1522
|
this.autoDetectLayers();
|
|
1500
1523
|
}
|
|
1501
1524
|
this.buildLayerItems();
|
|
1525
|
+
this.finishListing();
|
|
1502
1526
|
}).catch((error) => {
|
|
1503
1527
|
console.warn(
|
|
1504
1528
|
"Failed to fetch basemap style, falling back to heuristic detection:",
|
|
@@ -1508,12 +1532,14 @@ class LayerControl {
|
|
|
1508
1532
|
this.autoDetectLayers();
|
|
1509
1533
|
}
|
|
1510
1534
|
this.buildLayerItems();
|
|
1535
|
+
this.finishListing();
|
|
1511
1536
|
});
|
|
1512
1537
|
} else {
|
|
1513
1538
|
if (Object.keys(this.state.layerStates).length === 0) {
|
|
1514
1539
|
this.autoDetectLayers();
|
|
1515
1540
|
}
|
|
1516
1541
|
this.buildLayerItems();
|
|
1542
|
+
this.finishListing();
|
|
1517
1543
|
}
|
|
1518
1544
|
if (!this.state.collapsed) {
|
|
1519
1545
|
requestAnimationFrame(() => {
|
|
@@ -1596,7 +1622,7 @@ class LayerControl {
|
|
|
1596
1622
|
if (this.targetLayers.length === 0) {
|
|
1597
1623
|
const userAddedLayers = [];
|
|
1598
1624
|
const backgroundLayerIds = [];
|
|
1599
|
-
const useBasemapStyleDetection = this.
|
|
1625
|
+
const useBasemapStyleDetection = this.hasBasemapLayerIds();
|
|
1600
1626
|
const userAddedSourceIds = useBasemapStyleDetection ? /* @__PURE__ */ new Set() : this.detectUserAddedSources();
|
|
1601
1627
|
allLayerIds.forEach((layerId) => {
|
|
1602
1628
|
const layer = this.map.getLayer(layerId);
|
|
@@ -2247,19 +2273,143 @@ class LayerControl {
|
|
|
2247
2273
|
}
|
|
2248
2274
|
}
|
|
2249
2275
|
/**
|
|
2250
|
-
*
|
|
2276
|
+
* Whether the panel is collapsed (closed). Before the control is added to a
|
|
2277
|
+
* map this reflects the `collapsed` option.
|
|
2278
|
+
* @returns True while the panel is closed
|
|
2279
|
+
*/
|
|
2280
|
+
isCollapsed() {
|
|
2281
|
+
return this.state.collapsed;
|
|
2282
|
+
}
|
|
2283
|
+
/**
|
|
2284
|
+
* Expand (open) the panel. Before the control is added to a map this only
|
|
2285
|
+
* records the state, and the panel opens when it is built.
|
|
2251
2286
|
*/
|
|
2252
2287
|
expand() {
|
|
2253
2288
|
this.state.collapsed = false;
|
|
2289
|
+
if (!this.panel) return;
|
|
2254
2290
|
this.panel.classList.add("expanded");
|
|
2255
2291
|
this.updatePanelPosition();
|
|
2256
2292
|
}
|
|
2257
2293
|
/**
|
|
2258
|
-
* Collapse the panel
|
|
2294
|
+
* Collapse (close) the panel.
|
|
2259
2295
|
*/
|
|
2260
2296
|
collapse() {
|
|
2297
|
+
var _a;
|
|
2261
2298
|
this.state.collapsed = true;
|
|
2262
|
-
this.panel.classList.remove("expanded");
|
|
2299
|
+
(_a = this.panel) == null ? void 0 : _a.classList.remove("expanded");
|
|
2300
|
+
}
|
|
2301
|
+
/**
|
|
2302
|
+
* Update a layer row's visibility, opacity and name in place, without
|
|
2303
|
+
* touching the map or firing any callback. Use it to mirror changes made
|
|
2304
|
+
* outside the control (for example by an application store that owns layer
|
|
2305
|
+
* state) into an already-built panel without rebuilding it.
|
|
2306
|
+
*
|
|
2307
|
+
* Only layers the control already lists are updated, with one exception:
|
|
2308
|
+
* `"Background"` adds the Background row when the control has none (a style
|
|
2309
|
+
* whose basemap lives in style imports has no root basemap layers to detect,
|
|
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.
|
|
2316
|
+
*
|
|
2317
|
+
* @param layerId The layer ID, or `"Background"` for the basemap row
|
|
2318
|
+
* @param state The fields to change; omitted fields keep their value
|
|
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
|
|
2321
|
+
*/
|
|
2322
|
+
setLayerState(layerId, state) {
|
|
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
|
+
}
|
|
2332
|
+
let current = this.state.layerStates[layerId];
|
|
2333
|
+
if (!current) {
|
|
2334
|
+
if (layerId !== "Background") return false;
|
|
2335
|
+
current = this.state.layerStates.Background = {
|
|
2336
|
+
visible: state.visible ?? true,
|
|
2337
|
+
opacity: state.opacity ?? 1,
|
|
2338
|
+
name: state.name ?? "Background"
|
|
2339
|
+
};
|
|
2340
|
+
this.buildLayerItems();
|
|
2341
|
+
}
|
|
2342
|
+
if (state.visible !== void 0) current.visible = state.visible;
|
|
2343
|
+
if (state.opacity !== void 0) current.opacity = state.opacity;
|
|
2344
|
+
if (state.name !== void 0) {
|
|
2345
|
+
current.name = state.name;
|
|
2346
|
+
if (this.state.customLayerNames.has(layerId)) {
|
|
2347
|
+
this.state.customLayerNames.set(layerId, state.name);
|
|
2348
|
+
}
|
|
2349
|
+
}
|
|
2350
|
+
for (const item of this.rowsByLayerId().get(layerId) ?? []) {
|
|
2351
|
+
const checkbox = item.querySelector(
|
|
2352
|
+
":scope > .layer-control-row > .layer-control-checkbox"
|
|
2353
|
+
);
|
|
2354
|
+
if (checkbox) checkbox.checked = current.visible;
|
|
2355
|
+
const slider = item.querySelector(
|
|
2356
|
+
":scope > .layer-control-row > .layer-control-opacity"
|
|
2357
|
+
);
|
|
2358
|
+
if (slider) {
|
|
2359
|
+
slider.value = String(current.opacity);
|
|
2360
|
+
slider.title = `Opacity: ${Math.round(current.opacity * 100)}%`;
|
|
2361
|
+
}
|
|
2362
|
+
const name = item.querySelector(
|
|
2363
|
+
":scope > .layer-control-row > .layer-control-name"
|
|
2364
|
+
);
|
|
2365
|
+
if (name) {
|
|
2366
|
+
name.textContent = current.name;
|
|
2367
|
+
name.title = current.name;
|
|
2368
|
+
}
|
|
2369
|
+
}
|
|
2370
|
+
return true;
|
|
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
|
+
}
|
|
2382
|
+
/**
|
|
2383
|
+
* Set the order the panel lists layers in, then redraw the rows.
|
|
2384
|
+
*
|
|
2385
|
+
* Layers whose native MapLibre layers are on the map are always listed in
|
|
2386
|
+
* map stacking order; this order applies to the rest (custom-adapter layers
|
|
2387
|
+
* with nothing on the map yet, such as hidden ones). Use it to put the panel
|
|
2388
|
+
* back after an application refused a drag or context-menu move, since the
|
|
2389
|
+
* control has already moved the row by then. Layers not listed keep their
|
|
2390
|
+
* relative order after the listed ones, and Background stays at the bottom.
|
|
2391
|
+
* A hidden custom layer whose adapter has other layers on the map keeps its
|
|
2392
|
+
* slot beside them in that adapter's list, since the adapter is the source of
|
|
2393
|
+
* truth for where it sits; this order breaks the remaining ties.
|
|
2394
|
+
*
|
|
2395
|
+
* @param layerIds Layer IDs, top of the panel first
|
|
2396
|
+
*/
|
|
2397
|
+
setLayerOrder(layerIds) {
|
|
2398
|
+
const states = this.state.layerStates;
|
|
2399
|
+
const order = [];
|
|
2400
|
+
const seen = /* @__PURE__ */ new Set(["Background"]);
|
|
2401
|
+
for (const id of [...layerIds, ...Object.keys(states)]) {
|
|
2402
|
+
if (id in states && !seen.has(id)) {
|
|
2403
|
+
seen.add(id);
|
|
2404
|
+
order.push(id);
|
|
2405
|
+
}
|
|
2406
|
+
}
|
|
2407
|
+
this.layerOrderHint = order;
|
|
2408
|
+
const ordered = {};
|
|
2409
|
+
if ("Background" in states) ordered.Background = states.Background;
|
|
2410
|
+
for (const id of order) ordered[id] = states[id];
|
|
2411
|
+
this.state.layerStates = ordered;
|
|
2412
|
+
if (this.panel) this.buildLayerItems();
|
|
2263
2413
|
}
|
|
2264
2414
|
/**
|
|
2265
2415
|
* Build layer items (called initially and when layers change)
|
|
@@ -3019,7 +3169,7 @@ class LayerControl {
|
|
|
3019
3169
|
if (this.state.layerStates[layerId] !== void 0 && layerId !== "Background") {
|
|
3020
3170
|
return true;
|
|
3021
3171
|
}
|
|
3022
|
-
if (this.
|
|
3172
|
+
if (this.hasBasemapLayerIds()) {
|
|
3023
3173
|
return !this.basemapLayerIds.has(layerId);
|
|
3024
3174
|
}
|
|
3025
3175
|
if (this.initialLayerIds !== null && !this.initialLayerIds.has(layerId)) {
|
|
@@ -5076,7 +5226,7 @@ class LayerControl {
|
|
|
5076
5226
|
);
|
|
5077
5227
|
let layersAdded = false;
|
|
5078
5228
|
const newLayers = [];
|
|
5079
|
-
const useBasemapStyleDetection = this.
|
|
5229
|
+
const useBasemapStyleDetection = this.hasBasemapLayerIds();
|
|
5080
5230
|
const useInitialLayerDetection = !useBasemapStyleDetection && this.initialLayerIds !== null && this.initialLayerIds.size > 0;
|
|
5081
5231
|
currentMapLayerIds.forEach((layerId) => {
|
|
5082
5232
|
if (layerId !== "Background" && !this.state.layerStates[layerId]) {
|
|
@@ -5489,6 +5639,14 @@ class LayerControl {
|
|
|
5489
5639
|
const userLayerIds = Object.keys(this.state.layerStates).filter(
|
|
5490
5640
|
(id) => id !== "Background"
|
|
5491
5641
|
);
|
|
5642
|
+
const hint = this.layerOrderHint;
|
|
5643
|
+
if (hint) {
|
|
5644
|
+
const rank = new Map(hint.map((id, index) => [id, index]));
|
|
5645
|
+
const keyIndex = new Map(userLayerIds.map((id, index) => [id, index]));
|
|
5646
|
+
userLayerIds.sort(
|
|
5647
|
+
(a, b) => (rank.get(a) ?? hint.length + keyIndex.get(a)) - (rank.get(b) ?? hint.length + keyIndex.get(b))
|
|
5648
|
+
);
|
|
5649
|
+
}
|
|
5492
5650
|
const resolvedIndexOf = (id) => {
|
|
5493
5651
|
var _a2;
|
|
5494
5652
|
const direct = indexById.get(id);
|
|
@@ -6114,4 +6272,4 @@ exports.normalizeColor = normalizeColor;
|
|
|
6114
6272
|
exports.restoreOriginalStyle = restoreOriginalStyle;
|
|
6115
6273
|
exports.rgbToHex = rgbToHex;
|
|
6116
6274
|
exports.setLayerOpacity = setLayerOpacity;
|
|
6117
|
-
//# sourceMappingURL=LayerControl-
|
|
6275
|
+
//# sourceMappingURL=LayerControl-2Gi-t4dS.cjs.map
|