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

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