maplibre-gl-layer-control 0.17.5 → 0.18.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
@@ -29,6 +29,7 @@ A comprehensive layer control for MapLibre GL with advanced styling capabilities
29
29
  - ✅ **TypeScript** - Full type safety and IntelliSense support
30
30
  - ✅ **React integration** - Optional React components and hooks
31
31
  - ✅ **Custom layer adapters** - Integrate non-MapLibre layers (deck.gl, Zarr, etc.)
32
+ - ✅ **Layer groups** - Nest an adapter's layers in collapsible, nestable folders with their own visibility checkbox and opacity slider
32
33
 
33
34
  ## Installation
34
35
 
@@ -469,6 +470,36 @@ deckLayers.set('my-deck-layer', myDeckLayer);
469
470
  deckAdapter.notifyLayerAdded('my-deck-layer');
470
471
  ```
471
472
 
473
+ #### Layer Groups
474
+
475
+ An adapter can organize its layers into collapsible folders by implementing the optional group methods. The panel then nests each layer under its group, nested groups included, and gives every group a collapse toggle, a visibility checkbox, and an opacity slider.
476
+
477
+ ```typescript
478
+ interface LayerGroupState {
479
+ id: string;
480
+ name: string;
481
+ parentId?: string; // enclosing group; omit for a top-level group
482
+ visible: boolean;
483
+ opacity: number; // 0-1
484
+ collapsed: boolean;
485
+ }
486
+
487
+ interface CustomLayerAdapter {
488
+ // ...the methods above, plus:
489
+ getGroups?(): LayerGroupState[];
490
+ getLayerGroupId?(layerId: string): string | undefined;
491
+ setGroupVisibility?(groupId: string, visible: boolean): void;
492
+ setGroupOpacity?(groupId: string, opacity: number): void;
493
+ setGroupCollapsed?(groupId: string, collapsed: boolean): void;
494
+ }
495
+ ```
496
+
497
+ - A group's `visible` and `opacity` are its own settings. The control shows and edits them, but it never folds them into the layer rows or applies them to the map: hiding or fading a group's layers is the adapter's job (for example, by ANDing the group's visibility into each child's).
498
+ - A group renders at the position of its top-most layer. Groups with no layers, directly or through a nested group, are not shown.
499
+ - Drag-and-drop and the context-menu moves keep a layer inside its own group, where a nested group counts as one block (a layer moves past it whole). Groups themselves cannot be dragged.
500
+ - **Show All** / **Hide All** also show or hide every group.
501
+ - 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.
502
+
472
503
  #### Limitations
473
504
 
474
505
  - **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.
package/dist/index.cjs CHANGED
@@ -164,6 +164,90 @@ class CustomLayerRegistry {
164
164
  }
165
165
  return false;
166
166
  }
167
+ /**
168
+ * Get the layer groups of every adapter. When two adapters define the same
169
+ * group ID, the first registered adapter's group wins.
170
+ * @returns The groups, in adapter order
171
+ */
172
+ getGroups() {
173
+ const groups = [];
174
+ const seen = /* @__PURE__ */ new Set();
175
+ this.adapters.forEach((adapter) => {
176
+ var _a;
177
+ for (const group of ((_a = adapter.getGroups) == null ? void 0 : _a.call(adapter)) ?? []) {
178
+ if (seen.has(group.id)) continue;
179
+ seen.add(group.id);
180
+ groups.push(group);
181
+ }
182
+ });
183
+ return groups;
184
+ }
185
+ /**
186
+ * Get the ID of the group a custom layer belongs to.
187
+ * @param layerId The layer ID
188
+ * @returns The group ID, or undefined when the layer is ungrouped
189
+ */
190
+ getLayerGroupId(layerId) {
191
+ var _a;
192
+ const adapter = this.getAdapterForLayer(layerId);
193
+ return (_a = adapter == null ? void 0 : adapter.getLayerGroupId) == null ? void 0 : _a.call(adapter, layerId);
194
+ }
195
+ /**
196
+ * Get the adapter that defines a group.
197
+ * @param groupId The group ID
198
+ * @returns The adapter or null if no adapter defines the group
199
+ */
200
+ getAdapterForGroup(groupId) {
201
+ var _a;
202
+ for (const adapter of this.adapters.values()) {
203
+ if ((_a = adapter.getGroups) == null ? void 0 : _a.call(adapter).some((group) => group.id === groupId)) {
204
+ return adapter;
205
+ }
206
+ }
207
+ return null;
208
+ }
209
+ /**
210
+ * Set visibility of a group.
211
+ * @param groupId The group ID
212
+ * @param visible Whether the group should be visible
213
+ * @returns true if the operation was handled by an adapter
214
+ */
215
+ setGroupVisibility(groupId, visible) {
216
+ const adapter = this.getAdapterForGroup(groupId);
217
+ if (adapter == null ? void 0 : adapter.setGroupVisibility) {
218
+ adapter.setGroupVisibility(groupId, visible);
219
+ return true;
220
+ }
221
+ return false;
222
+ }
223
+ /**
224
+ * Set opacity of a group.
225
+ * @param groupId The group ID
226
+ * @param opacity The opacity value (0-1)
227
+ * @returns true if the operation was handled by an adapter
228
+ */
229
+ setGroupOpacity(groupId, opacity) {
230
+ const adapter = this.getAdapterForGroup(groupId);
231
+ if (adapter == null ? void 0 : adapter.setGroupOpacity) {
232
+ adapter.setGroupOpacity(groupId, opacity);
233
+ return true;
234
+ }
235
+ return false;
236
+ }
237
+ /**
238
+ * Record that a group was collapsed or expanded.
239
+ * @param groupId The group ID
240
+ * @param collapsed Whether the group is now collapsed
241
+ * @returns true if the operation was handled by an adapter
242
+ */
243
+ setGroupCollapsed(groupId, collapsed) {
244
+ const adapter = this.getAdapterForGroup(groupId);
245
+ if (adapter == null ? void 0 : adapter.setGroupCollapsed) {
246
+ adapter.setGroupCollapsed(groupId, collapsed);
247
+ return true;
248
+ }
249
+ return false;
250
+ }
167
251
  /**
168
252
  * Subscribe to layer changes across all adapters.
169
253
  * @param callback Function called when layers are added or removed
@@ -709,6 +793,10 @@ class LayerControl {
709
793
  __publicField(this, "customLayerUnsubscribe", null);
710
794
  __publicField(this, "removedCustomLayerIds", /* @__PURE__ */ new Set());
711
795
  __publicField(this, "nativeLayerGroups", /* @__PURE__ */ new Map());
796
+ /** Layer groups from the custom layer adapters, as of the last panel build */
797
+ __publicField(this, "groupStates", /* @__PURE__ */ new Map());
798
+ /** Group IDs, parents, and memberships the panel was last built from */
799
+ __publicField(this, "groupStructureKey", "");
712
800
  __publicField(this, "basemapStyleUrl", null);
713
801
  __publicField(this, "basemapLayerIds", null);
714
802
  __publicField(this, "widthFrame", null);
@@ -1435,6 +1523,9 @@ class LayerControl {
1435
1523
  * Set visibility of all layers
1436
1524
  */
1437
1525
  setAllLayersVisibility(visible) {
1526
+ this.groupStates.forEach((_group, groupId) => {
1527
+ this.setGroupVisibility(groupId, visible);
1528
+ });
1438
1529
  Object.keys(this.state.layerStates).forEach((layerId) => {
1439
1530
  this.toggleLayerVisibility(layerId, visible);
1440
1531
  const itemEl = this.panel.querySelector(`[data-layer-id="${layerId}"]`);
@@ -1581,7 +1672,10 @@ class LayerControl {
1581
1672
  * Build layer items (called initially and when layers change)
1582
1673
  */
1583
1674
  buildLayerItems() {
1584
- const existingItems = this.panel.querySelectorAll(".layer-control-item");
1675
+ var _a;
1676
+ const existingItems = this.panel.querySelectorAll(
1677
+ ".layer-control-item, .layer-control-group"
1678
+ );
1585
1679
  existingItems.forEach((item) => item.remove());
1586
1680
  this.styleEditors.clear();
1587
1681
  const orderedLayerIds = this.getUserLayerIdsInMapOrder();
@@ -1594,20 +1688,302 @@ class LayerControl {
1594
1688
  if (this.state.layerStates["Background"]) {
1595
1689
  orderedLayerIds.push("Background");
1596
1690
  }
1691
+ this.groupStates = new Map(
1692
+ (((_a = this.customLayerRegistry) == null ? void 0 : _a.getGroups()) ?? []).map((group) => [
1693
+ group.id,
1694
+ { ...group }
1695
+ ])
1696
+ );
1697
+ this.groupStructureKey = this.computeGroupStructureKey(this.groupStates);
1698
+ const groupContainers = /* @__PURE__ */ new Map();
1597
1699
  orderedLayerIds.forEach((layerId) => {
1598
1700
  const state = this.state.layerStates[layerId];
1599
1701
  if (!state) {
1600
1702
  return;
1601
1703
  }
1602
1704
  if (layerId === "Background" || this.targetLayers.length === 0 || this.targetLayers.includes(layerId)) {
1603
- this.addLayerItem(layerId, state);
1705
+ const container = layerId === "Background" ? this.panel : this.getGroupContainer(
1706
+ this.getLayerGroupIdOf(layerId),
1707
+ groupContainers,
1708
+ /* @__PURE__ */ new Set()
1709
+ );
1710
+ this.addLayerItem(layerId, state, container);
1711
+ }
1712
+ });
1713
+ }
1714
+ /**
1715
+ * Get the ID of the group a layer belongs to, ignoring a group ID that no
1716
+ * adapter defines.
1717
+ * @param layerId The layer ID
1718
+ * @returns The group ID, or undefined when the layer is ungrouped
1719
+ */
1720
+ getLayerGroupIdOf(layerId) {
1721
+ var _a;
1722
+ if (this.groupStates.size === 0) return void 0;
1723
+ const groupId = (_a = this.customLayerRegistry) == null ? void 0 : _a.getLayerGroupId(layerId);
1724
+ return groupId !== void 0 && this.groupStates.has(groupId) ? groupId : void 0;
1725
+ }
1726
+ /**
1727
+ * Get the element a group's rows go into, creating the group (and any
1728
+ * enclosing groups) on first use.
1729
+ * @param groupId The group ID, or undefined for the panel's top level
1730
+ * @param containers Group child containers already created in this build
1731
+ * @param visiting Groups on the current parent chain, to stop on a cycle
1732
+ * @returns The element to append the group's rows to
1733
+ */
1734
+ getGroupContainer(groupId, containers, visiting) {
1735
+ if (groupId === void 0) return this.panel;
1736
+ const existing = containers.get(groupId);
1737
+ if (existing) return existing;
1738
+ const group = this.groupStates.get(groupId);
1739
+ if (!group || visiting.has(groupId)) return this.panel;
1740
+ visiting.add(groupId);
1741
+ const parentId = group.parentId !== void 0 && this.groupStates.has(group.parentId) ? group.parentId : void 0;
1742
+ const parent = this.getGroupContainer(parentId, containers, visiting);
1743
+ const { element, children } = this.createGroupItem(group);
1744
+ parent.appendChild(element);
1745
+ containers.set(groupId, children);
1746
+ return children;
1747
+ }
1748
+ /**
1749
+ * Summarize the group structure the panel is built from: each group's ID
1750
+ * and parent, and each layer's group. A change to any of these needs a
1751
+ * rebuild; a change to a group's name, visibility, opacity, or collapsed
1752
+ * state does not.
1753
+ * @param groups The groups to summarize
1754
+ * @returns A string that changes whenever the structure does
1755
+ */
1756
+ computeGroupStructureKey(groups) {
1757
+ if (groups.size === 0) return "";
1758
+ const memberships = Object.keys(this.state.layerStates).filter((layerId) => layerId !== "Background").map((layerId) => {
1759
+ var _a;
1760
+ return [
1761
+ layerId,
1762
+ ((_a = this.customLayerRegistry) == null ? void 0 : _a.getLayerGroupId(layerId)) ?? null
1763
+ ];
1764
+ });
1765
+ return JSON.stringify({
1766
+ groups: Array.from(groups.values(), (group) => [
1767
+ group.id,
1768
+ group.parentId ?? null
1769
+ ]),
1770
+ memberships
1771
+ });
1772
+ }
1773
+ /**
1774
+ * Create the element for a layer group: a header row with a collapse
1775
+ * toggle, visibility checkbox, name, and opacity slider, followed by a
1776
+ * container for the group's layers and nested groups.
1777
+ * @param group The group to render
1778
+ * @returns The group element and its child container
1779
+ */
1780
+ createGroupItem(group) {
1781
+ const element = document.createElement("div");
1782
+ element.className = "layer-control-group";
1783
+ element.setAttribute("data-group-id", group.id);
1784
+ const row = document.createElement("div");
1785
+ row.className = "layer-control-row layer-control-group-row";
1786
+ if (this.enableDragAndDrop) {
1787
+ row.appendChild(this.createDisabledDragHandle());
1788
+ }
1789
+ const checkbox = document.createElement("input");
1790
+ checkbox.type = "checkbox";
1791
+ checkbox.className = "layer-control-checkbox layer-control-group-checkbox";
1792
+ checkbox.addEventListener("change", () => {
1793
+ this.setGroupVisibility(group.id, checkbox.checked);
1794
+ });
1795
+ row.appendChild(checkbox);
1796
+ if (this.showLayerSymbol) {
1797
+ const symbol = document.createElement("span");
1798
+ symbol.className = "layer-control-symbol layer-control-group-symbol";
1799
+ symbol.innerHTML = `<svg viewBox="0 0 16 16" aria-hidden="true">
1800
+ <path d="M1.5 3.5h4.5l1.5 1.5h7v8.5h-13z" fill="#f2c94c" stroke="#b8902a" stroke-width="1"/>
1801
+ </svg>`;
1802
+ symbol.title = "Layer group";
1803
+ row.appendChild(symbol);
1804
+ }
1805
+ const name = document.createElement("span");
1806
+ name.className = "layer-control-name layer-control-group-name";
1807
+ row.appendChild(name);
1808
+ if (this.showOpacitySlider) {
1809
+ const opacity = document.createElement("input");
1810
+ opacity.type = "range";
1811
+ opacity.className = "layer-control-opacity layer-control-group-opacity";
1812
+ opacity.min = "0";
1813
+ opacity.max = "1";
1814
+ opacity.step = "0.01";
1815
+ opacity.addEventListener("mousedown", () => {
1816
+ this.state.userInteractingWithSlider = true;
1817
+ });
1818
+ opacity.addEventListener("mouseup", () => {
1819
+ this.state.userInteractingWithSlider = false;
1820
+ });
1821
+ opacity.addEventListener("input", () => {
1822
+ this.setGroupOpacity(group.id, parseFloat(opacity.value));
1823
+ });
1824
+ row.appendChild(opacity);
1825
+ }
1826
+ const toggle = document.createElement("button");
1827
+ toggle.type = "button";
1828
+ toggle.className = "layer-control-group-toggle";
1829
+ toggle.innerHTML = `<svg viewBox="0 0 16 16" fill="currentColor" aria-hidden="true">
1830
+ <path d="M6 4l4 4-4 4z"/>
1831
+ </svg>`;
1832
+ toggle.addEventListener("click", (e) => {
1833
+ var _a;
1834
+ e.stopPropagation();
1835
+ this.setGroupCollapsed(group.id, !((_a = this.groupStates.get(group.id)) == null ? void 0 : _a.collapsed));
1836
+ });
1837
+ row.appendChild(toggle);
1838
+ const children = document.createElement("div");
1839
+ children.className = "layer-control-group-children";
1840
+ element.appendChild(row);
1841
+ element.appendChild(children);
1842
+ this.updateGroupElement(element, group);
1843
+ return { element, children };
1844
+ }
1845
+ /**
1846
+ * Show a group's state on its element.
1847
+ * @param element The group element
1848
+ * @param group The group state to show
1849
+ */
1850
+ updateGroupElement(element, group) {
1851
+ element.classList.toggle("collapsed", group.collapsed);
1852
+ element.classList.toggle("layer-control-group-hidden", !group.visible);
1853
+ const row = element.querySelector(":scope > .layer-control-group-row");
1854
+ if (!row) return;
1855
+ const toggle = row.querySelector(
1856
+ ".layer-control-group-toggle"
1857
+ );
1858
+ if (toggle) {
1859
+ toggle.setAttribute("aria-expanded", String(!group.collapsed));
1860
+ toggle.title = group.collapsed ? "Expand group" : "Collapse group";
1861
+ toggle.setAttribute(
1862
+ "aria-label",
1863
+ `${group.collapsed ? "Expand" : "Collapse"} ${group.name}`
1864
+ );
1865
+ }
1866
+ const checkbox = row.querySelector(
1867
+ ".layer-control-group-checkbox"
1868
+ );
1869
+ if (checkbox) checkbox.checked = group.visible;
1870
+ const name = row.querySelector(".layer-control-group-name");
1871
+ if (name) {
1872
+ name.textContent = group.name;
1873
+ name.title = group.name;
1874
+ }
1875
+ const opacity = row.querySelector(
1876
+ ".layer-control-group-opacity"
1877
+ );
1878
+ if (opacity) {
1879
+ opacity.value = String(group.opacity);
1880
+ opacity.title = `Opacity: ${Math.round(group.opacity * 100)}%`;
1881
+ }
1882
+ }
1883
+ /**
1884
+ * Get the element of a rendered group.
1885
+ * @param groupId The group ID
1886
+ * @returns The group element, or null when the group is not rendered
1887
+ */
1888
+ getGroupElement(groupId) {
1889
+ const groups = this.panel.querySelectorAll(".layer-control-group");
1890
+ for (const element of Array.from(groups)) {
1891
+ if (element.dataset.groupId === groupId) {
1892
+ return element;
1604
1893
  }
1894
+ }
1895
+ return null;
1896
+ }
1897
+ /**
1898
+ * Remove group elements left without any layer row, e.g. after their last
1899
+ * layer was removed.
1900
+ */
1901
+ pruneEmptyGroups() {
1902
+ const groups = Array.from(this.panel.querySelectorAll(".layer-control-group"));
1903
+ for (const element of groups.reverse()) {
1904
+ if (!element.querySelector(".layer-control-item")) element.remove();
1905
+ }
1906
+ }
1907
+ /**
1908
+ * Set a group's visibility from the panel and report it to its adapter.
1909
+ * @param groupId The group ID
1910
+ * @param visible Whether the group should be visible
1911
+ */
1912
+ setGroupVisibility(groupId, visible) {
1913
+ var _a;
1914
+ const group = this.groupStates.get(groupId);
1915
+ if (!group) return;
1916
+ group.visible = visible;
1917
+ const element = this.getGroupElement(groupId);
1918
+ if (element) this.updateGroupElement(element, group);
1919
+ this.state.isStyleOperationInProgress = true;
1920
+ (_a = this.customLayerRegistry) == null ? void 0 : _a.setGroupVisibility(groupId, visible);
1921
+ setTimeout(() => {
1922
+ this.state.isStyleOperationInProgress = false;
1923
+ }, 200);
1924
+ }
1925
+ /**
1926
+ * Set a group's opacity from the panel and report it to its adapter.
1927
+ * @param groupId The group ID
1928
+ * @param opacity The opacity value (0-1)
1929
+ */
1930
+ setGroupOpacity(groupId, opacity) {
1931
+ var _a;
1932
+ const group = this.groupStates.get(groupId);
1933
+ if (!group) return;
1934
+ group.opacity = opacity;
1935
+ const element = this.getGroupElement(groupId);
1936
+ if (element) this.updateGroupElement(element, group);
1937
+ this.state.isStyleOperationInProgress = true;
1938
+ (_a = this.customLayerRegistry) == null ? void 0 : _a.setGroupOpacity(groupId, opacity);
1939
+ setTimeout(() => {
1940
+ this.state.isStyleOperationInProgress = false;
1941
+ }, 200);
1942
+ }
1943
+ /**
1944
+ * Collapse or expand a group in the panel and report it to its adapter.
1945
+ * @param groupId The group ID
1946
+ * @param collapsed Whether the group should be collapsed
1947
+ */
1948
+ setGroupCollapsed(groupId, collapsed) {
1949
+ var _a;
1950
+ const group = this.groupStates.get(groupId);
1951
+ if (!group) return;
1952
+ group.collapsed = collapsed;
1953
+ const element = this.getGroupElement(groupId);
1954
+ if (element) this.updateGroupElement(element, group);
1955
+ (_a = this.customLayerRegistry) == null ? void 0 : _a.setGroupCollapsed(groupId, collapsed);
1956
+ }
1957
+ /**
1958
+ * Re-read the layer groups from the custom layer adapters. Call this after
1959
+ * groups change outside the control. The panel is rebuilt when a group was
1960
+ * added, removed, or re-parented, or a layer changed group; otherwise the
1961
+ * group rows' name, visibility, opacity, and collapsed state are updated in
1962
+ * place.
1963
+ */
1964
+ refreshGroups() {
1965
+ var _a;
1966
+ if (!this.panel) return;
1967
+ const next = new Map(
1968
+ (((_a = this.customLayerRegistry) == null ? void 0 : _a.getGroups()) ?? []).map((group) => [
1969
+ group.id,
1970
+ { ...group }
1971
+ ])
1972
+ );
1973
+ if (this.computeGroupStructureKey(next) !== this.groupStructureKey) {
1974
+ this.buildLayerItems();
1975
+ return;
1976
+ }
1977
+ this.groupStates = next;
1978
+ next.forEach((group, groupId) => {
1979
+ const element = this.getGroupElement(groupId);
1980
+ if (element) this.updateGroupElement(element, group);
1605
1981
  });
1606
1982
  }
1607
1983
  /**
1608
1984
  * Add a single layer item to the panel
1609
1985
  */
1610
- addLayerItem(layerId, state) {
1986
+ addLayerItem(layerId, state, container = this.panel) {
1611
1987
  const item = document.createElement("div");
1612
1988
  item.className = "layer-control-item";
1613
1989
  item.setAttribute("data-layer-id", layerId);
@@ -1691,7 +2067,7 @@ class LayerControl {
1691
2067
  this.showContextMenu(layerId, e.clientX, e.clientY);
1692
2068
  });
1693
2069
  }
1694
- this.panel.appendChild(item);
2070
+ container.appendChild(item);
1695
2071
  }
1696
2072
  /**
1697
2073
  * Create a symbol element for a layer
@@ -3588,6 +3964,8 @@ class LayerControl {
3588
3964
  }
3589
3965
  if (layersAdded) {
3590
3966
  this.buildLayerItems();
3967
+ } else {
3968
+ this.pruneEmptyGroups();
3591
3969
  }
3592
3970
  } catch (error) {
3593
3971
  console.warn("Failed to check for new layers:", error);
@@ -3957,6 +4335,10 @@ class LayerControl {
3957
4335
  */
3958
4336
  moveLayerUp(layerId) {
3959
4337
  var _a, _b;
4338
+ if (this.groupStates.size > 0) {
4339
+ this.moveLayerWithinGroup(layerId, "up");
4340
+ return;
4341
+ }
3960
4342
  const layerIds = this.getUserLayerIdsInMapOrder();
3961
4343
  const index = layerIds.indexOf(layerId);
3962
4344
  if (index <= 0) return;
@@ -3984,13 +4366,17 @@ class LayerControl {
3984
4366
  });
3985
4367
  this.state.layerStates = newLayerStates;
3986
4368
  this.buildLayerItems();
3987
- (_b = this.onLayerReorder) == null ? void 0 : _b.call(this, this.getUserLayerIdsInMapOrder());
4369
+ (_b = this.onLayerReorder) == null ? void 0 : _b.call(this, orderedIds);
3988
4370
  }
3989
4371
  /**
3990
4372
  * Move a layer to the top (highest rendering order)
3991
4373
  */
3992
4374
  moveLayerToTop(layerId) {
3993
4375
  var _a, _b;
4376
+ if (this.groupStates.size > 0) {
4377
+ this.moveLayerWithinGroup(layerId, "top");
4378
+ return;
4379
+ }
3994
4380
  const layerIds = this.getUserLayerIdsInMapOrder();
3995
4381
  const index = layerIds.indexOf(layerId);
3996
4382
  if (index <= 0) return;
@@ -4015,13 +4401,17 @@ class LayerControl {
4015
4401
  });
4016
4402
  this.state.layerStates = newLayerStates;
4017
4403
  this.buildLayerItems();
4018
- (_b = this.onLayerReorder) == null ? void 0 : _b.call(this, this.getUserLayerIdsInMapOrder());
4404
+ (_b = this.onLayerReorder) == null ? void 0 : _b.call(this, orderedIds);
4019
4405
  }
4020
4406
  /**
4021
4407
  * Move a layer down in UI (lower rendering order = move to lower z-index)
4022
4408
  */
4023
4409
  moveLayerDown(layerId) {
4024
4410
  var _a, _b;
4411
+ if (this.groupStates.size > 0) {
4412
+ this.moveLayerWithinGroup(layerId, "down");
4413
+ return;
4414
+ }
4025
4415
  const layerIds = this.getUserLayerIdsInMapOrder();
4026
4416
  const index = layerIds.indexOf(layerId);
4027
4417
  if (index < 0 || index >= layerIds.length - 1) return;
@@ -4051,13 +4441,17 @@ class LayerControl {
4051
4441
  });
4052
4442
  this.state.layerStates = newLayerStates;
4053
4443
  this.buildLayerItems();
4054
- (_b = this.onLayerReorder) == null ? void 0 : _b.call(this, this.getUserLayerIdsInMapOrder());
4444
+ (_b = this.onLayerReorder) == null ? void 0 : _b.call(this, orderedIds);
4055
4445
  }
4056
4446
  /**
4057
4447
  * Move a layer to the bottom (lowest rendering order among user layers)
4058
4448
  */
4059
4449
  moveLayerToBottom(layerId) {
4060
4450
  var _a, _b;
4451
+ if (this.groupStates.size > 0) {
4452
+ this.moveLayerWithinGroup(layerId, "bottom");
4453
+ return;
4454
+ }
4061
4455
  const layerIds = this.getUserLayerIdsInMapOrder();
4062
4456
  if (layerIds.length <= 1) return;
4063
4457
  const index = layerIds.indexOf(layerId);
@@ -4090,7 +4484,83 @@ class LayerControl {
4090
4484
  });
4091
4485
  this.state.layerStates = newLayerStates;
4092
4486
  this.buildLayerItems();
4093
- (_b = this.onLayerReorder) == null ? void 0 : _b.call(this, this.getUserLayerIdsInMapOrder());
4487
+ (_b = this.onLayerReorder) == null ? void 0 : _b.call(this, orderedIds);
4488
+ }
4489
+ /**
4490
+ * Move a layer within its own group (or among the top-level rows), so the
4491
+ * context-menu moves never carry it out of its group. A nested group counts
4492
+ * as one block: moving up or down steps past the whole block.
4493
+ * @param layerId The layer ID
4494
+ * @param direction Where to move the layer within its group
4495
+ */
4496
+ moveLayerWithinGroup(layerId, direction) {
4497
+ var _a;
4498
+ const order = this.getUserLayerIdsInMapOrder();
4499
+ const groupId = this.getLayerGroupIdOf(layerId);
4500
+ const blocks = [];
4501
+ for (const id of order) {
4502
+ const key = this.getChildKeyWithin(id, groupId);
4503
+ if (key === null) continue;
4504
+ const lastBlock = blocks[blocks.length - 1];
4505
+ if (lastBlock && lastBlock.key === key && key !== `layer:${id}`) {
4506
+ lastBlock.ids.push(id);
4507
+ } else {
4508
+ blocks.push({ key, ids: [id] });
4509
+ }
4510
+ }
4511
+ const position = blocks.findIndex((block2) => block2.key === `layer:${layerId}`);
4512
+ const last = blocks.length - 1;
4513
+ if (position < 0) return;
4514
+ if ((direction === "up" || direction === "top") && position === 0) return;
4515
+ if ((direction === "down" || direction === "bottom") && position === last) {
4516
+ return;
4517
+ }
4518
+ const above = direction === "up" || direction === "top";
4519
+ const block = {
4520
+ up: blocks[position - 1],
4521
+ top: blocks[0],
4522
+ down: blocks[position + 1],
4523
+ bottom: blocks[last]
4524
+ }[direction];
4525
+ const anchor = above ? block.ids[0] : block.ids[block.ids.length - 1];
4526
+ const next = order.filter((id) => id !== layerId);
4527
+ const anchorIndex = next.indexOf(anchor);
4528
+ next.splice(above ? anchorIndex : anchorIndex + 1, 0, layerId);
4529
+ const newLayerStates = {};
4530
+ if (this.state.layerStates["Background"]) {
4531
+ newLayerStates["Background"] = this.state.layerStates["Background"];
4532
+ }
4533
+ next.forEach((id) => {
4534
+ if (this.state.layerStates[id]) {
4535
+ newLayerStates[id] = this.state.layerStates[id];
4536
+ }
4537
+ });
4538
+ this.state.layerStates = newLayerStates;
4539
+ this.applyLayerOrderToMap(next);
4540
+ this.buildLayerItems();
4541
+ (_a = this.onLayerReorder) == null ? void 0 : _a.call(this, next);
4542
+ }
4543
+ /**
4544
+ * Identify which row of a group a layer falls under: the layer itself when
4545
+ * it belongs to the group directly, or the nested group that holds it.
4546
+ * @param layerId The layer ID
4547
+ * @param groupId The group, or undefined for the panel's top level
4548
+ * @returns `layer:<id>` or `group:<id>`, or null when the layer is not
4549
+ * inside the group at all
4550
+ */
4551
+ getChildKeyWithin(layerId, groupId) {
4552
+ var _a;
4553
+ let current = this.getLayerGroupIdOf(layerId);
4554
+ if (current === groupId) return `layer:${layerId}`;
4555
+ const visited = /* @__PURE__ */ new Set();
4556
+ while (current !== void 0 && !visited.has(current)) {
4557
+ visited.add(current);
4558
+ const parentId = (_a = this.groupStates.get(current)) == null ? void 0 : _a.parentId;
4559
+ const parent = parentId !== void 0 && this.groupStates.has(parentId) ? parentId : void 0;
4560
+ if (parent === groupId) return `group:${current}`;
4561
+ current = parent;
4562
+ }
4563
+ return null;
4094
4564
  }
4095
4565
  /**
4096
4566
  * Show inline remove confirmation instead of browser confirm() dialog.
@@ -4176,6 +4646,7 @@ class LayerControl {
4176
4646
  if (itemEl) {
4177
4647
  itemEl.remove();
4178
4648
  }
4649
+ this.pruneEmptyGroups();
4179
4650
  (_b = this.onLayerRemove) == null ? void 0 : _b.call(this, layerId);
4180
4651
  setTimeout(() => {
4181
4652
  this.state.isStyleOperationInProgress = false;
@@ -4278,10 +4749,9 @@ class LayerControl {
4278
4749
  clone.style.top = `${currentTop + deltaY}px`;
4279
4750
  this.state.drag.startY = e.clientY;
4280
4751
  this.state.drag.currentY = e.clientY;
4281
- const items = Array.from(
4282
- this.panel.querySelectorAll(".layer-control-item:not(.dragging)")
4283
- ).filter(
4284
- (item) => item.dataset.layerId !== "Background"
4752
+ const scope = placeholder.parentElement;
4753
+ const items = Array.from((scope == null ? void 0 : scope.children) ?? []).filter(
4754
+ (item) => (item.classList.contains("layer-control-item") || item.classList.contains("layer-control-group")) && !item.classList.contains("dragging") && item.dataset.layerId !== "Background"
4285
4755
  );
4286
4756
  for (const item of items) {
4287
4757
  const itemRect = item.getBoundingClientRect();
@@ -4345,8 +4815,7 @@ class LayerControl {
4345
4815
  * Apply UI order to map layers
4346
4816
  */
4347
4817
  applyUIOrderToMap() {
4348
- var _a, _b;
4349
- this.state.isStyleOperationInProgress = true;
4818
+ var _a;
4350
4819
  const items = this.panel.querySelectorAll(".layer-control-item");
4351
4820
  const uiLayerIds = [];
4352
4821
  items.forEach((item) => {
@@ -4355,6 +4824,16 @@ class LayerControl {
4355
4824
  uiLayerIds.push(layerId);
4356
4825
  }
4357
4826
  });
4827
+ this.applyLayerOrderToMap(uiLayerIds);
4828
+ (_a = this.onLayerReorder) == null ? void 0 : _a.call(this, uiLayerIds);
4829
+ }
4830
+ /**
4831
+ * Restack the map's MapLibre layers to match a panel order.
4832
+ * @param uiLayerIds User layer IDs top-to-bottom, as the panel lists them
4833
+ */
4834
+ applyLayerOrderToMap(uiLayerIds) {
4835
+ var _a;
4836
+ this.state.isStyleOperationInProgress = true;
4358
4837
  const style = this.map.getStyle();
4359
4838
  const mapLibreLayerIds = new Set(((_a = style == null ? void 0 : style.layers) == null ? void 0 : _a.map((l) => l.id)) || []);
4360
4839
  for (let i = 0; i < uiLayerIds.length; i++) {
@@ -4374,7 +4853,6 @@ class LayerControl {
4374
4853
  } catch (e) {
4375
4854
  }
4376
4855
  }
4377
- (_b = this.onLayerReorder) == null ? void 0 : _b.call(this, uiLayerIds);
4378
4856
  setTimeout(() => {
4379
4857
  this.state.isStyleOperationInProgress = false;
4380
4858
  }, 200);