Skip to content

Commit

Permalink
Merge pull request #15371 from ahocevar/draw-appendcoordinates-maxpoints
Browse files Browse the repository at this point in the history
End drawing when appendCoordinates() hits maxPoints
  • Loading branch information
ahocevar committed Nov 25, 2023
2 parents ab204fe + a1ec887 commit d887da5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
13 changes: 10 additions & 3 deletions changelog/upgrade-notes.md
Expand Up @@ -2,11 +2,18 @@

### 9.0.0

Removed the `ol/style/RegularShape`'s `radius1` property. Use `radius` for regular polygons or `radius` and `radius2` for stars.
#### Changes in `ol/style`

Removed the `shape-radius1` property from `ol/style/flat~FlatShape`. Use `shape-radius` instead.
* Removed the `ol/style/RegularShape`'s `radius1` property. Use `radius` for regular polygons or `radius` and `radius2` for stars.
* Removed the `shape-radius1` property from `ol/style/flat~FlatShape`. Use `shape-radius` instead.

`ol/geom/GeometryCollection` can no longer be created without providing a Geometry array. Emtpy arrays are still valid.
#### `GeometryCollection` constructor

`ol/geom/GeometryCollection` can no longer be created without providing a Geometry array. Emtpy arrays are still valid.

#### `ol/interaction/Draw`

* The `finishDrawing()` method now returns the drawn feature or `null` if no drawing could be finished. Previously it returned `undefined`.

### 8.0.0

Expand Down
10 changes: 7 additions & 3 deletions src/ol/interaction/Draw.js
Expand Up @@ -1562,6 +1562,7 @@ class Draw extends PointerInteraction {
/**
* Add a new coordinate to the drawing.
* @param {!PointCoordType} coordinate Coordinate
* @return {Feature<import("../geom/SimpleGeometry.js").default>} The sketch feature.
* @private
*/
addToDrawing_(coordinate) {
Expand Down Expand Up @@ -1600,8 +1601,9 @@ class Draw extends PointerInteraction {
this.createOrUpdateSketchPoint_(coordinate.slice());
this.updateSketchFeatures_();
if (done) {
this.finishDrawing();
return this.finishDrawing();
}
return this.sketchFeature_;
}

/**
Expand Down Expand Up @@ -1666,12 +1668,13 @@ class Draw extends PointerInteraction {
* Stop drawing and add the sketch feature to the target layer.
* The {@link module:ol/interaction/Draw~DrawEventType.DRAWEND} event is
* dispatched before inserting the feature.
* @return {Feature<import("../geom/SimpleGeometry.js").default>|null} The drawn feature.
* @api
*/
finishDrawing() {
const sketchFeature = this.abortDrawing_();
if (!sketchFeature) {
return;
return null;
}
let coordinates = this.sketchCoords_;
const geometry = sketchFeature.getGeometry();
Expand Down Expand Up @@ -1712,6 +1715,7 @@ class Draw extends PointerInteraction {
if (this.source_) {
this.source_.addFeature(sketchFeature);
}
return sketchFeature;
}

/**
Expand Down Expand Up @@ -1783,7 +1787,7 @@ class Draw extends PointerInteraction {

const ending = coordinates[coordinates.length - 1];
// Duplicate last coordinate for sketch drawing (cursor position)
this.addToDrawing_(ending);
this.sketchFeature_ = this.addToDrawing_(ending);
this.modifyDrawing_(ending);
}

Expand Down
28 changes: 28 additions & 0 deletions test/browser/spec/ol/interaction/Draw.test.js
Expand Up @@ -1935,6 +1935,34 @@ describe('ol/interaction/Draw', function () {
});
});

describe('append coordinates when drawing a LineString feature with maxPoints', () => {
let draw;
beforeEach(() => {
draw = new Draw({
source: source,
type: 'LineString',
maxPoints: 2,
});
map.addInteraction(draw);
});
it('finishes drawing when starting empty and appending maxPoints coordinates', (done) => {
draw.once('drawend', () => {
try {
setTimeout(() => {
expect(source.getFeatures()).to.have.length(1);
done();
}, 0);
} catch (e) {
done(e);
}
});
draw.appendCoordinates([
[0, 0],
[1, 1],
]);
});
});

describe('append coordinates when drawing a Polygon feature', function () {
let draw;
let coordinates;
Expand Down

0 comments on commit d887da5

Please sign in to comment.