terra-draw 1.0.0-beta.0 → 1.0.0-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/README.md +10 -3
  2. package/dist/adapters/common/adapter-listener.d.ts +22 -0
  3. package/dist/adapters/common/base.adapter.d.ts +59 -0
  4. package/dist/adapters/mapbox-gl.adapter.d.ts +0 -1
  5. package/dist/adapters/openlayers.adapter.d.ts +14 -13
  6. package/dist/common.d.ts +11 -9
  7. package/dist/extend.d.ts +4 -0
  8. package/dist/geometry/calculate-relative-angle.d.ts +9 -0
  9. package/dist/geometry/clockwise.d.ts +2 -0
  10. package/dist/geometry/determine-halfplane.d.ts +2 -0
  11. package/dist/geometry/measure/bearing.d.ts +3 -7
  12. package/dist/geometry/measure/destination.d.ts +2 -0
  13. package/dist/geometry/measure/pixel-distance-to-line.d.ts +2 -10
  14. package/dist/geometry/measure/pixel-distance.d.ts +2 -7
  15. package/dist/geometry/point-on-line.d.ts +5 -0
  16. package/dist/geometry/project/web-mercator.d.ts +2 -4
  17. package/dist/geometry/web-mercator-centroid.d.ts +2 -4
  18. package/dist/geometry/web-mercator-point-on-line.d.ts +11 -0
  19. package/dist/modes/angled-rectangle/angled-rectangle.mode.d.ts +57 -0
  20. package/dist/modes/base.mode.d.ts +3 -1
  21. package/dist/modes/circle/circle.mode.d.ts +3 -2
  22. package/dist/modes/coordinate-snapping.behavior.d.ts +16 -0
  23. package/dist/modes/freehand/freehand.mode.d.ts +8 -2
  24. package/dist/modes/{snapping.behavior.d.ts → line-snapping.behavior.d.ts} +1 -1
  25. package/dist/modes/linestring/linestring.mode.d.ts +11 -4
  26. package/dist/modes/point/point.mode.d.ts +2 -2
  27. package/dist/modes/polygon/polygon.mode.d.ts +14 -4
  28. package/dist/modes/rectangle/rectangle.mode.d.ts +2 -2
  29. package/dist/modes/render/render.mode.d.ts +2 -1
  30. package/dist/modes/sector/sector.mode.d.ts +60 -0
  31. package/dist/modes/select/behaviors/drag-feature.behavior.d.ts +2 -2
  32. package/dist/modes/sensor/sensor.mode.d.ts +69 -0
  33. package/dist/store/store-feature-validation.d.ts +8 -2
  34. package/dist/store/store.d.ts +7 -3
  35. package/dist/terra-draw.cjs +1 -1
  36. package/dist/terra-draw.cjs.map +1 -1
  37. package/dist/terra-draw.d.ts +13 -13
  38. package/dist/terra-draw.modern.js +1 -1
  39. package/dist/terra-draw.modern.js.map +1 -1
  40. package/dist/terra-draw.module.js +1 -1
  41. package/dist/terra-draw.module.js.map +1 -1
  42. package/dist/terra-draw.umd.js +1 -1
  43. package/dist/terra-draw.umd.js.map +1 -1
  44. package/dist/validation-reasons.d.ts +13 -0
  45. package/dist/validations/common-validations.d.ts +2 -0
  46. package/dist/validations/linestring.validation.d.ts +4 -1
  47. package/dist/validations/max-size.validation.d.ts +3 -1
  48. package/dist/validations/min-size.validation.d.ts +3 -1
  49. package/dist/validations/not-self-intersecting.validation.d.ts +4 -1
  50. package/dist/validations/point.validation.d.ts +4 -1
  51. package/dist/validations/polygon.validation.d.ts +8 -2
  52. package/e2e/package-lock.json +5 -4
  53. package/e2e/package.json +1 -1
  54. package/e2e/public/index.html +3 -0
  55. package/e2e/tests/leaflet.spec.ts +490 -14
  56. package/e2e/tests/setup.ts +34 -6
  57. package/eslint.config.js +31 -0
  58. package/package.json +30 -30
  59. package/tsconfig.json +2 -1
  60. package/readme.gif +0 -0
package/README.md CHANGED
@@ -1,4 +1,10 @@
1
- <img src="./logo.png" alt="Terra Draw Logo" width="400"/>
1
+ <picture>
2
+ <source media="(prefers-color-scheme: dark)" srcset="./assets/logo-dark-mode.png">
3
+ <source media="(prefers-color-scheme: light)" srcset="./assets/logo.png">
4
+ <img alt="Terra Draw logo" src="./assets/logo.png" width="400px">
5
+ </picture>
6
+
7
+ <p></p>
2
8
 
