Skip to content

Commit

Permalink
fix: prevent feature.edit to be called twice on polygon/line end edit
Browse files Browse the repository at this point in the history
When finishing drawing a polygon/line, Leaflet.Editable will send
both `editable:drawing:end` and `editable:drawing:commit`. In normal
flow, we only need to listen to the later, and we only need to
listen for the former in the case of pressing escape while drawing.
  • Loading branch information
yohanboniface committed Oct 24, 2024
1 parent 71b55e7 commit 1da0962
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
28 changes: 16 additions & 12 deletions umap/static/umap/js/umap.controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ const ControlsMixin = {
const connectedPeersCount = L.DomUtil.createButton(
'leaflet-control-connected-peers',
rightContainer,
'',
''
)
L.DomEvent.on(connectedPeersCount, 'mouseover', () => {
this.tooltip.open({
Expand Down Expand Up @@ -1182,17 +1182,6 @@ U.Editable = L.Editable.extend({
initialize: function (map, options) {
L.Editable.prototype.initialize.call(this, map, options)
this.on('editable:drawing:click editable:drawing:move', this.drawingTooltip)
this.on('editable:drawing:end', (event) => {
this.map.tooltip.close()
// Leaflet.Editable will delete the drawn shape if invalid
// (eg. line has only one drawn point)
// So let's check if the layer has no more shape
if (!event.layer.feature.hasGeom()) {
event.layer.feature.del()
} else {
event.layer.feature.edit()
}
})
// Layer for items added by users
this.on('editable:drawing:cancel', (event) => {
if (event.layer instanceof U.LeafletMarker) event.layer.feature.del()
Expand Down Expand Up @@ -1322,4 +1311,19 @@ U.Editable = L.Editable.extend({
L.DomEvent.stop(e)
e.cancel()
},

onEscape: () => {
this.once('editable:drawing:end', (event) => {
this.map.tooltip.close()
// Leaflet.Editable will delete the drawn shape if invalid
// (eg. line has only one drawn point)
// So let's check if the layer has no more shape
if (!event.layer.feature.hasGeom()) {
event.layer.feature.del()
} else {
event.layer.feature.edit()
}
})
this.stopDrawing()
},
})
2 changes: 1 addition & 1 deletion umap/static/umap/js/umap.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ U.Map = L.Map.extend({
if (this.importer.dialog.visible) {
this.importer.dialog.close()
} else if (this.editEnabled && this.editTools.drawing()) {
this.editTools.stopDrawing()
this.editTools.onEscape()
} else if (this.measureTools.enabled()) {
this.measureTools.stopDrawing()
} else if (this.fullPanel?.isOpen()) {
Expand Down

0 comments on commit 1da0962

Please sign in to comment.