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.mjs CHANGED
@@ -43,6 +43,13 @@ class CustomLayerRegistry {
43
43
  });
44
44
  return ids;
45
45
  }
46
+ /**
47
+ * Get each adapter's layer IDs as a separate list, in adapter order.
48
+ * @returns One array of layer IDs per registered adapter
49
+ */
50
+ getLayerIdGroups() {
51
+ return Array.from(this.adapters.values(), (adapter) => adapter.getLayerIds());
52
+ }
46
53
  /**
47
54
  * Check if a layer ID is managed by any adapter.
48
55
  * @param layerId The layer ID to check
@@ -722,6 +729,7 @@ class LayerControl {
722
729
  __publicField(this, "onLayerRename");
723
730
  __publicField(this, "onLayerReorder");
724
731
  __publicField(this, "onLayerRemove");
732
+ __publicField(this, "onLayerStyleChange");
725
733
  __publicField(this, "onBackgroundVisibilityChange");
726
734
  __publicField(this, "onBackgroundOpacityChange");
727
735
  // Background-layer visibility presets
@@ -744,6 +752,7 @@ class LayerControl {
744
752
  this.onLayerRename = options.onLayerRename;
745
753
  this.onLayerReorder = options.onLayerReorder;
746
754
  this.onLayerRemove = options.onLayerRemove;
755
+ this.onLayerStyleChange = options.onLayerStyleChange;
747
756
  this.onBackgroundVisibilityChange = options.onBackgroundVisibilityChange;
748
757
  this.onBackgroundOpacityChange = options.onBackgroundOpacityChange;
749
758
  this.enableBackgroundPresets = options.enableBackgroundPresets !== false;
@@ -2635,7 +2644,7 @@ class LayerControl {
2635
2644
  actions.className = "style-editor-actions";
2636
2645
  const removeBtn = document.createElement("button");
2637
2646
  removeBtn.className = "style-editor-button style-editor-button-remove";
2638
- removeBtn.textContent = "Remove";
2647
+ removeBtn.textContent = "Remove Layer";
2639
2648
  removeBtn.title = "Remove layer from map";
2640
2649
  removeBtn.addEventListener("click", (e) => {
2641
2650
  e.stopPropagation();
@@ -2716,18 +2725,33 @@ class LayerControl {
2716
2725
  actions.className = "style-editor-actions";
2717
2726
  const resetBtn = document.createElement("button");
2718
2727
  resetBtn.className = "style-editor-button style-editor-button-reset";
2719
- resetBtn.textContent = "Reset";
2728
+ resetBtn.textContent = "Reset Style";
2720
2729
  resetBtn.addEventListener("click", (e) => {
2721
2730
  e.stopPropagation();
2722
2731
  for (const nativeId of nativeLayerIds) {
2723
2732
  restoreOriginalStyle(this.map, nativeId, this.state.originalStyles);
2724
2733
  }
2734
+ if (this.onLayerStyleChange) {
2735
+ editor.querySelectorAll("[data-property]").forEach((el) => {
2736
+ const control = el;
2737
+ const property = control.dataset.property;
2738
+ const sourceId = control.dataset.layerId;
2739
+ if (!property || !sourceId) return;
2740
+ const value = this.map.getPaintProperty(sourceId, property);
2741
+ if (value !== void 0) {
2742
+ this.notifyLayerStyleChange(
2743
+ property,
2744
+ typeof value === "number" ? value : normalizeColor(value)
2745
+ );
2746
+ }
2747
+ });
2748
+ }
2725
2749
  this.closeStyleEditor(layerId);
2726
2750
  this.openStyleEditor(layerId);
2727
2751
  });
2728
2752
  const removeBtn = document.createElement("button");
2729
2753
  removeBtn.className = "style-editor-button style-editor-button-remove";
2730
- removeBtn.textContent = "Remove";
2754
+ removeBtn.textContent = "Remove Layer";
2731
2755
  removeBtn.title = "Remove layer from map";
2732
2756
  removeBtn.addEventListener("click", (e) => {
2733
2757
  e.stopPropagation();
@@ -2803,14 +2827,14 @@ class LayerControl {
2803
2827
  actions.className = "style-editor-actions";
2804
2828
  const resetBtn = document.createElement("button");
2805
2829
  resetBtn.className = "style-editor-button style-editor-button-reset";
2806
- resetBtn.textContent = "Reset";
2830
+ resetBtn.textContent = "Reset Style";
2807
2831
  resetBtn.addEventListener("click", (e) => {
2808
2832
  e.stopPropagation();
2809
2833
  this.resetLayerStyle(layerId);
2810
2834
  });
2811
2835
  const removeBtn = document.createElement("button");
2812
2836
  removeBtn.className = "style-editor-button style-editor-button-remove";
2813
- removeBtn.textContent = "Remove";
2837
+ removeBtn.textContent = "Remove Layer";
2814
2838
  removeBtn.title = "Remove layer from map";
2815
2839
  removeBtn.addEventListener("click", (e) => {
2816
2840
  e.stopPropagation();
@@ -3158,6 +3182,86 @@ class LayerControl {
3158
3182
  );
3159
3183
  }
3160
3184
  }
3185
+ /**
3186
+ * Notify the host that a paint property changed through the style editor.
3187
+ * Reports the layer the active style editor belongs to (`activeStyleEditor`),
3188
+ * not the internal native sub-layer id a control may edit, so consumers can
3189
+ * map the change back to their own layer model.
3190
+ */
3191
+ notifyLayerStyleChange(property, value) {
3192
+ const layerId = this.state.activeStyleEditor;
3193
+ if (layerId && this.onLayerStyleChange) {
3194
+ this.onLayerStyleChange(layerId, property, value);
3195
+ }
3196
+ }
3197
+ /**
3198
+ * Re-read an open style editor's controls from the current map paint so they
3199
+ * reflect changes made elsewhere (e.g. an external sidebar writing the same
3200
+ * paint properties). Setting input `.value` programmatically does not
3201
+ * dispatch an `input` event, so this never re-triggers
3202
+ * {@link LayerControlOptions.onLayerStyleChange}. The control the user is
3203
+ * actively dragging (the focused input) is left untouched so the refresh
3204
+ * does not fight an in-progress drag.
3205
+ *
3206
+ * @param layerId Restrict the refresh to one layer's editor. Defaults to the
3207
+ * currently active editor. No-op when that editor is not open.
3208
+ */
3209
+ refreshStyleEditor(layerId) {
3210
+ const targetId = layerId ?? this.state.activeStyleEditor;
3211
+ if (!targetId) return;
3212
+ const editor = this.styleEditors.get(targetId);
3213
+ if (editor) {
3214
+ this.syncStyleEditorControlsFromMap(editor);
3215
+ }
3216
+ }
3217
+ /**
3218
+ * Update every slider/color control in a style editor from the layer's
3219
+ * current map paint. Each control records the layer it reads from in
3220
+ * `dataset.layerId` (see createSliderControl/createColorControl).
3221
+ */
3222
+ syncStyleEditorControlsFromMap(editor) {
3223
+ const active = document.activeElement;
3224
+ const sliders = editor.querySelectorAll(
3225
+ ".style-control-slider"
3226
+ );
3227
+ sliders.forEach((slider) => {
3228
+ var _a;
3229
+ if (slider === active) return;
3230
+ const property = slider.dataset.property;
3231
+ const sourceId = slider.dataset.layerId;
3232
+ if (!property || !sourceId) return;
3233
+ const value = this.map.getPaintProperty(sourceId, property);
3234
+ if (typeof value === "number") {
3235
+ slider.value = String(value);
3236
+ const valueDisplay = (_a = slider.parentElement) == null ? void 0 : _a.querySelector(
3237
+ ".style-control-value"
3238
+ );
3239
+ if (valueDisplay) {
3240
+ const step = parseFloat(slider.step);
3241
+ valueDisplay.textContent = formatNumericValue(value, step);
3242
+ }
3243
+ }
3244
+ });
3245
+ const colorPickers = editor.querySelectorAll(
3246
+ ".style-control-color-picker"
3247
+ );
3248
+ colorPickers.forEach((picker) => {
3249
+ var _a;
3250
+ if (picker === active) return;
3251
+ const property = picker.dataset.property;
3252
+ const sourceId = picker.dataset.layerId;
3253
+ if (!property || !sourceId) return;
3254
+ const value = this.map.getPaintProperty(sourceId, property);
3255
+ if (value !== void 0) {
3256
+ const hexColor = normalizeColor(value);
3257
+ picker.value = hexColor;
3258
+ const hexDisplay = (_a = picker.parentElement) == null ? void 0 : _a.querySelector(
3259
+ ".style-control-color-value"
3260
+ );
3261
+ if (hexDisplay) hexDisplay.value = hexColor;
3262
+ }
3263
+ });
3264
+ }
3161
3265
  /**
3162
3266
  * Create a color control
3163
3267
  */
