Merge pull request #7537 from walkermatt/removeLastPoint

Fix Draw.removeLastPoint exception when no points to remove
This commit is contained in:
Frédéric Junod
2017-12-06 11:51:14 +01:00
committed by GitHub
2 changed files with 29 additions and 1 deletions
+6 -1
View File
@@ -168,7 +168,12 @@ ol.interaction.Draw = function(options) {
var geometry = opt_geometry; var geometry = opt_geometry;
if (geometry) { if (geometry) {
if (mode === ol.interaction.Draw.Mode_.POLYGON) { if (mode === ol.interaction.Draw.Mode_.POLYGON) {
geometry.setCoordinates([coordinates[0].concat([coordinates[0][0]])]); if (coordinates[0].length) {
// Add a closing coordinate to match the first
geometry.setCoordinates([coordinates[0].concat([coordinates[0][0]])]);
} else {
geometry.setCoordinates([]);
}
} else { } else {
geometry.setCoordinates(coordinates); geometry.setCoordinates(coordinates);
} }
+23
View File
@@ -585,6 +585,29 @@ describe('ol.interaction.Draw', function() {
expect(source.getFeatures()).to.have.length(0); expect(source.getFeatures()).to.have.length(0);
}); });
it('will tolerate removeLastPoint being called when no coordinates', function() {
// first point
simulateEvent('pointermove', 10, 20);
simulateEvent('pointerdown', 10, 20);
simulateEvent('pointerup', 10, 20);
// second point
simulateEvent('pointermove', 40, 30);
simulateEvent('pointerdown', 40, 30);
simulateEvent('pointerup', 40, 30);
simulateEvent('pointermove', 100, 100);
expect(function() {
draw.removeLastPoint();
draw.removeLastPoint();
draw.removeLastPoint();
}).to.not.throwException();
});
it('draws polygon with clicks, finishing on last point', function() { it('draws polygon with clicks, finishing on last point', function() {
// first point // first point
simulateEvent('pointermove', 10, 20); simulateEvent('pointermove', 10, 20);