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

End drawing when appendCoordinates() hits maxPoints #15371

Merged
merged 2 commits into from Nov 25, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hardly seems worth it, but we could add a note to the upgrade notes saying that finishDrawing now returns a 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