3
9
  ![Terra Draw CI Badge](https://github.com/JamesLMilner/terra-draw/actions/workflows/ci.yml/badge.svg)
4
10
  [![npm version](https://badge.fury.io/js/terra-draw.svg)](https://badge.fury.io/js/terra-draw)
@@ -7,14 +13,15 @@ Frictionless map drawing across mapping libraries.
7
13
 
8
14
  Terra Draw centralizes map drawing logic and provides a host of out-of-the-box drawing modes that work across different JavaScript mapping libraries. It also allows you to bring your own modes!
9
15
 
10
- ![An example of drawing geodesic lines using Terra Draw with Leaflet](./readme.gif)
16
+ ![An example of drawing geodesic lines using Terra Draw with Leaflet](./assets/readme.gif)
17
+
11
18
 
12
19
  ### Library Support
13
20
 
14
21
  Terra Draw uses the concept of 'adapters' to allow it to work with a host of different mapping libraries. Currently supported are:
15
22
 
16
23
  - [Leaflet](https://leafletjs.com/) v1
17
- - [OpenLayers](https://openlayers.org/) v7
24
+ - [OpenLayers](https://openlayers.org/) v10
18
25
  - [Mapbox GL JS](https://www.mapbox.com/mapbox-gljs) v2
19
26
  - [MapLibre](https://maplibre.org/maplibre-gl-js/docs/) v2/v3
20
27
  - [Google Maps JS API](https://developers.google.com/maps/documentation/javascript/overview) v3
@@ -0,0 +1,22 @@
1
+ export declare class AdapterListener<Callback extends (...args: any[]) => any> {
2
+ name: string;
3
+ callback: (...args: any[]) => any;
4
+ registered: boolean;
5
+ register: any;
6
+ unregister: any;
7
+ /**
8
+ * Creates a new AdapterListener instance with the provided configuration.
9
+ *
10
+ * @param {Object} config - The configuration object for the listener.
11
+ * @param {string} config.name - The name of the event listener.
12
+ * @param {Function} config.callback - The callback function to be called when the event is triggered.
13
+ * @param {Function} config.unregister - The function to unregister the event listeners.
14
+ * @param {Function} config.register - The function to register the event listeners.
15
+ */
16
+ constructor({ name, callback, unregister, register, }: {
17
+ name: string;
18
+ callback: Callback;
19
+ unregister: (callbacks: Callback) => void;
20
+ register: (callback: Callback) => void;
21
+ });
22
+ }
@@ -0,0 +1,59 @@
1
+ import { Project, Unproject, TerraDrawCallbacks, TerraDrawChanges, TerraDrawMouseEvent, SetCursor, TerraDrawStylingFunction, GetLngLatFromEvent, TerraDrawAdapter } from "../../common";
2
+ import { AdapterListener } from "./adapter-listener";
3
+ type BasePointerListener = (event: PointerEvent) => void;
4
+ type BaseKeyboardListener = (event: KeyboardEvent) => void;
5
+ type BaseMouseListener = (event: MouseEvent) => void;
6
+ export type BaseAdapterConfig = {
7
+ coordinatePrecision?: number;
8
+ minPixelDragDistanceDrawing?: number;
9
+ minPixelDragDistance?: number;
10
+ minPixelDragDistanceSelecting?: number;
11
+ };
12
+ export declare abstract class TerraDrawBaseAdapter implements TerraDrawAdapter {
13
+ constructor(config: BaseAdapterConfig);
14
+ protected _minPixelDragDistance: number;
15
+ protected _minPixelDragDistanceDrawing: number;
16
+ protected _minPixelDragDistanceSelecting: number;
17
+ protected _lastDrawEvent: TerraDrawMouseEvent | undefined;
18
+ protected _coordinatePrecision: number;
19
+ protected _heldKeys: Set<string>;
20
+ protected _listeners: AdapterListener<BasePointerListener | BaseKeyboardListener | BaseMouseListener>[];
21
+ protected _dragState: "not-dragging" | "pre-dragging" | "dragging";
22
+ protected _currentModeCallbacks: TerraDrawCallbacks | undefined;
23
+ abstract getMapEventElement(): HTMLElement;
24
+ protected getButton(event: PointerEvent | MouseEvent): "neither" | "left" | "middle" | "right";
25
+ protected getMapElementXYPosition(event: PointerEvent | MouseEvent): {
26
+ containerX: number;
27
+ containerY: number;
28
+ };
29
+ protected getDrawEventFromEvent(event: PointerEvent | MouseEvent): TerraDrawMouseEvent | null;
30
+ /**
31
+ * Registers the provided callbacks for the current drawing mode and attaches
32
+ * the necessary event listeners.
33
+ * @param {TerraDrawCallbacks} callbacks - An object containing callback functions
34
+ * for handling various drawing events in the current mode.
35
+ */
36
+ register(callbacks: TerraDrawCallbacks): void;
37
+ /**
38
+ * Gets the coordinate precision. The coordinate precision is the number of decimal places in geometry
39
+ * coordinates stored in the store.
40
+ * @returns {number} The coordinate precision.
41
+ */
42
+ getCoordinatePrecision(): number;
43
+ protected getAdapterListeners(): AdapterListener<BasePointerListener>[];
44
+ /**
45
+ * Unregisters the event listeners for the current drawing mode.
46
+ * This is typically called when switching between drawing modes or
47
+ * stopping the drawing process.
48
+ */
49
+ unregister(): void;
50
+ abstract clear(): void;
51
+ abstract project(...args: Parameters<Project>): ReturnType<Project>;
52
+ abstract unproject(...args: Parameters<Unproject>): ReturnType<Unproject>;
53
+ abstract setCursor(...args: Parameters<SetCursor>): ReturnType<SetCursor>;
54
+ abstract getLngLatFromEvent(...event: Parameters<GetLngLatFromEvent>): ReturnType<GetLngLatFromEvent>;
55
+ abstract setDraggability(enabled: boolean): void;
56
+ abstract setDoubleClickToZoom(enabled: boolean): void;
57
+ abstract render(changes: TerraDrawChanges, styling: TerraDrawStylingFunction): void;
58
+ }
59
+ export {};
@@ -22,7 +22,6 @@ export declare class TerraDrawMapboxGLAdapter extends TerraDrawBaseAdapter {
22
22
  private _addLayer;
23
23
  private _addGeoJSONLayer;
24
24
  private _setGeoJSONLayerData;
25
- private getEmptyGeometries;
26
25
  private changedIds;
27
26
  private updateChangedIds;
28
27
  /**
@@ -1,30 +1,31 @@
1
1
  import { TerraDrawChanges, SetCursor, TerraDrawStylingFunction, TerraDrawCallbacks } from "../common";
2
- import CircleGeom from "ol/geom/Circle";
3
2
  import Feature from "ol/Feature";
4
3
  import GeoJSON from "ol/format/GeoJSON";
5
4
  import Map from "ol/Map";
6
5
  import Circle from "ol/style/Circle";
6
+ import Fill from "ol/style/Fill";
7
7
  import Stroke from "ol/style/Stroke";
8
8
  import Style from "ol/style/Style";
9
9
  import VectorSource from "ol/source/Vector";
10
10
  import VectorLayer from "ol/layer/Vector";
11
- import { toLonLat } from "ol/proj";
11
+ import { getUserProjection } from "ol/proj";
12
12
  import { BaseAdapterConfig, TerraDrawBaseAdapter } from "./common/base.adapter";
13
- type InjectableOL = {
14
- Circle: typeof CircleGeom;
13
+ export type InjectableOL = {
14
+ Fill: typeof Fill;
15
15
  Feature: typeof Feature;
16
16
  GeoJSON: typeof GeoJSON;
17
17
  Style: typeof Style;
18
- CircleStyle: typeof Circle;
18
+ Circle: typeof Circle;
19
19
  VectorLayer: typeof VectorLayer;
20
20
  VectorSource: typeof VectorSource;
21
21
  Stroke: typeof Stroke;
22
- toLonLat: typeof toLonLat;
22
+ getUserProjection: typeof getUserProjection;
23
23
  };
24
24
  export declare class TerraDrawOpenLayersAdapter extends TerraDrawBaseAdapter {
25
25
  constructor(config: {
26
26
  map: Map;
27
27
  lib: InjectableOL;
28
+ zIndex?: number;
28
29
  } & BaseAdapterConfig);
29
30
  private stylingFunction;
30
31
  private _lib;
@@ -39,12 +40,6 @@ export declare class TerraDrawOpenLayersAdapter extends TerraDrawBaseAdapter {
39
40
  * @returns an object to red green and blue (RGB) color
40
41
  */
41
42
  private hexToRGB;
42
- /**
43
- * Converts a hexideciaml color to RGB
44
- * @param feature
45
- * @param styling
46
- * @returns an object to red green and blue (RGB) color
47
- */
48
43
  private getStyles;
49
44
  /**
50
45
  * Clears the layers created by the adapter
@@ -53,6 +48,12 @@ export declare class TerraDrawOpenLayersAdapter extends TerraDrawBaseAdapter {
53
48
  private clearLayers;
54
49
  private addFeature;
55
50
  private removeFeature;
51
+ /**
52
+ * Sorts an array of DOM elements based on their order in the document, from earliest to latest.
53
+ * @param elements - An array of `HTMLElement` objects to be sorted.
54
+ * @returns A new array of `HTMLElement` objects sorted by their document order.
55
+ */
56
+ private sortElementsByDOMOrder;
56
57
  /**
57
58
  * Returns the longitude and latitude coordinates from a given PointerEvent on the map.
58
59
  * @param event The PointerEvent or MouseEvent containing the screen coordinates of the pointer.
@@ -113,8 +114,8 @@ export declare class TerraDrawOpenLayersAdapter extends TerraDrawBaseAdapter {
113
114
  * @returns void
114
115
  * */
115
116
  clear(): void;
117
+ private registeredLayerHandlers;
116
118
  register(callbacks: TerraDrawCallbacks): void;
117
119
  getCoordinatePrecision(): number;
118
120
  unregister(): void;
119
121
  }
120
- export {};
package/dist/common.d.ts CHANGED
@@ -15,6 +15,10 @@ export interface TerraDrawAdapterStyling {
15
15
  lineStringColor: HexColor;
16
16
  zIndex: number;
17
17
  }
18
+ export type CartesianPoint = {
19
+ x: number;
20
+ y: number;
21
+ };
18
22
  export interface TerraDrawMouseEvent {
19
23
  lng: number;
20
24
  lat: number;
@@ -28,15 +32,9 @@ export interface TerraDrawKeyboardEvent {
28
32
  heldKeys: string[];
29
33
  preventDefault: () => void;
30
34
  }
31
- export type Required<T> = {
32
- [P in keyof T]-?: T[P];
33
- };
34
35
  export type Cursor = Parameters<SetCursor>[0];
35
36
  export type SetCursor = (cursor: "unset" | "grab" | "grabbing" | "crosshair" | "pointer" | "wait" | "move") => void;
36
- export type Project = (lng: number, lat: number) => {
37
- x: number;
38
- y: number;
39
- };
37
+ export type Project = (lng: number, lat: number) => CartesianPoint;
40
38
  export type Unproject = (x: number, y: number) => {
41
39
  lat: number;
42
40
  lng: number;
@@ -71,7 +69,10 @@ export declare enum UpdateTypes {
71
69
  type ValidationContext = Pick<TerraDrawModeRegisterConfig, "project" | "unproject" | "coordinatePrecision"> & {
72
70
  updateType: UpdateTypes;
73
71
  };
74
- export type Validation = (feature: GeoJSONStoreFeatures, context: ValidationContext) => boolean;
72
+ export type Validation = (feature: GeoJSONStoreFeatures, context: ValidationContext) => {
73
+ valid: boolean;
74
+ reason?: string;
75
+ };
75
76
  export type TerraDrawModeState = "unregistered" | "registered" | "started" | "drawing" | "selecting" | "stopped";
76
77
  export interface TerraDrawCallbacks {
77
78
  getState: () => TerraDrawModeState;
@@ -112,7 +113,8 @@ export declare const SELECT_PROPERTIES: {
112
113
  readonly MID_POINT: "midPoint";
113
114
  readonly SELECTION_POINT: "selectionPoint";
114
115
  };
115
- export declare const POLYGON_PROPERTIES: {
116
+ export declare const COMMON_PROPERTIES: {
116
117
  CLOSING_POINT: string;
118
+ SNAPPING_POINT: string;
117
119
  };
118
120
  export {};
@@ -0,0 +1,4 @@
1
+ import { TerraDrawBaseAdapter, BaseAdapterConfig } from "./adapters/common/base.adapter";
2
+ import { HexColorStyling, NumericStyling, TerraDrawCallbacks } from "./common";
3
+ import { BaseModeOptions, TerraDrawBaseDrawMode } from "./modes/base.mode";
4
+ export { TerraDrawBaseDrawMode, TerraDrawBaseAdapter, BaseAdapterConfig, NumericStyling, HexColorStyling, BaseModeOptions, TerraDrawCallbacks, };
@@ -0,0 +1,9 @@
1
+ import { CartesianPoint } from "../common";
2
+ /**
3
+ * Calculate the relative angle between two lines
4
+ * @param A The first point of the first line
5
+ * @param B The second point of the first line and the first point of the second line
6
+ * @param C The second point of the second line
7
+ * @returns The relative angle between the two lines
8
+ */
9
+ export declare function calculateRelativeAngle(A: CartesianPoint, B: CartesianPoint, C: CartesianPoint): number;
@@ -0,0 +1,2 @@
1
+ import { CartesianPoint } from "../common";
2
+ export declare function isClockwiseWebMercator(center: CartesianPoint, secondCoord: CartesianPoint, thirdCoord: CartesianPoint): boolean;
@@ -0,0 +1,2 @@
1
+ import { CartesianPoint } from "../common";
2
+ export declare function determineHalfPlane(point: CartesianPoint, lineStart: CartesianPoint, lineEnd: CartesianPoint): string;
@@ -1,9 +1,5 @@
1
1
  import { Position } from "geojson";
2
+ import { CartesianPoint } from "../../common";
2
3
  export declare function bearing(start: Position, end: Position): number;
3
- export declare function webMercatorBearing({ x: x1, y: y1 }: {
4
- x: number;
5
- y: number;
6
- }, { x: x2, y: y2 }: {
7
- x: number;
8
- y: number;
9
- }): number;
4
+ export declare function webMercatorBearing({ x: x1, y: y1 }: CartesianPoint, { x: x2, y: y2 }: CartesianPoint): number;
5
+ export declare function normalizeBearing(bearing: number): number;
@@ -1,2 +1,4 @@
1
1
  import { Position } from "geojson";
2
+ import { CartesianPoint } from "../../common";
2
3
  export declare function destination(origin: Position, distance: number, bearing: number): Position;
4
+ export declare function webMercatorDestination({ x, y }: CartesianPoint, distance: number, bearing: number): CartesianPoint;
@@ -1,10 +1,2 @@
1
- export declare const pixelDistanceToLine: (point: {
2
- x: number;
3
- y: number;
4
- }, linePointOne: {
5
- x: number;
6
- y: number;
7
- }, linePointTwo: {
8
- x: number;
9
- y: number;
10
- }) => number;
1
+ import { CartesianPoint } from "../../common";
2
+ export declare const pixelDistanceToLine: (point: CartesianPoint, linePointOne: CartesianPoint, linePointTwo: CartesianPoint) => number;
@@ -1,7 +1,2 @@
1
- export declare const pixelDistance: (pointOne: {
2
- x: number;
3
- y: number;
4
- }, pointTwo: {
5
- x: number;
6
- y: number;
7
- }) => number;
1
+ import { CartesianPoint } from "../../common";
2
+ export declare const cartesianDistance: (pointOne: CartesianPoint, pointTwo: CartesianPoint) => number;
@@ -0,0 +1,5 @@
1
+ import { Position } from "geojson";
2
+ export declare function nearestPointOnLine(inputCoordinate: Position, lines: [Position, Position][]): {
3
+ coordinate: Position;
4
+ distance: number;
5
+ } | undefined;
@@ -1,13 +1,11 @@
1
+ import { CartesianPoint } from "../../common";
1
2
  /**
2
3
  * Convert longitude and latitude to web mercator x and y
3
4
  * @param lng
4
5
  * @param lat
5
6
  * @returns - web mercator x and y
6
7
  */
7
- export declare const lngLatToWebMercatorXY: (lng: number, lat: number) => {
8
- x: number;
9
- y: number;
10
- };
8
+ export declare const lngLatToWebMercatorXY: (lng: number, lat: number) => CartesianPoint;
11
9
  /**
12
10
  * Convert web mercator x and y to longitude and latitude
13
11
  * @param x - web mercator x
@@ -1,11 +1,9 @@
1
1
  import { Feature, LineString, Polygon } from "geojson";
2
+ import { CartesianPoint } from "../common";
2
3
  /**
3
4
  * Calculates the centroid of a GeoJSON Polygon or LineString in Web Mercator
4
5
 
5
6
  * @param {Feature<Polygon | LineString>} feature - The GeoJSON Feature containing either a Polygon or LineString
6
7
  * @returns {{ x: number, y: number }} The centroid of the polygon or line string in Web Mercator coordinates.
7
8
  */
8
- export declare function webMercatorCentroid(feature: Feature<Polygon | LineString>): {
9
- x: number;
10
- y: number;
11
- };
9
+ export declare function webMercatorCentroid(feature: Feature<Polygon | LineString>): CartesianPoint;
@@ -0,0 +1,11 @@
1
+ import { Position } from "geojson";
2
+ /**
3
+ * Takes two points and finds the closest point on the line between them to a third point.
4
+ * @param lines
5
+ * @param inputCoordinate
6
+ * @returns
7
+ */
8
+ export declare function webMercatorNearestPointOnLine(inputCoordinate: Position, lines: [Position, Position][]): {
9
+ coordinate: Position;
10
+ distance: number;
11
+ } | undefined;
@@ -0,0 +1,57 @@
1
+ import { TerraDrawMouseEvent, TerraDrawAdapterStyling, TerraDrawKeyboardEvent, HexColorStyling, NumericStyling, Cursor } from "../../common";
2
+ import { TerraDrawBaseDrawMode, BaseModeOptions, CustomStyling } from "../base.mode";
3
+ import { GeoJSONStoreFeatures, StoreValidation } from "../../store/store";
4
+ type TerraDrawPolygonModeKeyEvents = {
5
+ cancel?: KeyboardEvent["key"] | null;
6
+ finish?: KeyboardEvent["key"] | null;
7
+ };
8
+ type PolygonStyling = {
9
+ fillColor: HexColorStyling;
10
+ outlineColor: HexColorStyling;
11
+ outlineWidth: NumericStyling;
12
+ fillOpacity: NumericStyling;
13
+ };
14
+ interface Cursors {
15
+ start?: Cursor;
16
+ close?: Cursor;
17
+ }
18
+ interface TerraDrawPolygonModeOptions<T extends CustomStyling> extends BaseModeOptions<T> {
19
+ pointerDistance?: number;
20
+ keyEvents?: TerraDrawPolygonModeKeyEvents | null;
21
+ cursors?: Cursors;
22
+ }
23
+ export declare class TerraDrawAngledRectangleMode extends TerraDrawBaseDrawMode<PolygonStyling> {
24
+ mode: string;
25
+ private currentCoordinate;
26
+ private currentId;
27
+ private keyEvents;
28
+ private cursors;
29
+ private mouseMove;
30
+ constructor(options?: TerraDrawPolygonModeOptions<PolygonStyling>);
31
+ private close;
32
+ /** @internal */
33
+ start(): void;
34
+ /** @internal */
35
+ stop(): void;
36
+ /** @internal */
37
+ onMouseMove(event: TerraDrawMouseEvent): void;
38
+ private updatePolygonGeometry;
39
+ /** @internal */
40
+ onClick(event: TerraDrawMouseEvent): void;
41
+ /** @internal */
42
+ onKeyUp(event: TerraDrawKeyboardEvent): void;
43
+ /** @internal */
44
+ onKeyDown(): void;
45
+ /** @internal */
46
+ onDragStart(): void;
47
+ /** @internal */
48
+ onDrag(): void;
49
+ /** @internal */
50
+ onDragEnd(): void;
51
+ /** @internal */
52
+ cleanUp(): void;
53
+ /** @internal */
54
+ styleFeature(feature: GeoJSONStoreFeatures): TerraDrawAdapterStyling;
55
+ validateFeature(feature: unknown): StoreValidation;
56
+ }
57
+ export {};
@@ -40,7 +40,9 @@ export declare abstract class TerraDrawBaseDrawMode<T extends CustomStyling> {
40
40
  protected setStarted(): void;
41
41
  protected setStopped(): void;
42
42
  register(config: TerraDrawModeRegisterConfig): void;
43
- validateFeature(feature: unknown): feature is GeoJSONStoreFeatures;
43
+ validateFeature(feature: unknown): ReturnType<Validation>;
44
+ private performFeatureValidation;
45
+ protected validateModeFeature(feature: unknown, modeValidationFn: (feature: GeoJSONStoreFeatures) => ReturnType<Validation>): ReturnType<Validation>;
44
46
  abstract start(): void;
45
47
  abstract stop(): void;
46
48
  abstract cleanUp(): void;
@@ -1,5 +1,5 @@
1
1
  import { TerraDrawMouseEvent, TerraDrawAdapterStyling, TerraDrawKeyboardEvent, HexColorStyling, NumericStyling, Cursor, Projection } from "../../common";
2
- import { GeoJSONStoreFeatures } from "../../store/store";
2
+ import { GeoJSONStoreFeatures, StoreValidation } from "../../store/store";
3
3
  import { BaseModeOptions, CustomStyling, TerraDrawBaseDrawMode } from "../base.mode";
4
4
  type TerraDrawCircleModeKeyEvents = {
5
5
  cancel: KeyboardEvent["key"] | null;
@@ -28,6 +28,7 @@ export declare class TerraDrawCircleMode extends TerraDrawBaseDrawMode<CirclePol
28
28
  private keyEvents;
29
29
  private cursors;
30
30
  private startingRadiusKilometers;
31
+ private cursorMovedAfterInitialCursorDown;
31
32
  /**
32
33
  * Create a new circle mode instance
33
34
  * @param options - Options to customize the behavior of the circle mode
@@ -60,7 +61,7 @@ export declare class TerraDrawCircleMode extends TerraDrawBaseDrawMode<CirclePol
60
61
  cleanUp(): void;
61
62
  /** @internal */
62
63
  styleFeature(feature: GeoJSONStoreFeatures): TerraDrawAdapterStyling;
63
- validateFeature(feature: unknown): feature is GeoJSONStoreFeatures;
64
+ validateFeature(feature: unknown): StoreValidation;
64
65
  private updateCircle;
65
66
  }
66
67
  export {};
@@ -0,0 +1,16 @@
1
+ import { BehaviorConfig, TerraDrawModeBehavior } from "./base.behavior";
2
+ import { TerraDrawMouseEvent } from "../common";
3
+ import { Position } from "geojson";
4
+ import { ClickBoundingBoxBehavior } from "./click-bounding-box.behavior";
5
+ import { FeatureId } from "../store/store";
6
+ import { PixelDistanceBehavior } from "./pixel-distance.behavior";
7
+ export declare class CoordinateSnappingBehavior extends TerraDrawModeBehavior {
8
+ readonly config: BehaviorConfig;
9
+ private readonly pixelDistance;
10
+ private readonly clickBoundingBox;
11
+ constructor(config: BehaviorConfig, pixelDistance: PixelDistanceBehavior, clickBoundingBox: ClickBoundingBoxBehavior);
12
+ /** Returns the nearest snappable coordinate - on first click there is no currentId so no need to provide */
13
+ getSnappableCoordinateFirstClick: (event: TerraDrawMouseEvent) => Position | undefined;
14
+ getSnappableCoordinate: (event: TerraDrawMouseEvent, currentFeatureId: FeatureId) => Position | undefined;
15
+ private getSnappable;
16
+ }
@@ -1,6 +1,6 @@
1
1
  import { TerraDrawMouseEvent, TerraDrawAdapterStyling, TerraDrawKeyboardEvent, HexColorStyling, NumericStyling, Cursor } from "../../common";
2
2
  import { BaseModeOptions, CustomStyling, TerraDrawBaseDrawMode } from "../base.mode";
3
- import { GeoJSONStoreFeatures } from "../../store/store";
3
+ import { GeoJSONStoreFeatures, StoreValidation } from "../../store/store";
4
4
  type TerraDrawFreehandModeKeyEvents = {
5
5
  cancel: KeyboardEvent["key"] | null;
6
6
  finish: KeyboardEvent["key"] | null;
@@ -22,6 +22,8 @@ interface Cursors {
22
22
  interface TerraDrawFreehandModeOptions<T extends CustomStyling> extends BaseModeOptions<T> {
23
23
  minDistance?: number;
24
24
  preventPointsNearClose?: boolean;
25
+ autoClose?: boolean;
26
+ autoCloseTimeout?: number;
25
27
  keyEvents?: TerraDrawFreehandModeKeyEvents | null;
26
28
  cursors?: Cursors;
27
29
  }
@@ -34,6 +36,10 @@ export declare class TerraDrawFreehandMode extends TerraDrawBaseDrawMode<Freehan
34
36
  private keyEvents;
35
37
  private cursors;
36
38
  private preventPointsNearClose;
39
+ private autoClose;
40
+ private autoCloseTimeout;
41
+ private hasLeftStartingPoint;
42
+ private preventNewFeature;
37
43
  constructor(options?: TerraDrawFreehandModeOptions<FreehandPolygonStyling>);
38
44
  private close;
39
45
  /** @internal */
@@ -58,6 +64,6 @@ export declare class TerraDrawFreehandMode extends TerraDrawBaseDrawMode<Freehan
58
64
  cleanUp(): void;
59
65
  /** @internal */
60
66
  styleFeature(feature: GeoJSONStoreFeatures): TerraDrawAdapterStyling;
61
- validateFeature(feature: unknown): feature is GeoJSONStoreFeatures;
67
+ validateFeature(feature: unknown): StoreValidation;
62
68
  }
63
69
  export {};
@@ -4,7 +4,7 @@ import { Position } from "geojson";
4
4
  import { ClickBoundingBoxBehavior } from "./click-bounding-box.behavior";
5
5
  import { FeatureId } from "../store/store";
6
6
  import { PixelDistanceBehavior } from "./pixel-distance.behavior";
7
- export declare class SnappingBehavior extends TerraDrawModeBehavior {
7
+ export declare class LineSnappingBehavior extends TerraDrawModeBehavior {
8
8
  readonly config: BehaviorConfig;
9
9
  private readonly pixelDistance;
10
10
  private readonly clickBoundingBox;
@@ -1,7 +1,7 @@
1
1
  import { TerraDrawMouseEvent, TerraDrawAdapterStyling, TerraDrawKeyboardEvent, HexColorStyling, NumericStyling, Cursor } from "../../common";
2
2
  import { BaseModeOptions, CustomStyling, TerraDrawBaseDrawMode } from "../base.mode";
3
3
  import { BehaviorConfig } from "../base.behavior";
4
- import { GeoJSONStoreFeatures } from "../../store/store";
4
+ import { GeoJSONStoreFeatures, StoreValidation } from "../../store/store";
5
5
  type TerraDrawLineStringModeKeyEvents = {
6
6
  cancel: KeyboardEvent["key"] | null;
7
7
  finish: KeyboardEvent["key"] | null;
@@ -13,6 +13,10 @@ type LineStringStyling = {
13
13
  closingPointWidth: NumericStyling;
14
14
  closingPointOutlineColor: HexColorStyling;
15
15
  closingPointOutlineWidth: NumericStyling;
16
+ snappingPointColor: HexColorStyling;
17
+ snappingPointWidth: NumericStyling;
18
+ snappingPointOutlineColor: HexColorStyling;
19
+ snappingPointOutlineWidth: NumericStyling;
16
20
  };
17
21
  interface Cursors {
18
22
  start?: Cursor;
@@ -23,7 +27,9 @@ interface InertCoordinates {
23
27
  value: number;
24
28
  }
25
29
  interface TerraDrawLineStringModeOptions<T extends CustomStyling> extends BaseModeOptions<T> {
26
- snapping?: boolean;
30
+ snapping?: {
31
+ toCoordinate?: boolean;
32
+ };
27
33
  pointerDistance?: number;
28
34
  keyEvents?: TerraDrawLineStringModeKeyEvents | null;
29
35
  cursors?: Cursors;
@@ -40,7 +46,8 @@ export declare class TerraDrawLineStringMode extends TerraDrawBaseDrawMode<LineS
40
46
  private mouseMove;
41
47
  private insertCoordinates;
42
48
  private lastCommitedCoordinates;
43
- private snapping;
49
+ private snappedPointId;
50
+ private coordinateSnapping;
44
51
  private insertPoint;
45
52
  constructor(options?: TerraDrawLineStringModeOptions<LineStringStyling>);
46
53
  private close;
@@ -73,6 +80,6 @@ export declare class TerraDrawLineStringMode extends TerraDrawBaseDrawMode<LineS
73
80
  cleanUp(): void;
74
81
  /** @internal */
75
82
  styleFeature(feature: GeoJSONStoreFeatures): TerraDrawAdapterStyling;
76
- validateFeature(feature: unknown): feature is GeoJSONStoreFeatures;
83
+ validateFeature(feature: unknown): StoreValidation;
77
84
  }
78
85
  export {};
@@ -1,5 +1,5 @@
1
1
  import { TerraDrawMouseEvent, TerraDrawAdapterStyling, NumericStyling, HexColorStyling, Cursor } from "../../common";
2
- import { GeoJSONStoreFeatures } from "../../store/store";
2
+ import { GeoJSONStoreFeatures, StoreValidation } from "../../store/store";
3
3
  import { BaseModeOptions, CustomStyling, TerraDrawBaseDrawMode } from "../base.mode";
4
4
  type PointModeStyling = {
5
5
  pointWidth: NumericStyling;
@@ -39,6 +39,6 @@ export declare class TerraDrawPointMode extends TerraDrawBaseDrawMode<PointModeS
39
39
  onDragEnd(): void;
40
40
  /** @internal */
41
41
  styleFeature(feature: GeoJSONStoreFeatures): TerraDrawAdapterStyling;
42
- validateFeature(feature: unknown): feature is GeoJSONStoreFeatures;
42
+ validateFeature(feature: unknown): StoreValidation;
43
43
  }
44
44
  export {};
@@ -1,7 +1,7 @@
1
1
  import { TerraDrawMouseEvent, TerraDrawAdapterStyling, TerraDrawKeyboardEvent, HexColorStyling, NumericStyling, Cursor } from "../../common";
2
2
  import { TerraDrawBaseDrawMode, BaseModeOptions, CustomStyling } from "../base.mode";
3
3
  import { BehaviorConfig } from "../base.behavior";
4
- import { GeoJSONStoreFeatures } from "../../store/store";
4
+ import { GeoJSONStoreFeatures, StoreValidation } from "../../store/store";
5
5
  type TerraDrawPolygonModeKeyEvents = {
6
6
  cancel?: KeyboardEvent["key"] | null;
7
7
  finish?: KeyboardEvent["key"] | null;
@@ -15,13 +15,20 @@ type PolygonStyling = {
15
15
  closingPointColor: HexColorStyling;
16
16
  closingPointOutlineWidth: NumericStyling;
17
17
  closingPointOutlineColor: HexColorStyling;
18
+ snappingPointWidth: NumericStyling;
19
+ snappingPointColor: HexColorStyling;
20
+ snappingPointOutlineWidth: NumericStyling;
21
+ snappingPointOutlineColor: HexColorStyling;
18
22
  };
19
23
  interface Cursors {
20
24
  start?: Cursor;
21
25
  close?: Cursor;
22
26
  }
23
27
  interface TerraDrawPolygonModeOptions<T extends CustomStyling> extends BaseModeOptions<T> {
24
- snapping?: boolean;
28
+ snapping?: {
29
+ toLine?: boolean;
30
+ toCoordinate?: boolean;
31
+ };
25
32
  pointerDistance?: number;
26
33
  keyEvents?: TerraDrawPolygonModeKeyEvents | null;
27
34
  cursors?: Cursors;
@@ -32,7 +39,9 @@ export declare class TerraDrawPolygonMode extends TerraDrawBaseDrawMode<PolygonS
32
39
  private currentId;
33
40
  private keyEvents;
34
41
  private snappingEnabled;
35
- private snapping;
42
+ private snappedPointId;
43
+ private lineSnapping;
44
+ private coordinateSnapping;
36
45
  private pixelDistance;
37
46
  private closingPoints;
38
47
  private cursors;
@@ -48,6 +57,7 @@ export declare class TerraDrawPolygonMode extends TerraDrawBaseDrawMode<PolygonS
48
57
  /** @internal */
49
58
  onMouseMove(event: TerraDrawMouseEvent): void;
50
59
  private updatePolygonGeometry;
60
+ private snapCoordinate;
51
61
  /** @internal */
52
62
  onClick(event: TerraDrawMouseEvent): void;
53
63
  /** @internal */
@@ -64,6 +74,6 @@ export declare class TerraDrawPolygonMode extends TerraDrawBaseDrawMode<PolygonS
64
74
  cleanUp(): void;
65
75
  /** @internal */
66
76
  styleFeature(feature: GeoJSONStoreFeatures): TerraDrawAdapterStyling;
67
- validateFeature(feature: unknown): feature is GeoJSONStoreFeatures;
77
+ validateFeature(feature: unknown): StoreValidation;
68
78
  }
69
79
  export {};