maplibre-gl-layer-control 0.9.2 → 0.9.3

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
@@ -2898,78 +2898,173 @@ class LayerControl {
2898
2898
  }
2899
2899
  /**
2900
2900
  * Get user layer IDs in current map order (top to bottom in UI = high z-index to low)
2901
+ * Includes both MapLibre layers and custom layers managed by adapters.
2901
2902
  */
2902
2903
  getUserLayerIdsInMapOrder() {
2903
2904
  const style = this.map.getStyle();
2904
2905
  if (!(style == null ? void 0 : style.layers)) return [];
2905
2906
  const mapLayerIds = style.layers.map((l) => l.id);
2906
2907
  const userLayerIds = Object.keys(this.state.layerStates).filter((id) => id !== "Background");
2907
- return userLayerIds.filter((id) => mapLayerIds.includes(id)).sort((a, b) => mapLayerIds.indexOf(b) - mapLayerIds.indexOf(a));
2908
+ const mapLibreLayers = userLayerIds.filter((id) => mapLayerIds.includes(id));
2909
+ const customLayers = userLayerIds.filter(
2910
+ (id) => {
2911
+ var _a;
2912
+ return ((_a = this.state.layerStates[id]) == null ? void 0 : _a.isCustomLayer) && !mapLayerIds.includes(id);
2913
+ }
2914
+ );
2915
+ const sortedMapLibreLayers = mapLibreLayers.sort((a, b) => mapLayerIds.indexOf(b) - mapLayerIds.indexOf(a));
2916
+ return [...customLayers, ...sortedMapLibreLayers];
2917
+ }
2918
+ /**
2919
+ * Check if a layer is a MapLibre layer (not a custom layer)
2920
+ */
2921
+ isMapLibreLayer(layerId) {
2922
+ return this.map.getLayer(layerId) !== void 0;
2923
+ }
2924
+ /**
2925
+ * Find the next MapLibre layer ID in a direction from a given index
2926
+ * @param layerIds Array of layer IDs
2927
+ * @param startIndex Index to start searching from
2928
+ * @param direction 1 for forward (toward bottom), -1 for backward (toward top)
2929
+ * @returns MapLibre layer ID or undefined if not found
2930
+ */
2931
+ findNextMapLibreLayer(layerIds, startIndex, direction) {
2932
+ for (let i = startIndex; direction === 1 ? i < layerIds.length : i >= 0; i += direction) {
2933
+ if (this.isMapLibreLayer(layerIds[i])) {
2934
+ return layerIds[i];
2935
+ }
2936
+ }
2937
+ return void 0;
2908
2938
  }
2909
2939
  /**
2910
2940
  * Move a layer up in UI (higher rendering order = move to higher z-index)
2911
2941
  */
