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

Add pm:intersect event to Draw mode and refactor old intersection code #1368

Merged
merged 2 commits into from
Nov 21, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion cypress/integration/polygon.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,17 +252,22 @@ describe('Draw & Edit Poly', () => {
});

it('prevents self intersections', () => {
let intersectEventCalled = false;
cy.window().then(({ map }) => {
map.pm.enableDraw('Polygon', {
allowSelfIntersection: false,
});

Cypress.$(map).on('pm:create', ({ originalEvent: event }) => {
map.on('pm:create', (event) => {
const poly = event.layer;
poly.pm.enable({
allowSelfIntersection: false,
});
});

map.on('pm:intersect', () => {
intersectEventCalled = true;
});
});

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

cy.hasVertexMarkers(4);

cy.window().then(() => {
expect(intersectEventCalled).to.eql(true);
});
});

it('doesnt allow duplicate points in polygon', () => {
Expand Down
5 changes: 0 additions & 5 deletions src/css/layers.css
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@
cursor: crosshair;
}

.leaflet-pm-invalid {
stroke: red;
transition: fill ease 0s, stroke ease 0s;
}

.rect-style-marker,
.rect-start-marker {
opacity: 0;
Expand Down
15 changes: 12 additions & 3 deletions src/js/Draw/L.PM.Draw.Line.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ Draw.Line = Draw.extend({
// TODO: think about moving this somewhere else?
this._otherSnapLayers = [];

// make sure intersection is not set while start drawing
this.isRed = false;

// fire drawstart event
this._fireDrawStart();
this._setGlobalDrawMode();
Expand Down Expand Up @@ -216,10 +219,16 @@ Draw.Line = Draw.extend({

// change the style based on self intersection
if (this._doesSelfIntersect) {
this._hintline.setStyle({
color: '#f00000ff',
});
if (!this.isRed) {
this.isRed = true;
this._hintline.setStyle({
color: '#f00000ff',
});
// fire intersect event
this._fireIntersect(selfIntersection, this._map, 'Draw');
}
} else if (!this._hintline.isEmpty()) {
this.isRed = false;
this._hintline.setStyle(this.options.hintlineStyle);
}
},
Expand Down
26 changes: 15 additions & 11 deletions src/js/Edit/L.PM.Edit.Line.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,6 @@ Edit.Line = Edit.extend({
: this._layer._renderer._container;
L.DomUtil.removeClass(el, 'leaflet-pm-draggable');

// remove invalid class if layer has self intersection
if (!this._map.hasLayer(this._layer) || this.hasSelfIntersection()) {
L.DomUtil.removeClass(el, 'leaflet-pm-invalid');
}

if (this._layerEdited) {
this._fireUpdate();
}
Expand Down Expand Up @@ -355,9 +350,9 @@ Edit.Line = Edit.extend({

_handleSelfIntersectionOnVertexRemoval() {
// check for selfintersection again (mainly to reset the style)
this._handleLayerStyle(true);
const selfIntersection = this._handleLayerStyle(true);

if (this.hasSelfIntersection()) {
if (selfIntersection) {
// reset coordinates
this._layer.setLatLngs(this._coordsBeforeEdit);
this._coordsBeforeEdit = null;
Expand All @@ -370,7 +365,16 @@ Edit.Line = Edit.extend({
_handleLayerStyle(flash) {
const layer = this._layer;

if (this.hasSelfIntersection()) {
let selfIntersection;
let intersection;
if (this.options.allowSelfIntersection) {
selfIntersection = false;
} else {
intersection = kinks(this._layer.toGeoJSON(15));
selfIntersection = intersection.features.length > 0;
}

if (selfIntersection) {
if (
!this.options.allowSelfIntersection &&
this.options.allowSelfIntersectionEdit
Expand All @@ -379,7 +383,7 @@ Edit.Line = Edit.extend({
}

if (this.isRed) {
return;
return selfIntersection;
}

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

// TODO: call kinks only once (hasSelfIntersection)
// fire intersect event
this._fireIntersect(kinks(this._layer.toGeoJSON(15)));
this._fireIntersect(intersection);
} else {
// if not, reset the style to the default color
layer.setStyle({ color: this.cachedColor });
Expand All @@ -404,6 +407,7 @@ Edit.Line = Edit.extend({
this._updateDisabledMarkerStyle(this._markers, false);
}
}
return selfIntersection;
},
_flashLayer() {
if (!this.cachedColor) {
Expand Down
9 changes: 7 additions & 2 deletions src/js/Mixins/Events.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,14 @@ const EventMixin = {
);
},
// Fired when a Line / Polygon has self intersection
_fireIntersect(intersection, source = 'Edit', customPayload = {}) {
_fireIntersect(
intersection,
fireLayer = this._layer,
source = 'Edit',
customPayload = {}
) {
this.__fire(
this._layer,
fireLayer,
'pm:intersect',
{
layer: this._layer,
Expand Down