maplibre-gl-layer-control 0.17.2 → 0.17.4

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
@@ -722,6 +722,7 @@ class LayerControl {
722
722
  __publicField(this, "onLayerRename");
723
723
  __publicField(this, "onLayerReorder");
724
724
  __publicField(this, "onLayerRemove");
725
+ __publicField(this, "onLayerStyleChange");
725
726
  __publicField(this, "onBackgroundVisibilityChange");
726
727
  __publicField(this, "onBackgroundOpacityChange");
727
728
  // Background-layer visibility presets
@@ -744,6 +745,7 @@ class LayerControl {
744
745
  this.onLayerRename = options.onLayerRename;
745
746
  this.onLayerReorder = options.onLayerReorder;
746
747
  this.onLayerRemove = options.onLayerRemove;
748
+ this.onLayerStyleChange = options.onLayerStyleChange;
747
749
  this.onBackgroundVisibilityChange = options.onBackgroundVisibilityChange;
748
750
  this.onBackgroundOpacityChange = options.onBackgroundOpacityChange;
749
751
  this.enableBackgroundPresets = options.enableBackgroundPresets !== false;
@@ -1588,7 +1590,7 @@ class LayerControl {
1588
1590
  if (!state) {
1589
1591
  return;
1590
1592
  }
1591
- if (this.targetLayers.length === 0 || this.targetLayers.includes(layerId)) {
1593
+ if (layerId === "Background" || this.targetLayers.length === 0 || this.targetLayers.includes(layerId)) {
1592
1594
  this.addLayerItem(layerId, state);
1593
1595
  }
1594
1596
  });
@@ -2635,7 +2637,7 @@ class LayerControl {
2635
2637
  actions.className = "style-editor-actions";
2636
2638
  const removeBtn = document.createElement("button");
2637
2639
  removeBtn.className = "style-editor-button style-editor-button-remove";
2638
- removeBtn.textContent = "Remove";
2640
+ removeBtn.textContent = "Remove Layer";
2639
2641
  removeBtn.title = "Remove layer from map";
2640
2642
  removeBtn.addEventListener("click", (e) => {
2641
2643
  e.stopPropagation();
@@ -2716,18 +2718,33 @@ class LayerControl {
2716
2718
  actions.className = "style-editor-actions";
2717
2719
  const resetBtn = document.createElement("button");
2718
2720
  resetBtn.className = "style-editor-button style-editor-button-reset";
2719
- resetBtn.textContent = "Reset";
2721
+ resetBtn.textContent = "Reset Style";
2720
2722
  resetBtn.addEventListener("click", (e) => {
2721
2723
  e.stopPropagation();
2722
2724
  for (const nativeId of nativeLayerIds) {
2723
2725
  restoreOriginalStyle(this.map, nativeId, this.state.originalStyles);
2724
2726
  }
2727
+ if (this.onLayerStyleChange) {
2728
+ editor.querySelectorAll("[data-property]").forEach((el) => {
2729
+ const control = el;
2730
+ const property = control.dataset.property;
2731
+ const sourceId = control.dataset.layerId;
2732
+ if (!property || !sourceId) return;
2733
+ const value = this.map.getPaintProperty(sourceId, property);
2734
+ if (value !== void 0) {
2735
+ this.notifyLayerStyleChange(
2736
+ property,
2737
+ typeof value === "number" ? value : normalizeColor(value)
2738
+ );
2739
+ }
2740
+ });
2741
+ }
2725
2742
  this.closeStyleEditor(layerId);
2726
2743
  this.openStyleEditor(layerId);
2727
2744
  });
2728
2745
  const removeBtn = document.createElement("button");
2729
2746
  removeBtn.className = "style-editor-button style-editor-button-remove";
2730
- removeBtn.textContent = "Remove";
2747
+ removeBtn.textContent = "Remove Layer";
2731
2748
  removeBtn.title = "Remove layer from map";
2732
2749
  removeBtn.addEventListener("click", (e) => {
2733
2750
  e.stopPropagation();
@@ -2803,14 +2820,14 @@ class LayerControl {
2803
2820
  actions.className = "style-editor-actions";
2804
2821
  const resetBtn = document.createElement("button");
2805
2822
  resetBtn.className = "style-editor-button style-editor-button-reset";
2806
- resetBtn.textContent = "Reset";
2823
+ resetBtn.textContent = "Reset Style";
2807
2824
  resetBtn.addEventListener("click", (e) => {
2808
2825
  e.stopPropagation();
2809
2826
  this.resetLayerStyle(layerId);
2810
2827
  });
2811
2828
  const removeBtn = document.createElement("button");
2812
2829
  removeBtn.className = "style-editor-button style-editor-button-remove";
2813
- removeBtn.textContent = "Remove";
2830
+ removeBtn.textContent = "Remove Layer";
2814
2831
  removeBtn.title = "Remove layer from map";
2815
2832
  removeBtn.addEventListener("click", (e) => {
2816
2833
  e.stopPropagation();
@@ -3158,6 +3175,86 @@ class LayerControl {
3158
3175
  );
3159
3176
  }
3160
3177
  }
3178
+ /**
3179
+ * Notify the host that a paint property changed through the style editor.
3180
+ * Reports the layer the active style editor belongs to (`activeStyleEditor`),
3181
+ * not the internal native sub-layer id a control may edit, so consumers can
3182
+ * map the change back to their own layer model.
3183
+ */
3184
+ notifyLayerStyleChange(property, value) {
3185
+ const layerId = this.state.activeStyleEditor;
3186
+ if (layerId && this.onLayerStyleChange) {
3187
+ this.onLayerStyleChange(layerId, property, value);
3188
+ }
3189
+ }
3190
+ /**
3191
+ * Re-read an open style editor's controls from the current map paint so they
3192
+ * reflect changes made elsewhere (e.g. an external sidebar writing the same
3193
+ * paint properties). Setting input `.value` programmatically does not
3194
+ * dispatch an `input` event, so this never re-triggers
3195
+ * {@link LayerControlOptions.onLayerStyleChange}. The control the user is
3196
+ * actively dragging (the focused input) is left untouched so the refresh
3197
+ * does not fight an in-progress drag.
3198
+ *
3199
+ * @param layerId Restrict the refresh to one layer's editor. Defaults to the
3200
+ * currently active editor. No-op when that editor is not open.
3201
+ */
3202
+ refreshStyleEditor(layerId) {
3203
+ const targetId = layerId ?? this.state.activeStyleEditor;
3204
+ if (!targetId) return;
3205
+ const editor = this.styleEditors.get(targetId);
3206
+ if (editor) {
3207
+ this.syncStyleEditorControlsFromMap(editor);
3208
+ }
3209
+ }
3210
+ /**
3211
+ * Update every slider/color control in a style editor from the layer's
3212
+ * current map paint. Each control records the layer it reads from in
3213
+ * `dataset.layerId` (see createSliderControl/createColorControl).
3214
+ */
3215
+ syncStyleEditorControlsFromMap(editor) {
3216
+ const active = document.activeElement;
3217
+ const sliders = editor.querySelectorAll(
3218
+ ".style-control-slider"
3219
+ );
3220
+ sliders.forEach((slider) => {
3221
+ var _a;
3222
+ if (slider === active) return;
3223
+ const property = slider.dataset.property;
3224
+ const sourceId = slider.dataset.layerId;
3225
+ if (!property || !sourceId) return;
3226
+ const value = this.map.getPaintProperty(sourceId, property);
3227
+ if (typeof value === "number") {
3228
+ slider.value = String(value);
3229
+ const valueDisplay = (_a = slider.parentElement) == null ? void 0 : _a.querySelector(
3230
+ ".style-control-value"
3231
+ );
3232
+ if (valueDisplay) {
3233
+ const step = parseFloat(slider.step);
3234
+ valueDisplay.textContent = formatNumericValue(value, step);
3235
+ }
3236
+ }
3237
+ });
3238
+ const colorPickers = editor.querySelectorAll(
3239
+ ".style-control-color-picker"
3240
+ );
3241
+ colorPickers.forEach((picker) => {
3242
+ var _a;
3243
+ if (picker === active) return;
3244
+ const property = picker.dataset.property;
3245
+ const sourceId = picker.dataset.layerId;
3246
+ if (!property || !sourceId) return;
3247
+ const value = this.map.getPaintProperty(sourceId, property);
3248
+ if (value !== void 0) {
3249
+ const hexColor = normalizeColor(value);
3250
+ picker.value = hexColor;
3251
+ const hexDisplay = (_a = picker.parentElement) == null ? void 0 : _a.querySelector(
3252
+ ".style-control-color-value"
3253
+ );
3254
+ if (hexDisplay) hexDisplay.value = hexColor;
3255
+ }
3256
+ });
3257
+ }
3161
3258
  /**
3162
3259
  * Create a color control
3163
3260
  */
@@ -3174,6 +3271,7 @@ class LayerControl {
3174
3271
  colorInput.className = "style-control-color-picker";
3175
3272
  colorInput.value = initialValue;
3176
3273
  colorInput.dataset.property = property;
3274
+ colorInput.dataset.layerId = layerId;
3177
3275
  const hexDisplay = document.createElement("input");
3178
3276
  hexDisplay.type = "text";
3179
3277
  hexDisplay.className = "style-control-color-value";
@@ -3186,6 +3284,7 @@ class LayerControl {
3186
3284
  for (const id of targetIds) {
3187
3285
  this.map.setPaintProperty(id, property, color);
3188
3286
  }
3287
+ this.notifyLayerStyleChange(property, color);
3189
3288
  });
3190
3289
  inputWrapper.appendChild(colorInput);
3191
3290
  inputWrapper.appendChild(hexDisplay);
@@ -3212,6 +3311,7 @@ class LayerControl {
3212
3311
  slider.step = String(step);
3213
3312
  slider.value = String(initialValue);
3214
3313
  slider.dataset.property = property;
3314
+ slider.dataset.layerId = layerId;
3215
3315
  const valueDisplay = document.createElement("span");
3216
3316
  valueDisplay.className = "style-control-value";
3217
3317
  valueDisplay.textContent = formatNumericValue(initialValue, step);
@@ -3222,6 +3322,7 @@ class LayerControl {
3222
3322
  for (const id of targetIds) {
3223
3323
  this.map.setPaintProperty(id, property, value);
3224
3324
  }
3325
+ this.notifyLayerStyleChange(property, value);
3225
3326
  });
3226
3327
  inputWrapper.appendChild(slider);
3227
3328
  inputWrapper.appendChild(valueDisplay);
@@ -3255,6 +3356,7 @@ class LayerControl {
3255
3356
  const step = parseFloat(slider.step);
3256
3357
  valueDisplay.textContent = formatNumericValue(value, step);
3257
3358
  }
3359
+ this.notifyLayerStyleChange(property, value);
3258
3360
  }
3259
3361
  }
3260
3362
  });
@@ -3273,8 +3375,9 @@ class LayerControl {
3273
3375
  ".style-control-color-value"
3274
3376
  );
3275
3377
  if (hexDisplay) {
3276
- hexDisplay.textContent = hexColor;
3378
+ hexDisplay.value = hexColor;
3277
3379
  }
3380
+ this.notifyLayerStyleChange(property, hexColor);
3278
3381
  }
3279
3382
  }
3280
3383
  });
@@ -3351,6 +3454,7 @@ class LayerControl {
3351
3454
  const isAutoDetectMode = this.targetLayers.length === 0 || this.targetLayers.length === 1 && this.targetLayers[0] === "Background" || this.targetLayers.every(
3352
3455
  (id) => id === "Background" || this.state.layerStates[id]
3353
3456
  );
3457
+ let layersAdded = false;
3354
3458
  const newLayers = [];
3355
3459
  const useBasemapStyleDetection = this.basemapLayerIds !== null && this.basemapLayerIds.size > 0;
3356
3460
  const useInitialLayerDetection = !useBasemapStyleDetection && this.initialLayerIds !== null && this.initialLayerIds.size > 0;
@@ -3430,7 +3534,10 @@ class LayerControl {
3430
3534
  opacity,
3431
3535
  name: this.generateFriendlyName(layerId)
3432
3536
  };
3433
- this.addLayerItem(layerId, this.state.layerStates[layerId]);
3537
+ if (this.targetLayers.length > 0 && !this.targetLayers.includes(layerId)) {
3538
+ this.targetLayers.push(layerId);
3539
+ }
3540
+ layersAdded = true;
3434
3541
  });
3435
3542
  }
3436
3543
  if (this.customLayerRegistry) {
@@ -3446,7 +3553,10 @@ class LayerControl {
3446
3553
  isCustomLayer: true,
3447
3554
  customLayerType: this.customLayerRegistry.getSymbolType(layerId) || void 0
3448
3555
  };
3449
- this.addLayerItem(layerId, this.state.layerStates[layerId]);
3556
+ if (this.targetLayers.length > 0 && !this.targetLayers.includes(layerId)) {
3557
+ this.targetLayers.push(layerId);
3558
+ }
3559
+ layersAdded = true;
3450
3560
  }
3451
3561
  }
3452
3562
  });
@@ -3467,6 +3577,9 @@ class LayerControl {
3467
3577
  }
3468
3578
  });
3469
3579
  }
3580
+ if (layersAdded) {
3581
+ this.buildLayerItems();
3582
+ }
3470
3583
  } catch (error) {
3471
3584
  console.warn("Failed to check for new layers:", error);
3472
3585
  }