Skip to content

Commit e714545

Browse files
Add pm:intersect event to Draw mode and refactor old intersection code (#1368)
Co-authored-by: Sumit Kumar <[email protected]>
1 parent a615e6a commit e714545

File tree

5 files changed

+44
-22
lines changed

5 files changed

+44
-22
lines changed

cypress/integration/polygon.spec.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,17 +252,22 @@ describe('Draw & Edit Poly', () => {
252252
});
253253

254254
it('prevents self intersections', () => {
255+
let intersectEventCalled = false;
255256
cy.window().then(({ map }) => {
256257
map.pm.enableDraw('Polygon', {
257258
allowSelfIntersection: false,
258259
});
259260

260-
Cypress.$(map).on('pm:create', ({ originalEvent: event }) => {
261+
map.on('pm:create', (event) => {
261262
const poly = event.layer;
262263
poly.pm.enable({
263264
allowSelfIntersection: false,
264265
});
265266
});
267+
268+
map.on('pm:intersect', () => {
269+
intersectEventCalled = true;
270+
});
266271
});
267272

268273
cy.get(mapSelector)
@@ -276,6 +281,10 @@ describe('Draw & Edit Poly', () => {
276281
cy.toolbarButton('edit').click();
277282

278283
cy.hasVertexMarkers(4);
284+
285+
cy.window().then(() => {
286+
expect(intersectEventCalled).to.eql(true);
287+
});
279288
});
280289

281290
it('doesnt allow duplicate points in polygon', () => {

src/css/layers.css

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@
3636
cursor: crosshair;
3737
}
3838

39-
.leaflet-pm-invalid {
40-
stroke: red;
41-
transition: fill ease 0s, stroke ease 0s;
42-
}
43-
4439
.rect-style-marker,
4540
.rect-start-marker {
4641
opacity: 0;

src/js/Draw/L.PM.Draw.Line.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ Draw.Line = Draw.extend({
100100
// TODO: think about moving this somewhere else?
101101
this._otherSnapLayers = [];
102102

103+
// make sure intersection is not set while start drawing
104+
this.isRed = false;
105+
103106
// fire drawstart event
104107
this._fireDrawStart();
105108
this._setGlobalDrawMode();
@@ -216,10 +219,16 @@ Draw.Line = Draw.extend({
216219

217220
// change the style based on self intersection
218221
if (this._doesSelfIntersect) {
219-
this._hintline.setStyle({
220-
color: '#f00000ff',
221-
});
222+
if (!this.isRed) {
223+
this.isRed = true;
224+
this._hintline.setStyle({
225+
color: '#f00000ff',
226+
});
227+
// fire intersect event
228+
this._fireIntersect(selfIntersection, this._map, 'Draw');
229+
}
222230
} else if (!this._hintline.isEmpty()) {
231+
this.isRed = false;
223232
this._hintline.setStyle(this.options.hintlineStyle);
224233
}
225234
},

src/js/Edit/L.PM.Edit.Line.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,6 @@ Edit.Line = Edit.extend({
106106
: this._layer._renderer._container;
107107
L.DomUtil.removeClass(el, 'leaflet-pm-draggable');
108108

109-
// remove invalid class if layer has self intersection
110-
if (!this._map.hasLayer(this._layer) || this.hasSelfIntersection()) {
111-
L.DomUtil.removeClass(el, 'leaflet-pm-invalid');
112-
}
113-
114109
if (this._layerEdited) {
115110
this._fireUpdate();
116111
}
@@ -355,9 +350,9 @@ Edit.Line = Edit.extend({
355350

356351
_handleSelfIntersectionOnVertexRemoval() {
357352
// check for selfintersection again (mainly to reset the style)
358-
this._handleLayerStyle(true);
353+
const selfIntersection = this._handleLayerStyle(true);
359354

360-
if (this.hasSelfIntersection()) {
355+
if (selfIntersection) {
361356
// reset coordinates
362357
this._layer.setLatLngs(this._coordsBeforeEdit);
363358
this._coordsBeforeEdit = null;
@@ -370,7 +365,16 @@ Edit.Line = Edit.extend({
370365
_handleLayerStyle(flash) {
371366
const layer = this._layer;
372367

373-
if (this.hasSelfIntersection()) {
368+
let selfIntersection;
369+
let intersection;
370+
if (this.options.allowSelfIntersection) {
371+
selfIntersection = false;
372+
} else {
373+
intersection = kinks(this._layer.toGeoJSON(15));
374+
selfIntersection = intersection.features.length > 0;
375+
}
376+
377+
if (selfIntersection) {
374378
if (
375379
!this.options.allowSelfIntersection &&
376380
this.options.allowSelfIntersectionEdit
@@ -379,7 +383,7 @@ Edit.Line = Edit.extend({
379383
}
380384

381385
if (this.isRed) {
382-
return;
386+
return selfIntersection;
383387
}
384388

385389
// if it does self-intersect, mark or flash it red
@@ -390,9 +394,8 @@ Edit.Line = Edit.extend({
390394
this.isRed = true;
391395
}
392396

393-
// TODO: call kinks only once (hasSelfIntersection)
394397
// fire intersect event
395-
this._fireIntersect(kinks(this._layer.toGeoJSON(15)));
398+
this._fireIntersect(intersection);
396399
} else {
397400
// if not, reset the style to the default color
398401
layer.setStyle({ color: this.cachedColor });
@@ -404,6 +407,7 @@ Edit.Line = Edit.extend({
404407
this._updateDisabledMarkerStyle(this._markers, false);
405408
}
406409
}
410+
return selfIntersection;
407411
},
408412
_flashLayer() {
409413
if (!this.cachedColor) {

src/js/Mixins/Events.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,14 @@ const EventMixin = {
314314
);
315315
},
316316
// Fired when a Line / Polygon has self intersection
317-
_fireIntersect(intersection, source = 'Edit', customPayload = {}) {
317+
_fireIntersect(
318+
intersection,
319+
fireLayer = this._layer,
320+
source = 'Edit',
321+
customPayload = {}
322+
) {
318323
this.__fire(
319-
this._layer,
324+
fireLayer,
320325
'pm:intersect',
321326
{
322327
layer: this._layer,

0 commit comments

Comments
 (0)