maplibre-gl-geo-editor 0.8.0 → 0.9.1

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/README.md CHANGED
@@ -11,13 +11,17 @@ A powerful MapLibre GL plugin for creating and editing geometries. Extends the f
11
11
  ## Features
12
12
 
13
13
  ### Draw Tools
14
- - **Polygon** - Draw polygons by clicking points
15
- - **Line** - Draw polylines
14
+ - **Polygon** - Draw polygons by clicking points; **double-click or right-click to finish**
15
+ - **Line** - Draw polylines; **double-click or right-click to finish**
16
16
  - **Rectangle** - Draw rectangles
17
17
  - **Circle** - Draw circles
18
18
  - **Marker** - Place point markers
19
19
  - **Freehand** - Draw shapes by dragging (custom implementation)
20
20
 
21
+ > Polygons and lines can also be completed the Geoman way — by clicking the
22
+ > first/last vertex — but a double-click or right-click ends the shape too, as
23
+ > in most drawing tools.
24
+
21
25
  ### Basic Edit Tools (via Geoman Free)
22
26
  - **Drag** - Move features on the map
23
27
  - **Edit** - Modify feature vertices
@@ -16686,18 +16686,25 @@ function radiansToDegrees(radians2) {
16686
16686
  return radians2 * 180 / Math.PI;
16687
16687
  }
16688
16688
  function roundCoordinates(coordinates, precision2 = 6) {
16689
- return [
16690
- Number(coordinates[0].toFixed(precision2)),
16691
- Number(coordinates[1].toFixed(precision2))
16692
- ];
16689
+ return coordinates.map(
16690
+ (value) => Number(value.toFixed(precision2))
16691
+ );
16693
16692
  }
16694
16693
  function positionsEqual(pos1, pos2, tolerance = 1e-9) {
16695
- return Math.abs(pos1[0] - pos2[0]) < tolerance && Math.abs(pos1[1] - pos2[1]) < tolerance;
16694
+ if (pos1.length !== pos2.length) {
16695
+ return false;
16696
+ }
16697
+ return pos1.every(
16698
+ (value, index) => Math.abs(value - pos2[index]) < tolerance
16699
+ );
16696
16700
  }
16697
16701
  function getVertexCount(feature2) {
16698
16702
  return coordAll(feature2).length;
16699
16703
  }
16700
16704
  function cloneFeature(feature2) {
16705
+ if (typeof structuredClone === "function") {
16706
+ return structuredClone(feature2);
16707
+ }
16701
16708
  return JSON.parse(JSON.stringify(feature2));
16702
16709
  }
