maplibre-gl-layer-control 0.17.4 → 0.17.5

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
@@ -45,6 +45,13 @@ class CustomLayerRegistry {
45
45
  });
46
46
  return ids;
47
47
  }
48
+ /**
49
+ * Get each adapter's layer IDs as a separate list, in adapter order.
50
+ * @returns One array of layer IDs per registered adapter
51
+ */
52
+ getLayerIdGroups() {
53
+ return Array.from(this.adapters.values(), (adapter) => adapter.getLayerIds());
54
+ }
48
55
  /**
49
56
  * Check if a layer ID is managed by any adapter.
50
57
  * @param layerId The layer ID to check
@@ -3861,6 +3868,7 @@ class LayerControl {
3861
3868
  * Includes both MapLibre layers and custom layers managed by adapters.
3862
3869
  */
3863
3870
  getUserLayerIdsInMapOrder() {
3871
+ var _a;
3864
3872
  const style = this.map.getStyle();
3865
3873
  if (!(style == null ? void 0 : style.layers)) return [];
3866
3874
  const mapLayerIds = style.layers.map((l) => l.id);
@@ -3868,11 +3876,11 @@ class LayerControl {
3868
3876
  const userLayerIds = Object.keys(this.state.layerStates).filter(
3869
3877
  (id) => id !== "Background"
3870
3878
  );
3871
- const mapIndexOf = (id) => {
3872
- var _a;
3879
+ const resolvedIndexOf = (id) => {
3880
+ var _a2;
3873
3881
  const direct = indexById.get(id);
3874
3882
  if (direct !== void 0) return direct;
3875
- const nativeIds = (_a = this.customLayerRegistry) == null ? void 0 : _a.getNativeLayerIds(id);
3883
+ const nativeIds = (_a2 = this.customLayerRegistry) == null ? void 0 : _a2.getNativeLayerIds(id);
3876
3884
  if (nativeIds && nativeIds.length > 0) {
3877
3885
  let best = -1;
3878
3886
  for (const nativeId of nativeIds) {
@@ -3881,14 +3889,45 @@ class LayerControl {
3881
3889
  }
3882
3890
  if (best !== -1) return best;
3883
3891
  }
3884
- return Number.POSITIVE_INFINITY;
3892
+ return void 0;
3893
+ };
3894
+ const anchors = /* @__PURE__ */ new Map();
3895
+ for (const group of ((_a = this.customLayerRegistry) == null ? void 0 : _a.getLayerIdGroups()) ?? []) {
3896
+ const resolved = group.map(resolvedIndexOf);
3897
+ const known = resolved.filter((idx) => idx !== void 0);
3898
+ if (known.length === 0) continue;
3899
+ const ids = known[0] > known[known.length - 1] ? [...group].reverse() : group;
3900
+ const indexes = ids === group ? resolved : [...resolved].reverse();
3901
+ ids.forEach((id, position) => {
3902
+ if (indexes[position] !== void 0) return;
3903
+ for (let below = position - 1; below >= 0; below--) {
3904
+ const idx = indexes[below];
3905
+ if (idx !== void 0) {
3906
+ anchors.set(id, { mapIndex: idx, offset: position - below });
3907
+ return;
3908
+ }
3909
+ }
3910
+ for (let above = position + 1; above < ids.length; above++) {
3911
+ const idx = indexes[above];
3912
+ if (idx !== void 0) {
3913
+ anchors.set(id, { mapIndex: idx, offset: position - above });
3914
+ return;
3915
+ }
3916
+ }
3917
+ });
3918
+ }
3919
+ const stackKeyOf = (id) => {
3920
+ const resolved = resolvedIndexOf(id);
3921
+ if (resolved !== void 0) return { mapIndex: resolved, offset: 0 };
3922
+ return anchors.get(id) ?? { mapIndex: Number.POSITIVE_INFINITY, offset: 0 };
3885
3923
  };
3886
3924
  return userLayerIds.map((id, insertionIndex) => ({
3887
3925
  id,
3888
3926
  insertionIndex,
3889
- mapIndex: mapIndexOf(id)
3927
+ ...stackKeyOf(id)
3890
3928
  })).sort((a, b) => {
3891
3929
  if (a.mapIndex !== b.mapIndex) return a.mapIndex > b.mapIndex ? -1 : 1;
3930
+ if (a.offset !== b.offset) return b.offset - a.offset;
3892
3931
  return a.insertionIndex - b.insertionIndex;
3893
3932
  }).map((entry) => entry.id);
3894
3933
  }