Skip to content

Commit

Permalink
Single-Coord Line/Polys can't be finished during draw (#349) (patch)
Browse files Browse the repository at this point in the history
* added failing test for lines

* added test for polygons

* finish shape is prevented when coord length is 1 or lower
  • Loading branch information
codeofsumit authored Oct 25, 2018
1 parent 02431a5 commit ad70532
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
12 changes: 12 additions & 0 deletions cypress/integration/line.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ describe('Draw & Edit Line', () => {

const mapSelector = '#map';

it('doesnt finish single point lines', () => {
cy.toolbarButton('polyline').click();

cy.get(mapSelector)
.click(90, 250)
.click(90, 250);

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

cy.hasVertexMarkers(0);
});

it('draws and edits a line', () => {
cy.window().then(({ map }) => {
cy.hasLayers(map, 1);
Expand Down
12 changes: 12 additions & 0 deletions cypress/integration/polygon.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
describe('Draw & Edit Poly', () => {
const mapSelector = '#map';

it('doesnt finish single point polys', () => {
cy.toolbarButton('polygon').click();

cy.get(mapSelector)
.click(90, 250)
.click(90, 250);

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

cy.hasVertexMarkers(0);
});

it('adds new vertex to end of array', () => {
// when adding a vertex between the first and last current vertex,
// the new coord should be added to the end, not the beginning of the coord array
Expand Down
9 changes: 8 additions & 1 deletion src/js/Draw/L.PM.Draw.Line.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,15 @@ Draw.Line = Draw.extend({
return;
}

// get coordinates, create the leaflet shape and add it to the map
// get coordinates
const coords = this._layer.getLatLngs();

// if there is only one coords, don't finish the shape!
if (coords.length <= 1) {
return;
}

// create the leaflet shape and add it to the map
const polylineLayer = L.polyline(
coords,
this.options.pathOptions,
Expand Down
11 changes: 9 additions & 2 deletions src/js/Draw/L.PM.Draw.Poly.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@ Draw.Poly = Draw.Line.extend({
return;
}

// get coordinates, create the leaflet shape and add it to the map
// get coordinates
const coords = this._layer.getLatLngs();

// if there is only one coords, don't finish the shape!
if (coords.length <= 1) {
return;
}

// create the leaflet shape and add it to the map
if (event && event.type === 'dblclick') {
// Leaflet creates an extra node with double click
coords.splice(coords.length - 1, 1);
}
const polygonLayer = L.polygon(coords, this.options.pathOptions).addTo(this._map);
const polygonLayer = L.polygon(coords, this.options.pathOptions).addTo(this._map,);

// disable drawing
this.disable();
Expand Down

0 comments on commit ad70532

Please sign in to comment.