maplibre-gl-layer-control 0.17.0 → 0.17.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/dist/index.cjs CHANGED
@@ -3743,22 +3743,33 @@ class LayerControl {
3743
3743
  const style = this.map.getStyle();
3744
3744
  if (!(style == null ? void 0 : style.layers)) return [];
3745
3745
  const mapLayerIds = style.layers.map((l) => l.id);
3746
+ const indexById = new Map(mapLayerIds.map((id, i) => [id, i]));
3746
3747
  const userLayerIds = Object.keys(this.state.layerStates).filter(
3747
3748
  (id) => id !== "Background"
3748
3749
  );
3749
- const mapLibreLayers = userLayerIds.filter(
3750
- (id) => mapLayerIds.includes(id)
3751
- );
3752
- const customLayers = userLayerIds.filter(
3753
- (id) => {
3754
- var _a;
3755
- return ((_a = this.state.layerStates[id]) == null ? void 0 : _a.isCustomLayer) && !mapLayerIds.includes(id);
3750
+ const mapIndexOf = (id) => {
3751
+ var _a;
3752
+ const direct = indexById.get(id);
3753
+ if (direct !== void 0) return direct;
3754
+ const nativeIds = (_a = this.customLayerRegistry) == null ? void 0 : _a.getNativeLayerIds(id);
3755
+ if (nativeIds && nativeIds.length > 0) {
3756
+ let best = -1;
3757
+ for (const nativeId of nativeIds) {
3758
+ const idx = indexById.get(nativeId);
3759
+ if (idx !== void 0 && idx > best) best = idx;
3760
+ }
3761
+ if (best !== -1) return best;
3756
3762
  }
3757
- );
3758
- const sortedMapLibreLayers = mapLibreLayers.sort(
3759
- (a, b) => mapLayerIds.indexOf(b) - mapLayerIds.indexOf(a)
3760
- );
3761
- return [...customLayers, ...sortedMapLibreLayers];
3763
+ return Number.POSITIVE_INFINITY;
3764
+ };
3765
+ return userLayerIds.map((id, insertionIndex) => ({
3766
+ id,
3767
+ insertionIndex,
3768
+ mapIndex: mapIndexOf(id)
3769
+ })).sort((a, b) => {
3770
+ if (a.mapIndex !== b.mapIndex) return a.mapIndex > b.mapIndex ? -1 : 1;
3771
+ return a.insertionIndex - b.insertionIndex;
3772
+ }).map((entry) => entry.id);
3762
3773
  }
3763
3774
  /**
3764
3775
  * Check if a layer is a MapLibre layer (not a custom layer)