Skip to content

Commit

Permalink
Fix fire vertex removed event (geoman-io#1028)
Browse files Browse the repository at this point in the history
* Fix fire vertex removed event

* Add test

* Move findDeepMarkerIndex into Utils and add it to the removeVertrex evt

* Fix this.L.PM

Co-authored-by: Falke Design <design.falke@gmail.com>
  • Loading branch information
2 people authored and BaharWeb committed May 13, 2022
1 parent 849f519 commit 3c12dbe
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 38 deletions.
12 changes: 12 additions & 0 deletions cypress/integration/line.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ describe('Draw & Edit Line', () => {
});

it('removes last vertex', () => {
let eventCalled = false;
cy.window().then(({ map }) => {
map.on('pm:drawstart', (e) => {
e.workingLayer.on('pm:vertexremoved', () => {
eventCalled = true;
});
});
});

cy.toolbarButton('polyline').click();

cy.get(mapSelector)
Expand All @@ -31,6 +40,9 @@ describe('Draw & Edit Line', () => {
cy.get('.button-container.active .action-removeLastVertex').click();

cy.hasVertexMarkers(3);
cy.window().then(() => {
expect(eventCalled).to.eq(true);
});
});

it('respects custom style', () => {
Expand Down
11 changes: 10 additions & 1 deletion src/js/Draw/L.PM.Draw.Line.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,15 +282,24 @@ Draw.Line = Draw.extend({
.filter((l) => !L.DomUtil.hasClass(l._icon, 'cursor-marker'))
.find((l) => l.getLatLng() === removedCoord);


const markers = this._layerGroup.getLayers().filter((l)=>l instanceof L.Marker);
// the index path to the marker inside the multidimensional marker array
const { indexPath } = L.PM.Utils.findDeepMarkerIndex(
markers,
marker
);

// remove that marker
this._layerGroup.removeLayer(marker);

// update layer with new coords
this._layer.setLatLngs(coords);

// sync the hintline again
this._syncHintLine();
this._setTooltipText();

this._fireVertexRemoved(marker, indexPath, 'Draw');
},
_finishShape() {
// if self intersection is not allowed, do not finish the shape!
Expand Down
46 changes: 9 additions & 37 deletions src/js/Edit/L.PM.Edit.Line.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ Edit.Line = Edit.extend({
delete newM.rightM;

// the index path to the marker inside the multidimensional marker array
const { indexPath, index, parentPath } = this.findDeepMarkerIndex(
const { indexPath, index, parentPath } = L.PM.Utils.findDeepMarkerIndex(
this._markers,
leftM
);
Expand Down Expand Up @@ -337,7 +337,7 @@ Edit.Line = Edit.extend({

this._fireVertexAdded(
newM,
this.findDeepMarkerIndex(this._markers, newM).indexPath,
L.PM.Utils.findDeepMarkerIndex(this._markers, newM).indexPath,
latlng
);

Expand Down Expand Up @@ -449,7 +449,7 @@ Edit.Line = Edit.extend({
let coords = this._layer.getLatLngs();

// the index path to the marker inside the multidimensional marker array
const { indexPath, index, parentPath } = this.findDeepMarkerIndex(
const { indexPath, index, parentPath } = L.PM.Utils.findDeepMarkerIndex(
this._markers,
marker
);
Expand Down Expand Up @@ -570,34 +570,6 @@ Edit.Line = Edit.extend({
// TODO: maybe fire latlng as well?
this._fireVertexRemoved(marker, indexPath);
},
findDeepMarkerIndex(arr, marker) {
// thanks for the function, Felix Heck
let result;

const run = (path) => (v, i) => {
const iRes = path.concat(i);

if (v._leaflet_id === marker._leaflet_id) {
result = iRes;
return true;
}

return Array.isArray(v) && v.some(run(iRes));
};
arr.some(run([]));

let returnVal = {};

if (result) {
returnVal = {
indexPath: result,
index: result[result.length - 1],
parentPath: result.slice(0, result.length - 1),
};
}

return returnVal;
},
updatePolygonCoordsFromMarkerDrag(marker) {
// update polygon coords
const coords = this._layer.getLatLngs();
Expand All @@ -606,7 +578,7 @@ Edit.Line = Edit.extend({
const latlng = marker.getLatLng();

// get indexPath of Marker
const { indexPath, index, parentPath } = this.findDeepMarkerIndex(
const { indexPath, index, parentPath } = L.PM.Utils.findDeepMarkerIndex(
this._markers,
marker
);
Expand All @@ -620,7 +592,7 @@ Edit.Line = Edit.extend({
},

_getNeighborMarkers(marker) {
const { indexPath, index, parentPath } = this.findDeepMarkerIndex(
const { indexPath, index, parentPath } = L.PM.Utils.findDeepMarkerIndex(
this._markers,
marker
);
Expand Down Expand Up @@ -682,7 +654,7 @@ Edit.Line = Edit.extend({
return;
}

const { indexPath } = this.findDeepMarkerIndex(this._markers, marker);
const { indexPath } = L.PM.Utils.findDeepMarkerIndex(this._markers, marker);

this._fireMarkerDragStart(e, indexPath);

Expand Down Expand Up @@ -710,7 +682,7 @@ Edit.Line = Edit.extend({
return;
}

const { indexPath, index, parentPath } = this.findDeepMarkerIndex(
const { indexPath, index, parentPath } = L.PM.Utils.findDeepMarkerIndex(
this._markers,
marker
);
Expand Down Expand Up @@ -783,7 +755,7 @@ Edit.Line = Edit.extend({
return;
}

const { indexPath } = this.findDeepMarkerIndex(this._markers, marker);
const { indexPath } = L.PM.Utils.findDeepMarkerIndex(this._markers, marker);

// if self intersection is not allowed but this edit caused a self intersection,
// reset and cancel; do not fire events
Expand Down Expand Up @@ -835,7 +807,7 @@ Edit.Line = Edit.extend({
return;
}

const { indexPath } = this.findDeepMarkerIndex(this._markers, vertex);
const { indexPath } = L.PM.Utils.findDeepMarkerIndex(this._markers, vertex);

this._fireVertexClick(e, indexPath);
},
Expand Down
28 changes: 28 additions & 0 deletions src/js/L.PM.Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,34 @@ const Utils = {

return returnVal;
},
findDeepMarkerIndex(arr, marker) {
// thanks for the function, Felix Heck
let result;

const run = (path) => (v, i) => {
const iRes = path.concat(i);

if (v._leaflet_id === marker._leaflet_id) {
result = iRes;
return true;
}

return Array.isArray(v) && v.some(run(iRes));
};
arr.some(run([]));

let returnVal = {};

if (result) {
returnVal = {
indexPath: result,
index: result[result.length - 1],
parentPath: result.slice(0, result.length - 1),
};
}

return returnVal;
},
_getIndexFromSegment(coords, segment) {
if (segment && segment.length === 2) {
const indexA = this.findDeepCoordIndex(coords, segment[0]);
Expand Down

0 comments on commit 3c12dbe

Please sign in to comment.