Replace Draw Mode enum with typedef

This commit is contained in:
Maximilian Krög
2022-07-17 02:51:00 +02:00
parent 185485b0f7
commit 7004d18519
+31 -38
View File
@@ -117,16 +117,10 @@ import {squaredDistance as squaredCoordinateDistance} from '../coordinate.js';
*/ */
/** /**
* @typedef {'Point' | 'LineString' | 'Polygon' | 'Circle'} Mode
* Draw mode. This collapses multi-part geometry types with their single-part * Draw mode. This collapses multi-part geometry types with their single-part
* cousins. * cousins.
* @enum {string}
*/ */
const Mode = {
POINT: 'Point',
LINE_STRING: 'LineString',
POLYGON: 'Polygon',
CIRCLE: 'Circle',
};
/** /**
* @enum {string} * @enum {string}
@@ -311,7 +305,7 @@ class Draw extends PointerInteraction {
*/ */
this.minPoints_ = options.minPoints this.minPoints_ = options.minPoints
? options.minPoints ? options.minPoints
: this.mode_ === Mode.POLYGON : this.mode_ === 'Polygon'
? 3 ? 3
: 2; : 2;
@@ -322,7 +316,7 @@ class Draw extends PointerInteraction {
* @private * @private
*/ */
this.maxPoints_ = this.maxPoints_ =
this.mode_ === Mode.CIRCLE this.mode_ === 'Circle'
? 2 ? 2
: options.maxPoints : options.maxPoints
? options.maxPoints ? options.maxPoints
@@ -348,7 +342,7 @@ class Draw extends PointerInteraction {
let geometryFunction = options.geometryFunction; let geometryFunction = options.geometryFunction;
if (!geometryFunction) { if (!geometryFunction) {
const mode = this.mode_; const mode = this.mode_;
if (mode === Mode.CIRCLE) { if (mode === 'Circle') {
/** /**
* @param {!LineCoordType} coordinates The coordinates. * @param {!LineCoordType} coordinates The coordinates.
* @param {import("../geom/SimpleGeometry.js").default|undefined} geometry Optional geometry. * @param {import("../geom/SimpleGeometry.js").default|undefined} geometry Optional geometry.
@@ -377,11 +371,11 @@ class Draw extends PointerInteraction {
}; };
} else { } else {
let Constructor; let Constructor;
if (mode === Mode.POINT) { if (mode === 'Point') {
Constructor = Point; Constructor = Point;
} else if (mode === Mode.LINE_STRING) { } else if (mode === 'LineString') {
Constructor = LineString; Constructor = LineString;
} else if (mode === Mode.POLYGON) { } else if (mode === 'Polygon') {
Constructor = Polygon; Constructor = Polygon;
} }
/** /**
@@ -392,7 +386,7 @@ class Draw extends PointerInteraction {
*/ */
geometryFunction = function (coordinates, geometry, projection) { geometryFunction = function (coordinates, geometry, projection) {
if (geometry) { if (geometry) {
if (mode === Mode.POLYGON) { if (mode === 'Polygon') {
if (coordinates[0].length) { if (coordinates[0].length) {
// Add a closing coordinate to match the first // Add a closing coordinate to match the first
geometry.setCoordinates( geometry.setCoordinates(
@@ -554,8 +548,7 @@ class Draw extends PointerInteraction {
// Avoid context menu for long taps when drawing on mobile // Avoid context menu for long taps when drawing on mobile
event.originalEvent.preventDefault(); event.originalEvent.preventDefault();
} }
this.freehand_ = this.freehand_ = this.mode_ !== 'Point' && this.freehandCondition_(event);
this.mode_ !== Mode.POINT && this.freehandCondition_(event);
let move = event.type === MapBrowserEventType.POINTERMOVE; let move = event.type === MapBrowserEventType.POINTERMOVE;
let pass = true; let pass = true;
if ( if (
@@ -673,7 +666,7 @@ class Draw extends PointerInteraction {
this.finishDrawing(); this.finishDrawing();
} else if ( } else if (
!this.freehand_ && !this.freehand_ &&
(!startingToDraw || this.mode_ === Mode.POINT) (!startingToDraw || this.mode_ === 'Point')
) { ) {
if (this.atFinish_(event.pixel)) { if (this.atFinish_(event.pixel)) {
if (this.finishCondition_(event)) { if (this.finishCondition_(event)) {
@@ -739,13 +732,13 @@ class Draw extends PointerInteraction {
let potentiallyDone = false; let potentiallyDone = false;
let potentiallyFinishCoordinates = [this.finishCoordinate_]; let potentiallyFinishCoordinates = [this.finishCoordinate_];
const mode = this.mode_; const mode = this.mode_;
if (mode === Mode.POINT) { if (mode === 'Point') {
at = true; at = true;
} else if (mode === Mode.CIRCLE) { } else if (mode === 'Circle') {
at = this.sketchCoords_.length === 2; at = this.sketchCoords_.length === 2;
} else if (mode === Mode.LINE_STRING) { } else if (mode === 'LineString') {
potentiallyDone = this.sketchCoords_.length > this.minPoints_; potentiallyDone = this.sketchCoords_.length > this.minPoints_;
} else if (mode === Mode.POLYGON) { } else if (mode === 'Polygon') {
const sketchCoords = /** @type {PolyCoordType} */ (this.sketchCoords_); const sketchCoords = /** @type {PolyCoordType} */ (this.sketchCoords_);
potentiallyDone = sketchCoords[0].length > this.minPoints_; potentiallyDone = sketchCoords[0].length > this.minPoints_;
potentiallyFinishCoordinates = [ potentiallyFinishCoordinates = [
@@ -823,9 +816,9 @@ class Draw extends PointerInteraction {
start.push(0); start.push(0);
} }
this.finishCoordinate_ = start; this.finishCoordinate_ = start;
if (this.mode_ === Mode.POINT) { if (this.mode_ === 'Point') {
this.sketchCoords_ = start.slice(); this.sketchCoords_ = start.slice();
} else if (this.mode_ === Mode.POLYGON) { } else if (this.mode_ === 'Polygon') {
this.sketchCoords_ = [[start.slice(), start.slice()]]; this.sketchCoords_ = [[start.slice(), start.slice()]];
this.sketchLineCoords_ = this.sketchCoords_[0]; this.sketchLineCoords_ = this.sketchCoords_[0];
} else { } else {
@@ -864,9 +857,9 @@ class Draw extends PointerInteraction {
while (coordinate.length < stride) { while (coordinate.length < stride) {
coordinate.push(0); coordinate.push(0);
} }
if (this.mode_ === Mode.POINT) { if (this.mode_ === 'Point') {
last = this.sketchCoords_; last = this.sketchCoords_;
} else if (this.mode_ === Mode.POLYGON) { } else if (this.mode_ === 'Polygon') {
coordinates = /** @type {PolyCoordType} */ (this.sketchCoords_)[0]; coordinates = /** @type {PolyCoordType} */ (this.sketchCoords_)[0];
last = coordinates[coordinates.length - 1]; last = coordinates[coordinates.length - 1];
if (this.atFinish_(map.getPixelFromCoordinate(coordinate))) { if (this.atFinish_(map.getPixelFromCoordinate(coordinate))) {
@@ -888,7 +881,7 @@ class Draw extends PointerInteraction {
const sketchPointGeom = this.sketchPoint_.getGeometry(); const sketchPointGeom = this.sketchPoint_.getGeometry();
sketchPointGeom.setCoordinates(coordinate); sketchPointGeom.setCoordinates(coordinate);
} }
if (geometry.getType() === 'Polygon' && this.mode_ !== Mode.POLYGON) { if (geometry.getType() === 'Polygon' && this.mode_ !== 'Polygon') {
this.createOrUpdateCustomSketchLine_(/** @type {Polygon} */ (geometry)); this.createOrUpdateCustomSketchLine_(/** @type {Polygon} */ (geometry));
} else if (this.sketchLineCoords_) { } else if (this.sketchLineCoords_) {
const sketchLineGeom = this.sketchLine_.getGeometry(); const sketchLineGeom = this.sketchLine_.getGeometry();
@@ -908,7 +901,7 @@ class Draw extends PointerInteraction {
let done; let done;
let coordinates; let coordinates;
const mode = this.mode_; const mode = this.mode_;
if (mode === Mode.LINE_STRING || mode === Mode.CIRCLE) { if (mode === 'LineString' || mode === 'Circle') {
this.finishCoordinate_ = coordinate.slice(); this.finishCoordinate_ = coordinate.slice();
coordinates = /** @type {LineCoordType} */ (this.sketchCoords_); coordinates = /** @type {LineCoordType} */ (this.sketchCoords_);
if (coordinates.length >= this.maxPoints_) { if (coordinates.length >= this.maxPoints_) {
@@ -920,7 +913,7 @@ class Draw extends PointerInteraction {
} }
coordinates.push(coordinate.slice()); coordinates.push(coordinate.slice());
this.geometryFunction_(coordinates, geometry, projection); this.geometryFunction_(coordinates, geometry, projection);
} else if (mode === Mode.POLYGON) { } else if (mode === 'Polygon') {
coordinates = /** @type {PolyCoordType} */ (this.sketchCoords_)[0]; coordinates = /** @type {PolyCoordType} */ (this.sketchCoords_)[0];
if (coordinates.length >= this.maxPoints_) { if (coordinates.length >= this.maxPoints_) {
if (this.freehand_) { if (this.freehand_) {
@@ -955,7 +948,7 @@ class Draw extends PointerInteraction {
const projection = this.getMap().getView().getProjection(); const projection = this.getMap().getView().getProjection();
let coordinates; let coordinates;
const mode = this.mode_; const mode = this.mode_;
if (mode === Mode.LINE_STRING || mode === Mode.CIRCLE) { if (mode === 'LineString' || mode === 'Circle') {
coordinates = /** @type {LineCoordType} */ (this.sketchCoords_); coordinates = /** @type {LineCoordType} */ (this.sketchCoords_);
coordinates.splice(-2, 1); coordinates.splice(-2, 1);
if (coordinates.length >= 2) { if (coordinates.length >= 2) {
@@ -968,7 +961,7 @@ class Draw extends PointerInteraction {
if (geometry.getType() === 'Polygon' && this.sketchLine_) { if (geometry.getType() === 'Polygon' && this.sketchLine_) {
this.createOrUpdateCustomSketchLine_(/** @type {Polygon} */ (geometry)); this.createOrUpdateCustomSketchLine_(/** @type {Polygon} */ (geometry));
} }
} else if (mode === Mode.POLYGON) { } else if (mode === 'Polygon') {
coordinates = /** @type {PolyCoordType} */ (this.sketchCoords_)[0]; coordinates = /** @type {PolyCoordType} */ (this.sketchCoords_)[0];
coordinates.splice(-2, 1); coordinates.splice(-2, 1);
const sketchLineGeom = this.sketchLine_.getGeometry(); const sketchLineGeom = this.sketchLine_.getGeometry();
@@ -1002,11 +995,11 @@ class Draw extends PointerInteraction {
let coordinates = this.sketchCoords_; let coordinates = this.sketchCoords_;
const geometry = sketchFeature.getGeometry(); const geometry = sketchFeature.getGeometry();
const projection = this.getMap().getView().getProjection(); const projection = this.getMap().getView().getProjection();
if (this.mode_ === Mode.LINE_STRING) { if (this.mode_ === 'LineString') {
// remove the redundant last point // remove the redundant last point
coordinates.pop(); coordinates.pop();
this.geometryFunction_(coordinates, geometry, projection); this.geometryFunction_(coordinates, geometry, projection);
} else if (this.mode_ === Mode.POLYGON) { } else if (this.mode_ === 'Polygon') {
// remove the redundant last point in ring // remove the redundant last point in ring
/** @type {PolyCoordType} */ (coordinates)[0].pop(); /** @type {PolyCoordType} */ (coordinates)[0].pop();
this.geometryFunction_(coordinates, geometry, projection); this.geometryFunction_(coordinates, geometry, projection);
@@ -1083,9 +1076,9 @@ class Draw extends PointerInteraction {
} }
/** @type {LineCoordType} */ /** @type {LineCoordType} */
let sketchCoords; let sketchCoords;
if (mode === Mode.LINE_STRING || mode === Mode.CIRCLE) { if (mode === 'LineString' || mode === 'Circle') {
sketchCoords = /** @type {LineCoordType} */ (this.sketchCoords_); sketchCoords = /** @type {LineCoordType} */ (this.sketchCoords_);
} else if (mode === Mode.POLYGON) { } else if (mode === 'Polygon') {
sketchCoords = sketchCoords =
this.sketchCoords_ && this.sketchCoords_.length this.sketchCoords_ && this.sketchCoords_.length
? /** @type {PolyCoordType} */ (this.sketchCoords_)[0] ? /** @type {PolyCoordType} */ (this.sketchCoords_)[0]
@@ -1276,15 +1269,15 @@ function getMode(type) {
switch (type) { switch (type) {
case 'Point': case 'Point':
case 'MultiPoint': case 'MultiPoint':
return Mode.POINT; return 'Point';
case 'LineString': case 'LineString':
case 'MultiLineString': case 'MultiLineString':
return Mode.LINE_STRING; return 'LineString';
case 'Polygon': case 'Polygon':
case 'MultiPolygon': case 'MultiPolygon':
return Mode.POLYGON; return 'Polygon';
case 'Circle': case 'Circle':
return Mode.CIRCLE; return 'Circle';
default: default:
throw new Error('Invalid type: ' + type); throw new Error('Invalid type: ' + type);
} }