maplibre-gl-geo-editor 0.10.2 → 0.10.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.
@@ -16194,6 +16194,60 @@ class HistoryManager {
16194
16194
  function resolveImportedCount(result, fallback) {
16195
16195
  return result?.stats?.success ?? result?.success ?? fallback;
16196
16196
  }
16197
+ const isPolygonFeature = (feature2) => feature2.geometry?.type === "Polygon" || feature2.geometry?.type === "MultiPolygon";
16198
+ const positions = (feature2) => {
16199
+ const polygons = feature2.geometry.type === "Polygon" ? [feature2.geometry.coordinates] : feature2.geometry.coordinates;
16200
+ return polygons.flatMap((polygon2) => polygon2.flatMap((ring) => ring));
16201
+ };
16202
+ const samePosition = (left, right, tolerance) => Math.abs(left[0] - right[0]) <= tolerance && Math.abs(left[1] - right[1]) <= tolerance;
16203
+ function removePolygonOverlaps(feature2, existingFeatures) {
16204
+ if (!isPolygonFeature(feature2)) return null;
16205
+ const masks = existingFeatures.filter(isPolygonFeature);
16206
+ if (masks.length === 0) return feature2;
16207
+ const result = difference2(
16208
+ featureCollection([feature2, ...masks])
16209
+ );
16210
+ if (!result) return null;
16211
+ result.id = feature2.id;
16212
+ result.properties = feature2.properties;
16213
+ return result;
16214
+ }
16215
+ function propagateSharedVertexMoves(oldFeature, newFeature, targetFeatures, tolerance = 1e-10) {
16216
+ if (!isPolygonFeature(oldFeature) || !isPolygonFeature(newFeature)) return [];
16217
+ const before = positions(oldFeature);
16218
+ const after = positions(newFeature);
16219
+ if (before.length !== after.length) return [];
16220
+ const moves = before.flatMap(
16221
+ (from, index) => samePosition(from, after[index], tolerance) ? [] : [{ from, to: after[index] }]
16222
+ );
16223
+ if (moves.length === 0) return [];
16224
+ const changed = [];
16225
+ for (const target of targetFeatures) {
16226
+ if (!isPolygonFeature(target)) continue;
16227
+ let didChange = false;
16228
+ const replace = (position) => {
16229
+ const move = moves.find(
16230
+ ({ from }) => samePosition(position, from, tolerance)
16231
+ );
16232
+ if (!move) return position;
16233
+ didChange = true;
16234
+ return [...move.to];
16235
+ };
16236
+ const geometry = target.geometry.type === "Polygon" ? {
16237
+ ...target.geometry,
16238
+ coordinates: target.geometry.coordinates.map(
16239
+ (ring) => ring.map(replace)
16240
+ )
16241
+ } : {
16242
+ ...target.geometry,
16243
+ coordinates: target.geometry.coordinates.map(
16244
+ (polygon2) => polygon2.map((ring) => ring.map(replace))
16245
+ )
16246
+ };
16247
+ if (didChange) changed.push({ ...target, geometry });
16248
+ }
16249
+ return changed;
16250
+ }
16197
16251
  class CreateFeatureCommand {
16198
16252
  description;
16199
16253
  type = "create";
@@ -16527,7 +16581,11 @@ const ADVANCED_EDIT_MODES = [
16527
16581
  "simplify",
16528
16582
  "lasso"
16529
16583
  ];
16530
- const DEFAULT_HELPER_MODES = ["snapping", "measurements"];
16584
+ const DEFAULT_HELPER_MODES = [
16585
+ "snapping",
16586
+ "topology",
16587
+ "measurements"
16588
+ ];
16531
16589
  const DEFAULT_FILE_MODES = ["open", "save"];
16532
16590
  const DEFAULT_OPTIONS = {
16533
16591
  position: "top-left",
@@ -16540,6 +16598,7 @@ const DEFAULT_OPTIONS = {
16540
16598
  showLabels: false,
16541
16599
  simplifyTolerance: 1e-3,
16542
16600
  snappingEnabled: true,
16601
+ topologyEnabled: false,
16543
16602
  measurementsEnabled: false,
16544
16603
  hideGeomanControl: true,
16545
16604
  saveFilename: "features.geojson",
@@ -18467,6 +18526,10 @@ class GeoEditor {
18467
18526
  pendingOperation = null;
18468
18527
  // Snapping state (independent of other modes)
18469
18528
  snappingEnabled = false;
18529
+ // Topology state (boundary reuse + shared-node editing)
18530
+ topologyEnabled = false;
18531
+ // Prevent geometry updates made by topology handling from being reprocessed.
18532
+ applyingTopology = false;
18470
18533
  // Monotonic token identifying the most recent enableDrawMode() request. Each
18471
18534
  // call bumps it so a tool armed after an async teardown can bail if a newer
18472
18535
  // selection has since superseded it (see enableDrawMode).
@@ -18526,6 +18589,7 @@ class GeoEditor {
18526
18589
  collapsed: this.options.collapsed
18527
18590
  };
18528
18591
  this.snappingEnabled = this.options.snappingEnabled;
18592
+ this.topologyEnabled = this.options.topologyEnabled;
18529
18593
  this.copyFeature = new CopyFeature();
18530
18594
  this.simplifyFeature = new SimplifyFeature({
18531
18595
  tolerance: this.options.simplifyTolerance
@@ -20914,7 +20978,7 @@ class GeoEditor {
20914
20978
  const historyGroup = this.createHistoryToolsGroup();
20915
20979
  toolsWrapper.appendChild(historyGroup);
20916
20980
  }
20917
- if (this.options.helperModes.includes("snapping")) {
20981
+ if (this.options.helperModes.includes("snapping") || this.options.helperModes.includes("topology")) {
20918
20982
  const helperGroup = this.createHelperToolsGroup();
20919
20983
  toolsWrapper.appendChild(helperGroup);
20920
20984
  }
@@ -21044,6 +21108,26 @@ class GeoEditor {
21044
21108
  );
21045
21109
  });
21046
21110
  buttons.appendChild(snappingBtn);
21111
+ if (this.options.helperModes.includes("topology")) {
21112
+ const topologyBtn = document.createElement("button");
21113
+ topologyBtn.className = `${CSS_PREFIX}-tool-button`;
21114
+ topologyBtn.dataset.helper = "topology";
21115
+ topologyBtn.title = "Toggle Topology (shared boundaries and shared-node editing)";
21116
+ topologyBtn.innerHTML = '<svg viewBox="0 0 24 24" width="18" height="18"><path d="M3 3h8v8H3V3zm10 0h8v8h-8V3zM3 13h8v8H3v-8zm10 0h8v8h-8v-8zM9 9h6v6H9V9z" fill="currentColor"/></svg>';
21117
+ topologyBtn.classList.toggle(
21118
+ `${CSS_PREFIX}-tool-button--active`,
21119
+ this.topologyEnabled
21120
+ );
21121
+ topologyBtn.addEventListener("click", (e) => {
21122
+ e.stopPropagation();
21123
+ this.setTopology(!this.topologyEnabled);
21124
+ topologyBtn.classList.toggle(
21125
+ `${CSS_PREFIX}-tool-button--active`,
21126
+ this.topologyEnabled
21127
+ );
21128
+ });
21129
+ buttons.appendChild(topologyBtn);
21130
+ }
21047
21131
  group.appendChild(buttons);
21048
21132
  return group;
21049
21133
  }
@@ -21312,6 +21396,23 @@ class GeoEditor {
21312
21396
  setSnapping(enabled) {
21313
21397
  this.snappingEnabled = enabled;
21314
21398
  this.applySnappingState();
21399
+ this.container?.querySelector('[data-helper="snapping"]')?.classList.toggle(
21400
+ `${CSS_PREFIX}-tool-button--active`,
21401
+ this.snappingEnabled
21402
+ );
21403
+ }
21404
+ isTopologyEnabled() {
21405
+ return this.topologyEnabled;
21406
+ }
21407
+ setTopology(enabled) {
21408
+ this.topologyEnabled = enabled;
21409
+ if (enabled && !this.snappingEnabled) {
21410
+ this.setSnapping(true);
21411
+ }
21412
+ this.container?.querySelector('[data-helper="topology"]')?.classList.toggle(
21413
+ `${CSS_PREFIX}-tool-button--active`,
21414
+ this.topologyEnabled
21415
+ );
21315
21416
  }
21316
21417
  applySnappingState() {
21317
21418
  if (!this.geoman) {
@@ -21532,6 +21633,80 @@ class GeoEditor {
21532
21633
  // ============================================================================
21533
21634
  // Geoman Integration
21534
21635
  // ============================================================================
21636
+ /**
21637
+ * Clip a new polygon to the uncovered area. This turns a roughly overlapping
21638
+ * stand boundary into an exact shared boundary with the polygons already in
21639
+ * the editor.
21640
+ */
21641
+ applyTopologyToCreatedFeature(feature2) {
21642
+ if (!this.topologyEnabled || this.applyingTopology || !isPolygonFeature(feature2)) {
21643
+ return feature2;
21644
+ }
21645
+ const featureId2 = this.getGeomanIdFromFeature(feature2);
21646
+ const existing = this.getFeatures().features.filter((candidate) => {
21647
+ if (candidate === feature2) return false;
21648
+ const candidateId = this.getGeomanIdFromFeature(candidate);
21649
+ if (featureId2 && candidateId === featureId2) return false;
21650
+ return JSON.stringify(candidate.geometry) !== JSON.stringify(feature2.geometry);
21651
+ });
21652
+ const topological = removePolygonOverlaps(feature2, existing);
21653
+ const featureData = this.findGeomanDataForFeature(feature2);
21654
+ this.applyingTopology = true;
21655
+ try {
21656
+ if (!topological) {
21657
+ featureData?.delete();
21658
+ return null;
21659
+ }
21660
+ if (JSON.stringify(topological.geometry) !== JSON.stringify(feature2.geometry)) {
21661
+ if (featureData?.updateGeometry) {
21662
+ featureData.updateGeometry(topological.geometry);
21663
+ } else {
21664
+ featureData?.updateGeoJsonGeometry?.(topological.geometry);
21665
+ }
21666
+ }
21667
+ return topological;
21668
+ } finally {
21669
+ this.applyingTopology = false;
21670
+ }
21671
+ }
21672
+ /**
21673
+ * Move matching vertices in adjacent polygons after a shared node is edited.
21674
+ */
21675
+ applyTopologyToEditedFeature(oldFeature, newFeature) {
21676
+ if (!this.topologyEnabled || this.applyingTopology || !isPolygonFeature(oldFeature) || !isPolygonFeature(newFeature)) {
21677
+ return [];
21678
+ }
21679
+ const editedId = this.getGeomanIdFromFeature(newFeature);
21680
+ const targets = this.getFeatures().features.filter((candidate) => {
21681
+ if (candidate === newFeature) return false;
21682
+ const candidateId = this.getGeomanIdFromFeature(candidate);
21683
+ return !(editedId && candidateId === editedId);
21684
+ });
21685
+ const changed = propagateSharedVertexMoves(
21686
+ oldFeature,
21687
+ newFeature,
21688
+ targets
21689
+ );
21690
+ this.applyingTopology = true;
21691
+ const edits = [];
21692
+ try {
21693
+ for (const updated of changed) {
21694
+ const featureData = this.findGeomanDataForFeature(updated);
21695
+ const original = this.getGeomanFeature(featureData);
21696
+ if (!featureData || !original) continue;
21697
+ if (featureData.updateGeometry) {
21698
+ featureData.updateGeometry(updated.geometry);
21699
+ } else {
21700
+ featureData.updateGeoJsonGeometry?.(updated.geometry);
21701
+ }
21702
+ this.options.onFeatureEdit?.(updated, original);
21703
+ edits.push({ oldFeature: original, newFeature: updated });
21704
+ }
21705
+ } finally {
21706
+ this.applyingTopology = false;
21707
+ }
21708
+ return edits;
21709
+ }
21535
21710
  setupGeomanEvents() {
21536
21711
  if (!this.geoman) return;
21537
21712
  this.geoman.setGlobalEventsListener((event) => {
@@ -21541,24 +21716,45 @@ class GeoEditor {
21541
21716
  );
21542
21717
  const eventAction = event.action ?? "";
21543
21718
  if ((eventName === "gm:create" || event.type === "gm:create") && eventFeature) {
21544
- this.lastCreatedFeature = eventFeature;
21545
- this.options.onFeatureCreate?.(eventFeature);
21546
- this.logSelectedFeatureCollection("created", eventFeature);
21547
- this.recordCreateOperation(eventFeature);
21719
+ const createdFeature = this.applyTopologyToCreatedFeature(eventFeature);
21720
+ if (!createdFeature) return;
21721
+ this.lastCreatedFeature = createdFeature;
21722
+ this.options.onFeatureCreate?.(createdFeature);
21723
+ this.logSelectedFeatureCollection("created", createdFeature);
21724
+ this.recordCreateOperation(createdFeature);
21548
21725
  if (this.options.enableAttributeEditing) {
21549
- this.applyDefaultValues(eventFeature);
21550
- const geomanData = this.findGeomanDataForFeature(eventFeature);
21551
- this.showAttributePanel(eventFeature, geomanData ?? void 0, true);
21726
+ this.applyDefaultValues(createdFeature);
21727
+ const geomanData = this.findGeomanDataForFeature(createdFeature);
21728
+ this.showAttributePanel(
21729
+ createdFeature,
21730
+ geomanData ?? void 0,
21731
+ true
21732
+ );
21552
21733
  }
21553
21734
  }
21554
21735
  if (eventAction === "feature_edit_start" && eventFeature) {
21555
21736
  this.pendingEditFeature = clone$1(eventFeature);
21556
21737
  }
21557
21738
  if (eventAction === "feature_edit_end" && eventFeature) {
21739
+ let topologyEdits = [];
21740
+ if (this.pendingEditFeature) {
21741
+ topologyEdits = this.applyTopologyToEditedFeature(
21742
+ this.pendingEditFeature,
21743
+ eventFeature
21744
+ );
21745
+ }
21558
21746
  this.lastEditedFeature = eventFeature;
21559
21747
  this.logSelectedFeatureCollection("edited", eventFeature);
21560
21748
  if (this.pendingEditFeature) {
21561
- this.recordEditOperation(this.pendingEditFeature, eventFeature);
21749
+ if (topologyEdits.length > 0) {
21750
+ this.recordTopologyEditOperation(
21751
+ this.pendingEditFeature,
21752
+ eventFeature,
21753
+ topologyEdits
21754
+ );
21755
+ } else {
21756
+ this.recordEditOperation(this.pendingEditFeature, eventFeature);
21757
+ }
21562
21758
  this.pendingEditFeature = null;
21563
21759
  }
21564
21760
  }
@@ -21670,6 +21866,30 @@ class GeoEditor {
21670
21866
  const command = new EditFeatureCommand(oldFeature, newFeature, context);
21671
21867
  this.historyManager.record(command);
21672
21868
  }
21869
+ /**
21870
+ * Record the edited polygon and every adjacent polygon changed through a
21871
+ * shared node as one undoable operation.
21872
+ */
21873
+ recordTopologyEditOperation(oldFeature, newFeature, propagatedEdits) {
21874
+ if (!this.historyManager || this.historyManager.isExecutingCommand() || this.isPerformingCompositeOperation) {
21875
+ return;
21876
+ }
21877
+ const context = this.getCommandContext();
21878
+ if (!context) return;
21879
+ const commands = [
21880
+ new EditFeatureCommand(oldFeature, newFeature, context),
21881
+ ...propagatedEdits.map(
21882
+ (edit) => new EditFeatureCommand(
21883
+ edit.oldFeature,
21884
+ edit.newFeature,
21885
+ context
21886
+ )
21887
+ )
21888
+ ];
21889
+ this.historyManager.record(
21890
+ new CompositeCommand(commands, "Edit shared polygon nodes")
21891
+ );
21892
+ }
21673
21893
  /**
21674
21894
  * Record a delete operation in history.
21675
21895
  */
@@ -22095,4 +22315,4 @@ export {
22095
22315
  createPolygon as y,
22096
22316
  createLine as z
22097
22317
  };
22098
- //# sourceMappingURL=geo-editor-F9NDq_Ei.js.map
22318
+ //# sourceMappingURL=geo-editor-CCU2yWKB.js.map