maplibre-gl-layer-control 0.17.3 → 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
@@ -724,6 +731,7 @@ class LayerControl {
724
731
  __publicField(this, "onLayerRename");
725
732
  __publicField(this, "onLayerReorder");
726
733
  __publicField(this, "onLayerRemove");
734
+ __publicField(this, "onLayerStyleChange");
727
735
  __publicField(this, "onBackgroundVisibilityChange");
728
736
  __publicField(this, "onBackgroundOpacityChange");
729
737
  // Background-layer visibility presets
@@ -746,6 +754,7 @@ class LayerControl {
746
754
  this.onLayerRename = options.onLayerRename;
747
755
  this.onLayerReorder = options.onLayerReorder;
748
756
  this.onLayerRemove = options.onLayerRemove;
757
+ this.onLayerStyleChange = options.onLayerStyleChange;
749
758
  this.onBackgroundVisibilityChange = options.onBackgroundVisibilityChange;
750
759
  this.onBackgroundOpacityChange = options.onBackgroundOpacityChange;
751
760
  this.enableBackgroundPresets = options.enableBackgroundPresets !== false;
@@ -2637,7 +2646,7 @@ class LayerControl {
2637
2646
  actions.className = "style-editor-actions";
2638
2647
  const removeBtn = document.createElement("button");
2639
2648
  removeBtn.className = "style-editor-button style-editor-button-remove";
2640
- removeBtn.textContent = "Remove";
2649
+ removeBtn.textContent = "Remove Layer";
2641
2650
  removeBtn.title = "Remove layer from map";
2642
2651
  removeBtn.addEventListener("click", (e) => {
2643
2652
  e.stopPropagation();
@@ -2718,18 +2727,33 @@ class LayerControl {
2718
2727
  actions.className = "style-editor-actions";
2719
2728
  const resetBtn = document.createElement("button");
2720
2729
  resetBtn.className = "style-editor-button style-editor-button-reset";
2721
- resetBtn.textContent = "Reset";
2730
+ resetBtn.textContent = "Reset Style";
2722
2731
  resetBtn.addEventListener("click", (e) => {
2723
2732
  e.stopPropagation();
2724
2733
  for (const nativeId of nativeLayerIds) {
2725
2734
  restoreOriginalStyle(this.map, nativeId, this.state.originalStyles);
2726
2735
  }
2736
+ if (this.onLayerStyleChange) {
2737
+ editor.querySelectorAll("[data-property]").forEach((el) => {
2738
+ const control = el;
2739
+ const property = control.dataset.property;
2740
+ const sourceId = control.dataset.layerId;
2741
+ if (!property || !sourceId) return;
2742
+ const value = this.map.getPaintProperty(sourceId, property);
2743
+ if (value !== void 0) {
2744
+ this.notifyLayerStyleChange(
2745
+ property,
2746
+ typeof value === "number" ? value : normalizeColor(value)
2747
+ );
2748
+ }
2749
+ });
2750
+ }
2727
2751
  this.closeStyleEditor(layerId);
2728
2752
  this.openStyleEditor(layerId);
2729
2753
  });
2730
2754
  const removeBtn = document.createElement("button");
2731
2755
  removeBtn.className = "style-editor-button style-editor-button-remove";
2732
- removeBtn.textContent = "Remove";
2756
+ removeBtn.textContent = "Remove Layer";
2733
2757
  removeBtn.title = "Remove layer from map";
2734
2758
  removeBtn.addEventListener("click", (e) => {
2735
2759
  e.stopPropagation();
@@ -2805,14 +2829,14 @@ class LayerControl {
2805
2829
  actions.className = "style-editor-actions";
2806
2830
  const resetBtn = document.createElement("button");
2807
2831
  resetBtn.className = "style-editor-button style-editor-button-reset";
2808
- resetBtn.textContent = "Reset";
2832
+ resetBtn.textContent = "Reset Style";
2809
2833
  resetBtn.addEventListener("click", (e) => {
2810
2834
  e.stopPropagation();
2811
2835
  this.resetLayerStyle(layerId);
2812
2836
  });
2813
2837
  const removeBtn = document.createElement("button");
2814
2838
  removeBtn.className = "style-editor-button style-editor-button-remove";
2815
- removeBtn.textContent = "Remove";
2839
+ removeBtn.textContent = "Remove Layer";
2816
2840
  removeBtn.title = "Remove layer from map";
2817
2841
  removeBtn.addEventListener("click", (e) => {
2818
2842
  e.stopPropagation();
@@ -3160,6 +3184,86 @@ class LayerControl {
3160
3184
  );
3161
3185
  }
3162
3186
  }
3187
+ /**
3188
+ * Notify the host that a paint property changed through the style editor.
3189
+ * Reports the layer the active style editor belongs to (`activeStyleEditor`),
3190
+ * not the internal native sub-layer id a control may edit, so consumers can
3191
+ * map the change back to their own layer model.
3192
+ */
3193
+ notifyLayerStyleChange(property, value) {
3194
+ const layerId = this.state.activeStyleEditor;
3195
+ if (layerId && this.onLayerStyleChange) {
3196
+ this.onLayerStyleChange(layerId, property, value);
3197
+ }
3198
+ }
3199
+ /**
3200
+ * Re-read an open style editor's controls from the current map paint so they
3201
+ * reflect changes made elsewhere (e.g. an external sidebar writing the same
3202
+ * paint properties). Setting input `.value` programmatically does not
3203
+ * dispatch an `input` event, so this never re-triggers
3204
+ * {@link LayerControlOptions.onLayerStyleChange}. The control the user is
3205
+ * actively dragging (the focused input) is left untouched so the refresh
3206
+ * does not fight an in-progress drag.
3207
+ *
3208
+ * @param layerId Restrict the refresh to one layer's editor. Defaults to the
3209
+ * currently active editor. No-op when that editor is not open.
3210
+ */
3211
+ refreshStyleEditor(layerId) {
3212
+ const targetId = layerId ?? this.state.activeStyleEditor;
3213
+ if (!targetId) return;
3214
+ const editor = this.styleEditors.get(targetId);
3215
+ if (editor) {
3216
+ this.syncStyleEditorControlsFromMap(editor);
3217
+ }
3218
+ }
3219
+ /**
3220
+ * Update every slider/color control in a style editor from the layer's
3221
+ * current map paint. Each control records the layer it reads from in
3222
+ * `dataset.layerId` (see createSliderControl/createColorControl).
3223
+ */
3224
+ syncStyleEditorControlsFromMap(editor) {
3225
+ const active = document.activeElement;
3226
+ const sliders = editor.querySelectorAll(
3227
+ ".style-control-slider"
3228
+ );
3229
+ sliders.forEach((slider) => {
3230
+ var _a;
3231
+ if (slider === active) return;
3232
+ const property = slider.dataset.property;
3233
+ const sourceId = slider.dataset.layerId;
3234
+ if (!property || !sourceId) return;
3235
+ const value = this.map.getPaintProperty(sourceId, property);
3236
+ if (typeof value === "number") {
3237
+ slider.value = String(value);
3238
+ const valueDisplay = (_a = slider.parentElement) == null ? void 0 : _a.querySelector(
3239
+ ".style-control-value"
3240
+ );
3241
+ if (valueDisplay) {
3242
+ const step = parseFloat(slider.step);
3243
+ valueDisplay.textContent = formatNumericValue(value, step);
3244
+ }
3245
+ }
3246
+ });
3247
+ const colorPickers = editor.querySelectorAll(
3248
+ ".style-control-color-picker"
3249
+ );
3250
+ colorPickers.forEach((picker) => {
3251
+ var _a;
3252
+ if (picker === active) return;
3253
+ const property = picker.dataset.property;
3254
+ const sourceId = picker.dataset.layerId;
3255
+ if (!property || !sourceId) return;
3256
+ const value = this.map.getPaintProperty(sourceId, property);
3257
+ if (value !== void 0) {
3258
+ const hexColor = normalizeColor(value);
3259
+ picker.value = hexColor;
3260
+ const hexDisplay = (_a = picker.parentElement) == null ? void 0 : _a.querySelector(
3261
+ ".style-control-color-value"
3262
+ );
3263
+ if (hexDisplay) hexDisplay.value = hexColor;
3264
+ }
3265
+ });
3266
+ }
3163
3267
  /**
3164
3268
  * Create a color control
3165
3269
  */
@@ -3176,6 +3280,7 @@ class LayerControl {
3176
3280
  colorInput.className = "style-control-color-picker";
3177
3281
  colorInput.value = initialValue;
3178
3282
  colorInput.dataset.property = property;
3283
+ colorInput.dataset.layerId = layerId;
3179
3284
  const hexDisplay = document.createElement("input");
3180
3285
  hexDisplay.type = "text";
3181
3286
  hexDisplay.className = "style-control-color-value";
@@ -3188,6 +3293,7 @@ class LayerControl {
3188
3293
  for (const id of targetIds) {
3189
3294
  this.map.setPaintProperty(id, property, color);
3190
3295
  }
3296
+ this.notifyLayerStyleChange(property, color);
3191
3297
  });
3192
3298
  inputWrapper.appendChild(colorInput);
3193
3299
  inputWrapper.appendChild(hexDisplay);
@@ -3214,6 +3320,7 @@ class LayerControl {
3214
3320
  slider.step = String(step);
3215
3321
  slider.value = String(initialValue);
3216
3322
  slider.dataset.property = property;
3323
+ slider.dataset.layerId = layerId;
3217
3324
  const valueDisplay = document.createElement("span");
3218
3325
  valueDisplay.className = "style-control-value";
3219
3326
  valueDisplay.textContent = formatNumericValue(initialValue, step);
@@ -3224,6 +3331,7 @@ class LayerControl {
3224
3331
  for (const id of targetIds) {
3225
3332
  this.map.setPaintProperty(id, property, value);
3226
3333
  }
3334
+ this.notifyLayerStyleChange(property, value);
3227
3335
  });
3228
3336
  inputWrapper.appendChild(slider);
3229
3337
  inputWrapper.appendChild(valueDisplay);
@@ -3257,6 +3365,7 @@ class LayerControl {
3257
3365
  const step = parseFloat(slider.step);
3258
3366
  valueDisplay.textContent = formatNumericValue(value, step);
3259
3367
  }
3368
+ this.notifyLayerStyleChange(property, value);
3260
3369
  }
3261
3370
  }
3262
3371
  });
@@ -3275,8 +3384,9 @@ class LayerControl {
3275
3384
  ".style-control-color-value"
3276
3385
  );
3277
3386
  if (hexDisplay) {
3278
- hexDisplay.textContent = hexColor;
3387
+ hexDisplay.value = hexColor;
3279
3388
  }
3389
+ this.notifyLayerStyleChange(property, hexColor);
3280
3390
  }
3281
3391
  }
3282
3392
  });
@@ -3758,6 +3868,7 @@ class LayerControl {
3758
3868
  * Includes both MapLibre layers and custom layers managed by adapters.
3759
3869
  */
3760
3870
  getUserLayerIdsInMapOrder() {
3871
+ var _a;
3761
3872
  const style = this.map.getStyle();
3762
3873
  if (!(style == null ? void 0 : style.layers)) return [];
3763
3874
  const mapLayerIds = style.layers.map((l) => l.id);
@@ -3765,11 +3876,11 @@ class LayerControl {
3765
3876
  const userLayerIds = Object.keys(this.state.layerStates).filter(
3766
3877
  (id) => id !== "Background"
3767
3878
  );
3768
- const mapIndexOf = (id) => {
3769
- var _a;
3879
+ const resolvedIndexOf = (id) => {
3880
+ var _a2;
3770
3881
  const direct = indexById.get(id);
3771
3882
  if (direct !== void 0) return direct;
3772
- const nativeIds = (_a = this.customLayerRegistry) == null ? void 0 : _a.getNativeLayerIds(id);
3883
+ const nativeIds = (_a2 = this.customLayerRegistry) == null ? void 0 : _a2.getNativeLayerIds(id);
3773
3884
  if (nativeIds && nativeIds.length > 0) {
3774
3885
  let best = -1;
3775
3886
  for (const nativeId of nativeIds) {
@@ -3778,14 +3889,45 @@ class LayerControl {
3778
3889
  }
3779
3890
  if (best !== -1) return best;
3780
3891
  }
3781
- 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 };
3782
3923
  };
3783
3924
  return userLayerIds.map((id, insertionIndex) => ({
3784
3925
  id,
3785
3926
  insertionIndex,
3786
- mapIndex: mapIndexOf(id)
3927
+ ...stackKeyOf(id)
3787
3928
  })).sort((a, b) => {
3788
3929
  if (a.mapIndex !== b.mapIndex) return a.mapIndex > b.mapIndex ? -1 : 1;
3930
+ if (a.offset !== b.offset) return b.offset - a.offset;
3789
3931
  return a.insertionIndex - b.insertionIndex;
3790
3932
  }).map((entry) => entry.id);
3791
3933
  }