@@ -3174,6 +3278,7 @@ class LayerControl {
3174
3278
  colorInput.className = "style-control-color-picker";
3175
3279
  colorInput.value = initialValue;
3176
3280
  colorInput.dataset.property = property;
3281
+ colorInput.dataset.layerId = layerId;
3177
3282
  const hexDisplay = document.createElement("input");
3178
3283
  hexDisplay.type = "text";
3179
3284
  hexDisplay.className = "style-control-color-value";
@@ -3186,6 +3291,7 @@ class LayerControl {
3186
3291
  for (const id of targetIds) {
3187
3292
  this.map.setPaintProperty(id, property, color);
3188
3293
  }
3294
+ this.notifyLayerStyleChange(property, color);
3189
3295
  });
3190
3296
  inputWrapper.appendChild(colorInput);
3191
3297
  inputWrapper.appendChild(hexDisplay);
@@ -3212,6 +3318,7 @@ class LayerControl {
3212
3318
  slider.step = String(step);
3213
3319
  slider.value = String(initialValue);
3214
3320
  slider.dataset.property = property;
3321
+ slider.dataset.layerId = layerId;
3215
3322
  const valueDisplay = document.createElement("span");
3216
3323
  valueDisplay.className = "style-control-value";
3217
3324
  valueDisplay.textContent = formatNumericValue(initialValue, step);
@@ -3222,6 +3329,7 @@ class LayerControl {
3222
3329
  for (const id of targetIds) {
3223
3330
  this.map.setPaintProperty(id, property, value);
3224
3331
  }
3332
+ this.notifyLayerStyleChange(property, value);
3225
3333
  });