2912
2942
  moveLayerUp(layerId) {
2913
- var _a;
2943
+ var _a, _b;
2914
2944
  const layerIds = this.getUserLayerIdsInMapOrder();
2915
2945
  const index = layerIds.indexOf(layerId);
2916
2946
  if (index <= 0) return;
2917
- try {
2918
- if (index === 1) {
2919
- this.map.moveLayer(layerId);
2920
- } else {
2921
- const targetBeforeId = layerIds[index - 2];
2947
+ const isCustom = (_a = this.state.layerStates[layerId]) == null ? void 0 : _a.isCustomLayer;
2948
+ if (!isCustom && this.isMapLibreLayer(layerId)) {
2949
+ try {
2950
+ const targetBeforeId = index >= 2 ? this.findNextMapLibreLayer(layerIds, index - 2, -1) : void 0;
2922
2951
  this.map.moveLayer(layerId, targetBeforeId);
2952
+ } catch (e) {
2923
2953
  }
2924
- } catch (e) {
2925
2954
  }
2926
- this.rebuildLayerItems();
2927
- (_a = this.onLayerReorder) == null ? void 0 : _a.call(this, this.getUserLayerIdsInMapOrder());
2955
+ const newLayerStates = {};
2956
+ const orderedIds = [...layerIds];
2957
+ [orderedIds[index], orderedIds[index - 1]] = [orderedIds[index - 1], orderedIds[index]];
2958
+ if (this.state.layerStates["Background"]) {
2959
+ newLayerStates["Background"] = this.state.layerStates["Background"];
2960
+ }
2961
+ orderedIds.forEach((id) => {
2962
+ if (this.state.layerStates[id]) {
2963
+ newLayerStates[id] = this.state.layerStates[id];
2964
+ }
2965
+ });
2966
+ this.state.layerStates = newLayerStates;
2967
+ this.buildLayerItems();
2968
+ (_b = this.onLayerReorder) == null ? void 0 : _b.call(this, this.getUserLayerIdsInMapOrder());
2928
2969
  }
2929
2970
  /**
2930
2971
  * Move a layer to the top (highest rendering order)
2931
2972
  */
2932
2973
  moveLayerToTop(layerId) {
2933
- var _a;
2934
- try {
2935
- this.map.moveLayer(layerId);
2936
- } catch (e) {
2974
+ var _a, _b;
2975
+ const layerIds = this.getUserLayerIdsInMapOrder();
2976
+ const index = layerIds.indexOf(layerId);
2977
+ if (index <= 0) return;
2978
+ const isCustom = (_a = this.state.layerStates[layerId]) == null ? void 0 : _a.isCustomLayer;
2979
+ if (!isCustom && this.isMapLibreLayer(layerId)) {
2980
+ try {
2981
+ this.map.moveLayer(layerId);
2982
+ } catch (e) {
2983
+ }
2984
+ }
2985
+ const newLayerStates = {};
2986
+ const orderedIds = [...layerIds];
2987
+ orderedIds.splice(index, 1);
2988
+ orderedIds.unshift(layerId);
2989
+ if (this.state.layerStates["Background"]) {
2990
+ newLayerStates["Background"] = this.state.layerStates["Background"];
2937
2991
  }
2938
- this.rebuildLayerItems();
2939
- (_a = this.onLayerReorder) == null ? void 0 : _a.call(this, this.getUserLayerIdsInMapOrder());
2992
+ orderedIds.forEach((id) => {
2993
+ if (this.state.layerStates[id]) {
2994
+ newLayerStates[id] = this.state.layerStates[id];
2995
+ }
2996
+ });
2997
+ this.state.layerStates = newLayerStates;
2998
+ this.buildLayerItems();
2999
+ (_b = this.onLayerReorder) == null ? void 0 : _b.call(this, this.getUserLayerIdsInMapOrder());
2940
3000
  }
2941
3001
  /**
2942
3002
  * Move a layer down in UI (lower rendering order = move to lower z-index)
2943
3003
  */
2944
3004
  moveLayerDown(layerId) {
2945
- var _a;
3005
+ var _a, _b;
2946
3006
  const layerIds = this.getUserLayerIdsInMapOrder();
2947
3007
  const index = layerIds.indexOf(layerId);
2948
3008
  if (index < 0 || index >= layerIds.length - 1) return;
2949
- const belowLayerId = layerIds[index + 1];
2950
- try {
2951
- this.map.moveLayer(layerId, belowLayerId);
2952
- } catch (e) {
3009
+ const isCustom = (_a = this.state.layerStates[layerId]) == null ? void 0 : _a.isCustomLayer;
3010
+ if (!isCustom && this.isMapLibreLayer(layerId)) {
3011
+ const belowLayerId = this.findNextMapLibreLayer(layerIds, index + 1, 1);
3012
+ if (belowLayerId) {
3013
+ try {
3014
+ this.map.moveLayer(layerId, belowLayerId);
3015
+ } catch (e) {
3016
+ }
3017
+ }
2953
3018
  }
2954
- this.rebuildLayerItems();
2955
- (_a = this.onLayerReorder) == null ? void 0 : _a.call(this, this.getUserLayerIdsInMapOrder());
3019
+ const newLayerStates = {};
3020
+ const orderedIds = [...layerIds];
3021
+ [orderedIds[index], orderedIds[index + 1]] = [orderedIds[index + 1], orderedIds[index]];
3022
+ if (this.state.layerStates["Background"]) {
3023
+ newLayerStates["Background"] = this.state.layerStates["Background"];
3024
+ }
3025
+ orderedIds.forEach((id) => {
3026
+ if (this.state.layerStates[id]) {
3027
+ newLayerStates[id] = this.state.layerStates[id];
3028
+ }
3029
+ });
3030
+ this.state.layerStates = newLayerStates;
3031
+ this.buildLayerItems();
3032
+ (_b = this.onLayerReorder) == null ? void 0 : _b.call(this, this.getUserLayerIdsInMapOrder());
2956
3033
  }
2957
3034
  /**
2958
3035
  * Move a layer to the bottom (lowest rendering order among user layers)
2959
3036
  */
2960
3037
  moveLayerToBottom(layerId) {
2961
- var _a;
3038
+ var _a, _b;
2962
3039
  const layerIds = this.getUserLayerIdsInMapOrder();
2963
3040
  if (layerIds.length <= 1) return;
2964
3041
  const index = layerIds.indexOf(layerId);
2965
3042
  if (index < 0 || index === layerIds.length - 1) return;
2966
- const bottomLayerId = layerIds[layerIds.length - 1];
2967
- try {
2968
- this.map.moveLayer(layerId, bottomLayerId);
2969
- } catch (e) {
3043
+ const isCustom = (_a = this.state.layerStates[layerId]) == null ? void 0 : _a.isCustomLayer;
3044
+ if (!isCustom && this.isMapLibreLayer(layerId)) {
3045
+ const bottomLayerId = this.findNextMapLibreLayer(layerIds, layerIds.length - 1, -1);
3046
+ if (bottomLayerId && bottomLayerId !== layerId) {
3047
+ try {
3048
+ this.map.moveLayer(layerId, bottomLayerId);
3049
+ } catch (e) {
3050
+ }
3051
+ }
2970
3052
  }
2971
- this.rebuildLayerItems();
2972
- (_a = this.onLayerReorder) == null ? void 0 : _a.call(this, this.getUserLayerIdsInMapOrder());
3053
+ const newLayerStates = {};
3054
+ const orderedIds = [...layerIds];
3055
+ orderedIds.splice(index, 1);
3056
+ orderedIds.push(layerId);
3057
+ if (this.state.layerStates["Background"]) {
3058
+ newLayerStates["Background"] = this.state.layerStates["Background"];
3059
+ }
3060
+ orderedIds.forEach((id) => {
3061
+ if (this.state.layerStates[id]) {
3062
+ newLayerStates[id] = this.state.layerStates[id];
3063
+ }
3064
+ });
3065
+ this.state.layerStates = newLayerStates;
3066
+ this.buildLayerItems();
3067
+ (_b = this.onLayerReorder) == null ? void 0 : _b.call(this, this.getUserLayerIdsInMapOrder());
2973
3068
  }
2974
3069
  /**
2975
3070
  * Remove a layer from the map
@@ -3008,23 +3103,6 @@ class LayerControl {
3008
3103
  }
3009
3104
  (_b = this.onLayerRemove) == null ? void 0 : _b.call(this, layerId);
3010
3105
  }
3011
- /**
3012
- * Rebuild layer items to reflect current order
3013
- */
3014
- rebuildLayerItems() {
3015
- const userLayerIds = this.getUserLayerIdsInMapOrder();
3016
- const newLayerStates = {};
3017
- if (this.state.layerStates["Background"]) {
3018
- newLayerStates["Background"] = this.state.layerStates["Background"];
3019
- }
3020
- userLayerIds.forEach((id) => {
3021
- if (this.state.layerStates[id]) {
3022
- newLayerStates[id] = this.state.layerStates[id];
3023
- }
3024
- });
3025
- this.state.layerStates = newLayerStates;
3026
- this.buildLayerItems();
3027
- }
3028
3106
  // ===== Drag and Drop Methods =====
3029
3107
  /**
3030
3108
  * Create drag handle element
@@ -3181,7 +3259,7 @@ class LayerControl {
3181
3259
  * Apply UI order to map layers
3182
3260
  */
3183
3261
  applyUIOrderToMap() {
3184
- var _a;
3262
+ var _a, _b;
3185
3263
  this.state.isStyleOperationInProgress = true;
3186
3264
  const items = this.panel.querySelectorAll(".layer-control-item");
3187
3265
  const uiLayerIds = [];
@@ -3192,18 +3270,26 @@ class LayerControl {
3192
3270
  }
3193
3271
  });
3194
3272
  const reversedIds = [...uiLayerIds].reverse();
3273
+ const style = this.map.getStyle();
3274
+ const mapLibreLayerIds = new Set(((_a = style == null ? void 0 : style.layers) == null ? void 0 : _a.map((l) => l.id)) || []);
3195
3275
  for (let i = 0; i < reversedIds.length; i++) {
3196
3276
  const layerId = reversedIds[i];
3197
- const beforeId = i > 0 ? reversedIds[i - 1] : void 0;
3198
- try {
3199
- const layer = this.map.getLayer(layerId);
3200
- if (layer) {
3201
- this.map.moveLayer(layerId, beforeId);
3277
+ if (!mapLibreLayerIds.has(layerId)) {
3278
+ continue;
3279
+ }
3280
+ let beforeId = void 0;
3281
+ for (let j = i - 1; j >= 0; j--) {
3282
+ if (mapLibreLayerIds.has(reversedIds[j])) {
3283
+ beforeId = reversedIds[j];
3284
+ break;
3202
3285
  }
3286
+ }
3287
+ try {
3288
+ this.map.moveLayer(layerId, beforeId);
3203
3289
  } catch (e) {
3204
3290
  }
3205
3291
  }
3206
- (_a = this.onLayerReorder) == null ? void 0 : _a.call(this, uiLayerIds);
3292
+ (_b = this.onLayerReorder) == null ? void 0 : _b.call(this, uiLayerIds);
3207
3293
  setTimeout(() => {
3208
3294
  this.state.isStyleOperationInProgress = false;
3209
3295
  }, 200);