Merge pull request #6526 from tchandelle/draw-abort

Draw interaction: add abortDrawing method and drawabort event
This commit is contained in:
Frédéric Junod
2020-02-14 09:05:22 +01:00
committed by GitHub
2 changed files with 118 additions and 5 deletions

View File

@@ -143,7 +143,13 @@ const DrawEventType = {
* @event DrawEvent#drawend
* @api
*/
DRAWEND: 'drawend'
DRAWEND: 'drawend',
/**
* Triggered upon feature draw abortion
* @event DrawEvent#drawabort
* @api
*/
DRAWABORT: 'drawabort'
};
@@ -584,8 +590,7 @@ class Draw extends PointerInteraction {
}
pass = false;
} else if (this.freehand_) {
this.finishCoordinate_ = null;
this.abortDrawing_();
this.abortDrawing();
}
if (!pass && this.stopClick_) {
event.stopPropagation();
@@ -834,7 +839,7 @@ class Draw extends PointerInteraction {
}
if (coordinates.length === 0) {
this.finishCoordinate_ = null;
this.abortDrawing();
}
this.updateSketchFeatures_();
@@ -901,6 +906,18 @@ class Draw extends PointerInteraction {
return sketchFeature;
}
/**
* Stop drawing without adding the sketch feature to the target layer.
* @api
*/
abortDrawing() {
const sketchFeature = this.abortDrawing_();
if (sketchFeature) {
this.dispatchEvent(new DrawEvent(DrawEventType.DRAWABORT, sketchFeature));
}
}
/**
* Append coordinates to the end of the geometry that is currently being drawn.
* This can be used when drawing LineStrings or Polygons. Coordinates will
@@ -982,7 +999,7 @@ class Draw extends PointerInteraction {
const map = this.getMap();
const active = this.getActive();
if (!map || !active) {
this.abortDrawing_();
this.abortDrawing();
}
this.overlay_.setMap(active ? map : null);
}

View File

@@ -215,16 +215,20 @@ describe('ol.interaction.Draw', function() {
it('triggers draw events', function() {
const ds = sinon.spy();
const de = sinon.spy();
const da = sinon.spy();
listen(draw, 'drawstart', ds);
listen(draw, 'drawend', de);
listen(draw, 'drawabort', da);
simulateEvent('pointermove', 10, 20);
simulateEvent('pointerdown', 10, 20);
simulateEvent('pointerup', 10, 20);
expect(ds.called).to.be(true);
expect(de.called).to.be(true);
expect(da.called).to.be(false);
simulateEvent('pointermove', 20, 20);
expect(ds.callCount).to.be(1);
expect(de.callCount).to.be(1);
expect(da.callCount).to.be(0);
});
it('triggers drawend event before inserting the feature', function() {
@@ -463,8 +467,10 @@ describe('ol.interaction.Draw', function() {
it('triggers draw events', function() {
const ds = sinon.spy();
const de = sinon.spy();
const da = sinon.spy();
listen(draw, 'drawstart', ds);
listen(draw, 'drawend', de);
listen(draw, 'drawabort', da);
// first point
simulateEvent('pointermove', 10, 20);
@@ -485,6 +491,8 @@ describe('ol.interaction.Draw', function() {
expect(ds.callCount).to.be(1);
expect(de.called).to.be(true);
expect(de.callCount).to.be(1);
expect(da.called).to.be(false);
expect(da.callCount).to.be(0);
});
it('works if finishDrawing is called when the sketch feature is not defined', function() {
@@ -781,8 +789,10 @@ describe('ol.interaction.Draw', function() {
it('triggers draw events', function() {
const ds = sinon.spy();
const de = sinon.spy();
const da = sinon.spy();
listen(draw, 'drawstart', ds);
listen(draw, 'drawend', de);
listen(draw, 'drawabort', da);
// first point
simulateEvent('pointermove', 10, 20);
@@ -808,6 +818,8 @@ describe('ol.interaction.Draw', function() {
expect(ds.callCount).to.be(1);
expect(de.called).to.be(true);
expect(de.callCount).to.be(1);
expect(da.called).to.be(false);
expect(da.callCount).to.be(0);
});
it('works if finishDrawing is called when the sketch feature is not defined', function() {
@@ -1020,8 +1032,10 @@ describe('ol.interaction.Draw', function() {
it('triggers draw events', function() {
const ds = sinon.spy();
const de = sinon.spy();
const da = sinon.spy();
listen(draw, 'drawstart', ds);
listen(draw, 'drawend', de);
listen(draw, 'drawabort', da);
// first point
simulateEvent('pointermove', 10, 20);
@@ -1037,6 +1051,88 @@ describe('ol.interaction.Draw', function() {
expect(ds.callCount).to.be(1);
expect(de.called).to.be(true);
expect(de.callCount).to.be(1);
expect(da.called).to.be(false);
expect(da.callCount).to.be(0);
});
});
describe('#abortDrawing()', function() {
let draw;
beforeEach(function() {
draw = new Draw({
source: source,
type: 'LineString'
});
map.addInteraction(draw);
});
it('aborts the current drawing', function() {
// first point
simulateEvent('pointermove', 10, 20);
simulateEvent('pointerdown', 10, 20);
simulateEvent('pointerup', 10, 20);
// second point
simulateEvent('pointermove', 30, 20);
simulateEvent('pointerdown', 30, 20);
simulateEvent('pointerup', 30, 20);
draw.abortDrawing();
expect(source.getFeatures()).to.have.length(0);
expect(draw.sketchFeature_).to.be(null);
});
it('triggers draw events', function() {
const ds = sinon.spy();
const de = sinon.spy();
const da = sinon.spy();
listen(draw, 'drawstart', ds);
listen(draw, 'drawend', de);
listen(draw, 'drawabort', da);
// first point
simulateEvent('pointermove', 10, 20);
simulateEvent('pointerdown', 10, 20);
simulateEvent('pointerup', 10, 20);
// second point
simulateEvent('pointermove', 30, 20);
simulateEvent('pointerdown', 30, 20);
simulateEvent('pointerup', 30, 20);
draw.abortDrawing();
expect(ds.called).to.be(true);
expect(ds.callCount).to.be(1);
expect(de.called).to.be(false);
expect(de.callCount).to.be(0);
expect(da.called).to.be(true);
expect(da.callCount).to.be(1);
// first point
simulateEvent('pointermove', 10, 20);
simulateEvent('pointerdown', 10, 20);
simulateEvent('pointerup', 10, 20);
// second point
simulateEvent('pointermove', 30, 20);
simulateEvent('pointerdown', 30, 20);
simulateEvent('pointerup', 30, 20);
draw.removeLastPoint();
draw.removeLastPoint();
draw.removeLastPoint();
expect(ds.called).to.be(true);
expect(ds.callCount).to.be(2);
expect(de.called).to.be(false);
expect(de.callCount).to.be(0);
expect(da.called).to.be(true);
expect(da.callCount).to.be(2);
});
it('works if finishDrawing is called when the sketch feature is not defined', function() {