16703
16710
  class CopyFeature {
@@ -18387,6 +18394,9 @@ class GeoEditor {
18387
18394
  // Event listeners
18388
18395
  boundKeyHandler = null;
18389
18396
  boundClickHandler = null;
18397
+ // Finish an in-progress polygon/line draw on double-click or right-click.
18398
+ boundDrawFinishDblClick = null;
18399
+ boundDrawFinishContextMenu = null;
18390
18400
  boundScaleMouseDown = null;
18391
18401
  boundScaleMouseMove = null;
18392
18402
  boundScaleMouseUp = null;
@@ -18486,6 +18496,7 @@ class GeoEditor {
18486
18496
  this.setupSelectionHandler();
18487
18497
  this.setupScaleHandler();
18488
18498
  this.setupMultiDragHandler();
18499
+ this.setupDrawFinishHandlers();
18489
18500
  this.setupVertexMarkerStyleListener();
18490
18501
  this.setupGeomanEvents();
18491
18502
  if (this.options.enableAttributeEditing) {
@@ -18504,6 +18515,7 @@ class GeoEditor {
18504
18515
  this.removeSelectionHandler();
18505
18516
  this.removeScaleHandler();
18506
18517
  this.removeMultiDragHandler();
18518
+ this.removeDrawFinishHandlers();
18507
18519
  this.removeVertexMarkerStyleListener();
18508
18520
  this.disableAllModes();
18509
18521
  this.hideFeaturePropertiesPopup();
@@ -19229,8 +19241,9 @@ class GeoEditor {
19229
19241
  * Disable all modes
19230
19242
  */
19231
19243
  disableAllModes() {
19244
+ let geomanDisable;
19232
19245
  if (this.geoman) {
19233
- this.geoman.disableAllModes();
19246
+ geomanDisable = this.geoman.disableAllModes();
19234
19247
  }
19235
19248
  this.scaleFeature.cancelScale();
19236
19249
  this.lassoFeature.disable();
@@ -19254,7 +19267,123 @@ class GeoEditor {
19254
19267
  this.state.isDrawing = false;
19255
19268
  this.state.isEditing = false;
19256
19269
  this.updateToolbarState();
19257
- this.applySnappingState();
19270
+ if (geomanDisable && typeof geomanDisable.then === "function") {
19271
+ geomanDisable.then(() => this.applySnappingState()).catch(() => this.applySnappingState());
19272
+ } else {
19273
+ this.applySnappingState();
19274
+ }
19275
+ }
19276
+ // ============================================================================
19277
+ // Finish polygon/line draw on double-click or right-click
19278
+ // ============================================================================
19279
+ /**
19280
+ * Attach map listeners that finish an in-progress polygon or line on a
19281
+ * double-click or right-click.
19282
+ *
19283
+ * Geoman free only completes these shapes when the user clicks the first or
19284
+ * last vertex, which is unintuitive: every other drawing tool ends a polygon
19285
+ * with a double-click (or right-click). These handlers add that gesture while
19286
+ * leaving the click-the-first-vertex behavior intact.
19287
+ */
19288
+ setupDrawFinishHandlers() {
19289
+ if (!this.map) return;
19290
+ const handler = (e) => {
19291
+ if (this.state.activeDrawMode !== "polygon" && this.state.activeDrawMode !== "line") {
19292
+ return;
19293
+ }
19294
+ if (this.finishActiveLineOrPolygonDraw()) {
19295
+ e.preventDefault();
19296
+ }
19297
+ };
19298
+ this.boundDrawFinishDblClick = handler;
19299
+ this.boundDrawFinishContextMenu = handler;
19300
+ this.map.on("dblclick", this.boundDrawFinishDblClick);
19301
+ this.map.on("contextmenu", this.boundDrawFinishContextMenu);
19302
+ }
19303
+ /**
19304
+ * Remove the double-click / right-click draw-finish listeners.
19305
+ */
19306
+ removeDrawFinishHandlers() {
19307
+ if (!this.map) return;
19308
+ if (this.boundDrawFinishDblClick) {
19309
+ this.map.off("dblclick", this.boundDrawFinishDblClick);
19310
+ this.boundDrawFinishDblClick = null;
19311
+ }
19312
+ if (this.boundDrawFinishContextMenu) {
19313
+ this.map.off("contextmenu", this.boundDrawFinishContextMenu);
19314
+ this.boundDrawFinishContextMenu = null;
19315
+ }
19316
+ }
19317
+ /**
19318
+ * Finish the polygon or line currently being drawn, as if the user had
19319
+ * clicked the closing vertex.
19320
+ *
19321
+ * This drives Geoman's own draw instance so the resulting geometry, cleanup,
19322
+ * and `gm:create` event match a normally-finished shape. Geoman's draw
19323
+ * internals are not part of the public API, so every access is feature-detected
19324
+ * and the method bails (leaving drawing untouched) if the shape cannot be
19325
+ * finished — e.g. too few vertices, or an incompatible Geoman build.
19326
+ *
19327
+ * @returns true when a shape was finished, false otherwise.
19328
+ */
19329
+ finishActiveLineOrPolygonDraw() {
19330
+ const mode = this.state.activeDrawMode;
19331
+ if (mode !== "polygon" && mode !== "line") return false;
19332
+ if (!this.geoman) return false;
19333
+ try {
19334
+ const instances = this.geoman.actionInstances;
19335
+ const instance = instances?.[`draw__${mode}`];
19336
+ const drawer = instance?.lineDrawer;
19337
+ if (!drawer || typeof drawer.getMarkerClickEventData !== "function") {
19338
+ return false;
19339
+ }
19340
+ const coords = Array.isArray(drawer.shapeLngLats) ? drawer.shapeLngLats : [];
19341
+ const count = coords.length;
19342
+ if (count === 0) return false;
19343
+ const hasTrailingDuplicate = count >= 2 && this.lngLatsEqual(coords[count - 1], coords[count - 2]);
19344
+ if (mode === "line") {
19345
+ const endIndex = hasTrailingDuplicate ? count - 2 : count - 1;
19346
+ if (endIndex < 1) return false;
19347
+ const eventData2 = drawer.getMarkerClickEventData(endIndex);
19348
+ if (typeof instance.lineFinished !== "function") return false;
19349
+ instance.lineFinished(eventData2);
19350
+ return true;
19351
+ }
19352
+ const eventData = drawer.getMarkerClickEventData(count - 1);
19353
+ const ring = eventData?.geoJson?.geometry?.coordinates;
19354
+ if (hasTrailingDuplicate && Array.isArray(ring) && ring.length >= 2) {
19355
+ ring.pop();
19356
+ }
19357
+ const ringLength = Array.isArray(ring) ? ring.length : count;
19358
+ if (ringLength < 3) return false;
19359
+ if (typeof instance.polygonFinished !== "function") return false;
19360
+ instance.polygonFinished(eventData);
19361
+ return true;
19362
+ } catch {
19363
+ return false;
19364
+ }
19365
+ }
19366
+ /**
19367
+ * Compare two Geoman vertices for equality. Geoman stores vertices as either
19368
+ * `[lng, lat]` tuples or MapLibre `LngLat` objects, so normalize both forms.
19369
+ */
19370
+ lngLatsEqual(a, b) {
19371
+ const toPair = (p) => {
19372
+ if (Array.isArray(p) && typeof p[0] === "number" && typeof p[1] === "number") {
19373
+ return [p[0], p[1]];
19374
+ }
19375
+ if (p && typeof p === "object") {
19376
+ const o = p;
19377
+ if (typeof o.lng === "number" && typeof o.lat === "number") {
19378
+ return [o.lng, o.lat];
19379
+ }
19380
+ }
19381
+ return null;
19382
+ };
19383
+ const pa = toPair(a);
19384
+ const pb = toPair(b);
19385
+ if (!pa || !pb) return false;
19386
+ return Math.abs(pa[0] - pb[0]) < 1e-9 && Math.abs(pa[1] - pb[1]) < 1e-9;
19258
19387
  }
19259
19388
  /**
19260
19389
  * Enable an advanced edit mode
@@ -21614,4 +21743,4 @@ exports.safeUnion = safeUnion;
21614
21743
  exports.selectFeaturesWithinLasso = selectFeaturesWithinLasso;
21615
21744
  exports.toSelectedFeatures = toSelectedFeatures;
21616
21745
  exports.toggleInSelection = toggleInSelection;
21617
- //# sourceMappingURL=geo-editor-DDTgH2JC.cjs.map
21746
+ //# sourceMappingURL=geo-editor-D7Ae1QRE.cjs.map