3226
3334
  inputWrapper.appendChild(slider);
3227
3335
  inputWrapper.appendChild(valueDisplay);
@@ -3255,6 +3363,7 @@ class LayerControl {
3255
3363
  const step = parseFloat(slider.step);
3256
3364
  valueDisplay.textContent = formatNumericValue(value, step);
3257
3365
  }
3366
+ this.notifyLayerStyleChange(property, value);
3258
3367
  }
3259
3368
  }
3260
3369
  });
@@ -3273,8 +3382,9 @@ class LayerControl {
3273
3382
  ".style-control-color-value"
3274
3383
  );
3275
3384
  if (hexDisplay) {
3276
- hexDisplay.textContent = hexColor;
3385
+ hexDisplay.value = hexColor;
3277
3386
  }
3387
+ this.notifyLayerStyleChange(property, hexColor);
3278
3388
  }
3279
3389
  }
3280
3390
  });
@@ -3756,6 +3866,7 @@ class LayerControl {
3756
3866
  * Includes both MapLibre layers and custom layers managed by adapters.
3757
3867
  */
3758
3868
  getUserLayerIdsInMapOrder() {
3869
+ var _a;
3759
3870
  const style = this.map.getStyle();
3760
3871
  if (!(style == null ? void 0 : style.layers)) return [];
3761
3872
  const mapLayerIds = style.layers.map((l) => l.id);
@@ -3763,11 +3874,11 @@ class LayerControl {
3763
3874
  const userLayerIds = Object.keys(this.state.layerStates).filter(
3764
3875
  (id) => id !== "Background"
3765
3876
  );
3766
- const mapIndexOf = (id) => {
3767
- var _a;
3877
+ const resolvedIndexOf = (id) => {
3878
+ var _a2;
3768
3879
  const direct = indexById.get(id);
3769
3880
  if (direct !== void 0) return direct;
3770
- const nativeIds = (_a = this.customLayerRegistry) == null ? void 0 : _a.getNativeLayerIds(id);
3881
+ const nativeIds = (_a2 = this.customLayerRegistry) == null ? void 0 : _a2.getNativeLayerIds(id);
3771
3882
  if (nativeIds && nativeIds.length > 0) {
3772
3883
  let best = -1;
3773
3884
  for (const nativeId of nativeIds) {
@@ -3776,14 +3887,45 @@ class LayerControl {
3776
3887
  }
3777
3888
  if (best !== -1) return best;
3778
3889
  }
3779
- return Number.POSITIVE_INFINITY;
3890
+ return void 0;
3891
+ };
3892
+ const anchors = /* @__PURE__ */ new Map();
3893
+ for (const group of ((_a = this.customLayerRegistry) == null ? void 0 : _a.getLayerIdGroups()) ?? []) {
3894
+ const resolved = group.map(resolvedIndexOf);
3895
+ const known = resolved.filter((idx) => idx !== void 0);
3896
+ if (known.length === 0) continue;
3897
+ const ids = known[0] > known[known.length - 1] ? [...group].reverse() : group;
3898
+ const indexes = ids === group ? resolved : [...resolved].reverse();
3899
+ ids.forEach((id, position) => {
3900
+ if (indexes[position] !== void 0) return;
3901
+ for (let below = position - 1; below >= 0; below--) {
3902
+ const idx = indexes[below];
3903
+ if (idx !== void 0) {
3904
+ anchors.set(id, { mapIndex: idx, offset: position - below });
3905
+ return;
3906
+ }
3907
+ }
3908
+ for (let above = position + 1; above < ids.length; above++) {
3909
+ const idx = indexes[above];
3910
+ if (idx !== void 0) {
3911
+ anchors.set(id, { mapIndex: idx, offset: position - above });
3912
+ return;
3913
+ }
3914
+ }
3915
+ });
3916
+ }
3917
+ const stackKeyOf = (id) => {
3918
+ const resolved = resolvedIndexOf(id);
3919
+ if (resolved !== void 0) return { mapIndex: resolved, offset: 0 };
3920
+ return anchors.get(id) ?? { mapIndex: Number.POSITIVE_INFINITY, offset: 0 };
3780
3921
  };
3781
3922
  return userLayerIds.map((id, insertionIndex) => ({
3782
3923
  id,
3783
3924
  insertionIndex,
3784
- mapIndex: mapIndexOf(id)
3925
+ ...stackKeyOf(id)
3785
3926
  })).sort((a, b) => {
3786
3927
  if (a.mapIndex !== b.mapIndex) return a.mapIndex > b.mapIndex ? -1 : 1;
3928
+ if (a.offset !== b.offset) return b.offset - a.offset;
3787
3929
  return a.insertionIndex - b.insertionIndex;
3788
3930
  }).map((entry) => entry.id);
3789
3931
  }