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

Better Typing for TerraDrawModes & Setting coordinatePrecision of modes inside of a TerraDraw to the respective Adapter #137

Merged
merged 5 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/adapters/common/base.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ export abstract class TerraDrawBaseAdapter {
});
}

/**
* Gets the coordinate precision.
* @returns {number} The coordinate precision.
* @description The coordinate precision is the number of decimal places. Note that the precision will be overriden by the precision of the TerraDraw Adapter.
*/
public getCoordinatePrecision() {
return this._coordinatePrecision;
}

private getAdapterListeners() {
return [
new AdapterListener<BasePointerListener>({
Expand Down
2 changes: 2 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export interface TerraDrawModeRegisterConfig {
onFinish: (finishedId: string) => void;
project: Project;
unproject: Unproject;
coordinatePrecision: number;
}

export type TerraDrawModeState =
Expand Down Expand Up @@ -136,6 +137,7 @@ export interface TerraDrawAdapter {
unregister(): void;
render(changes: TerraDrawChanges, styling: TerraDrawStylingFunction): void;
clear(): void;
getCoordinatePrecision(): number;
}

export const SELECT_PROPERTIES = {
Expand Down
29 changes: 19 additions & 10 deletions src/modes/base.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from "../store/store";
import { isValidStoreFeature } from "../store/store-feature-validation";

type CustomStyling = Record<
export type CustomStyling = Record<
string,
| string
| number
Expand All @@ -30,6 +30,12 @@ export enum ModeTypes {
Static = "static",
Render = "render",
}

export type BaseModeOptions<T extends CustomStyling> = {
styles?: Partial<T>;
pointerDistance?: number;
};

export abstract class TerraDrawBaseDrawMode<T extends CustomStyling> {
protected _state: TerraDrawModeState;
get state() {
Expand All @@ -54,7 +60,7 @@ export abstract class TerraDrawBaseDrawMode<T extends CustomStyling> {

protected behaviors: TerraDrawModeBehavior[] = [];
protected pointerDistance: number;
protected coordinatePrecision: number;
protected coordinatePrecision!: number;
protected onStyleChange!: StoreChangeHandler;
protected store!: GeoJSONStore;
protected setDoubleClickToZoom!: TerraDrawModeRegisterConfig["setDoubleClickToZoom"];
Expand All @@ -63,18 +69,12 @@ export abstract class TerraDrawBaseDrawMode<T extends CustomStyling> {
protected setCursor!: TerraDrawModeRegisterConfig["setCursor"];
protected registerBehaviors(behaviorConfig: BehaviorConfig): void {}

constructor(options?: {
styles?: Partial<T>;
pointerDistance?: number;
coordinatePrecision?: number;
}) {
constructor(options?: BaseModeOptions<T>) {
this._state = "unregistered";
this._styles =
options && options.styles ? { ...options.styles } : ({} as Partial<T>);

this.pointerDistance = (options && options.pointerDistance) || 40;

this.coordinatePrecision = (options && options.coordinatePrecision) || 9;
}

type = ModeTypes.Drawing;
Expand Down Expand Up @@ -131,7 +131,7 @@ export abstract class TerraDrawBaseDrawMode<T extends CustomStyling> {
project: this.project,
unproject: this.unproject,
pointerDistance: this.pointerDistance,
coordinatePrecision: this.coordinatePrecision,
coordinatePrecision: config.coordinatePrecision,
});
} else {
throw new Error("Can not register unless mode is unregistered");
Expand Down Expand Up @@ -196,4 +196,13 @@ export abstract class TerraDrawBaseDrawMode<T extends CustomStyling> {
return value;
}
}

/**
* Sets the coordinate precision for the mode.
*
* @param {number} coordinatePrecision The precision value for the coordinates.
*/
public setCoordinatePrecision(coordinatePrecision: number) {
this.coordinatePrecision = coordinatePrecision;
}
JamesLMilner marked this conversation as resolved.
Show resolved Hide resolved
}
18 changes: 12 additions & 6 deletions src/modes/circle/circle.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import { haversineDistanceKilometers } from "../../geometry/measure/haversine-di
import { circle } from "../../geometry/shape/create-circle";
import { GeoJSONStoreFeatures } from "../../store/store";
import { getDefaultStyling } from "../../util/styling";
import { TerraDrawBaseDrawMode } from "../base.mode";
import {
BaseModeOptions,
CustomStyling,
TerraDrawBaseDrawMode,
} from "../base.mode";
import { isValidNonIntersectingPolygonFeature } from "../../geometry/boolean/is-valid-polygon-feature";

type TerraDrawCircleModeKeyEvents = {
Expand All @@ -30,6 +34,12 @@ interface Cursors {
start?: Cursor;
}

interface TerraDrawCircleModeOptions<T extends CustomStyling>
extends BaseModeOptions<T> {
keyEvents?: TerraDrawCircleModeKeyEvents | null;
cursors?: Cursors;
}

export class TerraDrawCircleMode extends TerraDrawBaseDrawMode<CirclePolygonStyling> {
mode = "circle";
private center: Position | undefined;
Expand All @@ -38,11 +48,7 @@ export class TerraDrawCircleMode extends TerraDrawBaseDrawMode<CirclePolygonStyl
private keyEvents: TerraDrawCircleModeKeyEvents;
private cursors: Required<Cursors>;

constructor(options?: {
styles?: Partial<CirclePolygonStyling>;
keyEvents?: TerraDrawCircleModeKeyEvents | null;
cursors?: Cursors;
}) {
constructor(options?: TerraDrawCircleModeOptions<CirclePolygonStyling>) {
super(options);

const defaultCursors = {
Expand Down
22 changes: 14 additions & 8 deletions src/modes/freehand/freehand.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import {
} from "../../common";
import { Polygon } from "geojson";

import { TerraDrawBaseDrawMode } from "../base.mode";
import {
BaseModeOptions,
CustomStyling,
TerraDrawBaseDrawMode,
} from "../base.mode";
import { getDefaultStyling } from "../../util/styling";
import { GeoJSONStoreFeatures } from "../../store/store";
import { pixelDistance } from "../../geometry/measure/pixel-distance";
Expand All @@ -35,6 +39,14 @@ interface Cursors {
close?: Cursor;
}

interface TerraDrawFreehandModeOptions<T extends CustomStyling>
extends BaseModeOptions<T> {
minDistance?: number;
preventPointsNearClose?: boolean;
keyEvents?: TerraDrawFreehandModeKeyEvents | null;
cursors?: Cursors;
}

export class TerraDrawFreehandMode extends TerraDrawBaseDrawMode<FreehandPolygonStyling> {
mode = "freehand";

Expand All @@ -46,13 +58,7 @@ export class TerraDrawFreehandMode extends TerraDrawBaseDrawMode<FreehandPolygon
private cursors: Required<Cursors>;
private preventPointsNearClose: boolean;

constructor(options?: {
styles?: Partial<FreehandPolygonStyling>;
minDistance?: number;
preventPointsNearClose?: boolean;
keyEvents?: TerraDrawFreehandModeKeyEvents | null;
cursors?: Cursors;
}) {
constructor(options?: TerraDrawFreehandModeOptions<FreehandPolygonStyling>) {
super(options);

const defaultCursors = {
Expand Down
22 changes: 14 additions & 8 deletions src/modes/greatcircle/great-circle.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import {
Cursor,
} from "../../common";
import { LineString } from "geojson";
import { TerraDrawBaseDrawMode } from "../base.mode";
import {
BaseModeOptions,
CustomStyling,
TerraDrawBaseDrawMode,
} from "../base.mode";
import { BehaviorConfig } from "../base.behavior";
import { getDefaultStyling } from "../../util/styling";
import { GeoJSONStoreFeatures } from "../../store/store";
Expand Down Expand Up @@ -35,6 +39,14 @@ interface Cursors {
close?: Cursor;
}

interface TerraDrawGreatCircleModeOptions<T extends CustomStyling>
extends BaseModeOptions<T> {
snapping?: boolean;
pointerDistance?: number;
keyEvents?: TerraDrawGreateCircleModeKeyEvents | null;
cursors?: Cursors;
}

export class TerraDrawGreatCircleMode extends TerraDrawBaseDrawMode<GreateCircleStyling> {
mode = "greatcircle";

Expand All @@ -48,13 +60,7 @@ export class TerraDrawGreatCircleMode extends TerraDrawBaseDrawMode<GreateCircle
// Behaviors
private snapping!: GreatCircleSnappingBehavior;

constructor(options?: {
snapping?: boolean;
pointerDistance?: number;
styles?: Partial<GreateCircleStyling>;
keyEvents?: TerraDrawGreateCircleModeKeyEvents | null;
cursors?: Cursors;
}) {
constructor(options?: TerraDrawGreatCircleModeOptions<GreateCircleStyling>) {
super(options);

const defaultCursors = {
Expand Down
24 changes: 15 additions & 9 deletions src/modes/linestring/linestring.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import {
} from "../../common";
import { LineString } from "geojson";
import { selfIntersects } from "../../geometry/boolean/self-intersects";
import { TerraDrawBaseDrawMode } from "../base.mode";
import {
BaseModeOptions,
CustomStyling,
TerraDrawBaseDrawMode,
} from "../base.mode";
import { pixelDistance } from "../../geometry/measure/pixel-distance";
import { BehaviorConfig } from "../base.behavior";
import { ClickBoundingBoxBehavior } from "../click-bounding-box.behavior";
Expand Down Expand Up @@ -36,6 +40,15 @@ interface Cursors {
close?: Cursor;
}

interface TerraDrawLineStringModeOptions<T extends CustomStyling>
extends BaseModeOptions<T> {
snapping?: boolean;
allowSelfIntersections?: boolean;
pointerDistance?: number;
keyEvents?: TerraDrawLineStringModeKeyEvents | null;
cursors?: Cursors;
}

export class TerraDrawLineStringMode extends TerraDrawBaseDrawMode<LineStringStyling> {
mode = "linestring";

Expand All @@ -51,14 +64,7 @@ export class TerraDrawLineStringMode extends TerraDrawBaseDrawMode<LineStringSty
// Behaviors
private snapping!: SnappingBehavior;

constructor(options?: {
snapping?: boolean;
allowSelfIntersections?: boolean;
pointerDistance?: number;
styles?: Partial<LineStringStyling>;
keyEvents?: TerraDrawLineStringModeKeyEvents | null;
cursors?: Cursors;
}) {
constructor(options?: TerraDrawLineStringModeOptions<LineStringStyling>) {
super(options);

const defaultCursors = {
Expand Down
16 changes: 11 additions & 5 deletions src/modes/point/point.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import {
} from "../../common";
import { GeoJSONStoreFeatures } from "../../store/store";
import { getDefaultStyling } from "../../util/styling";
import { TerraDrawBaseDrawMode } from "../base.mode";
import {
BaseModeOptions,
CustomStyling,
TerraDrawBaseDrawMode,
} from "../base.mode";
import { isValidPoint } from "../../geometry/boolean/is-valid-point";

type PointModeStyling = {
Expand All @@ -21,15 +25,17 @@ interface Cursors {
create?: Cursor;
}

interface TerraDrawPointModeOptions<T extends CustomStyling>
extends BaseModeOptions<T> {
cursors?: Cursors;
}

export class TerraDrawPointMode extends TerraDrawBaseDrawMode<PointModeStyling> {
mode = "point";

private cursors: Required<Cursors>;

constructor(options?: {
styles?: Partial<PointModeStyling>;
cursors?: Cursors;
}) {
constructor(options?: TerraDrawPointModeOptions<PointModeStyling>) {
super(options);
const defaultCursors = {
create: "crosshair",
Expand Down
24 changes: 15 additions & 9 deletions src/modes/polygon/polygon.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import {
} from "../../common";
import { Polygon } from "geojson";
import { selfIntersects } from "../../geometry/boolean/self-intersects";
import { TerraDrawBaseDrawMode } from "../base.mode";
import {
TerraDrawBaseDrawMode,
BaseModeOptions,
CustomStyling,
} from "../base.mode";
import { PixelDistanceBehavior } from "../pixel-distance.behavior";
import { ClickBoundingBoxBehavior } from "../click-bounding-box.behavior";
import { BehaviorConfig } from "../base.behavior";
Expand Down Expand Up @@ -41,6 +45,15 @@ interface Cursors {
close?: Cursor;
}

interface TerraDrawPolygonModeOptions<T extends CustomStyling>
extends BaseModeOptions<T> {
allowSelfIntersections?: boolean;
snapping?: boolean;
pointerDistance?: number;
keyEvents?: TerraDrawPolygonModeKeyEvents | null;
cursors?: Cursors;
}

export class TerraDrawPolygonMode extends TerraDrawBaseDrawMode<PolygonStyling> {
mode = "polygon";

Expand All @@ -57,14 +70,7 @@ export class TerraDrawPolygonMode extends TerraDrawBaseDrawMode<PolygonStyling>
private cursors: Required<Cursors>;
private mouseMove = false;

constructor(options?: {
allowSelfIntersections?: boolean;
snapping?: boolean;
pointerDistance?: number;
styles?: Partial<PolygonStyling>;
keyEvents?: TerraDrawPolygonModeKeyEvents | null;
cursors?: Cursors;
}) {
constructor(options?: TerraDrawPolygonModeOptions<PolygonStyling>) {
super(options);

const defaultCursors = {
Expand Down
20 changes: 14 additions & 6 deletions src/modes/rectangle/rectangle.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import {
} from "../../common";
import { GeoJSONStoreFeatures } from "../../store/store";
import { getDefaultStyling } from "../../util/styling";
import { TerraDrawBaseDrawMode } from "../base.mode";
import {
BaseModeOptions,
CustomStyling,
TerraDrawBaseDrawMode,
} from "../base.mode";
import { isValidNonIntersectingPolygonFeature } from "../../geometry/boolean/is-valid-polygon-feature";

type TerraDrawRectangleModeKeyEvents = {
Expand All @@ -28,6 +32,12 @@ interface Cursors {
start?: Cursor;
}

interface TerraDrawRectangleModeOptions<T extends CustomStyling>
extends BaseModeOptions<T> {
keyEvents?: TerraDrawRectangleModeKeyEvents | null;
cursors?: Cursors;
}

export class TerraDrawRectangleMode extends TerraDrawBaseDrawMode<RectanglePolygonStyling> {
mode = "rectangle";
private center: Position | undefined;
Expand All @@ -36,11 +46,9 @@ export class TerraDrawRectangleMode extends TerraDrawBaseDrawMode<RectanglePolyg
private keyEvents: TerraDrawRectangleModeKeyEvents;
private cursors: Required<Cursors>;

constructor(options?: {
styles?: Partial<RectanglePolygonStyling>;
keyEvents?: TerraDrawRectangleModeKeyEvents | null;
cursors?: Cursors;
}) {
constructor(
options?: TerraDrawRectangleModeOptions<RectanglePolygonStyling>,
) {
super(options);

const defaultCursors = {
Expand Down
Loading
Loading