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