Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fire vertex removed event #1028

Merged
merged 5 commits into from
Dec 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -306,7 +306,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 @@ -339,7 +339,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 @@ -453,7 +453,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 @@ -574,34 +574,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 @@ -610,7 +582,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 @@ -624,7 +596,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 @@ -686,7 +658,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 @@ -714,7 +686,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 @@ -787,7 +759,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 @@ -839,7 +811,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 @@ -140,6 +140,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