terra-draw
Version:
Frictionless map drawing across mapping provider
212 lines (211 loc) • 10.2 kB
TypeScript
/**
* @module terra-draw
*/
import { TerraDrawAdapter, TerraDrawAdapterStyling, GetLngLatFromEvent, Project, SetCursor, TerraDrawChanges, TerraDrawStylingFunction, Unproject, HexColor, TerraDrawKeyboardEvent, TerraDrawMouseEvent, OnFinishContext, OnChangeContext } from "./common";
import { TerraDrawBaseDrawMode } from "./modes/base.mode";
import { TerraDrawCircleMode } from "./modes/circle/circle.mode";
import { TerraDrawFreehandMode } from "./modes/freehand/freehand.mode";
import { TerraDrawLineStringMode } from "./modes/linestring/linestring.mode";
import { TerraDrawPointMode } from "./modes/point/point.mode";
import { TerraDrawPolygonMode } from "./modes/polygon/polygon.mode";
import { TerraDrawRectangleMode } from "./modes/rectangle/rectangle.mode";
import { TerraDrawRenderMode } from "./modes/render/render.mode";
import { TerraDrawSelectMode } from "./modes/select/select.mode";
import { FeatureId, GeoJSONStoreFeatures, GeoJSONStoreGeometries, IdStrategy, StoreValidation } from "./store/store";
import { BehaviorConfig } from "./modes/base.behavior";
import { ValidateMinAreaSquareMeters } from "./validations/min-size.validation";
import { ValidateMaxAreaSquareMeters } from "./validations/max-size.validation";
import { ValidateNotSelfIntersecting } from "./validations/not-self-intersecting.validation";
import { TerraDrawAngledRectangleMode } from "./modes/angled-rectangle/angled-rectangle.mode";
import { TerraDrawSectorMode } from "./modes/sector/sector.mode";
import { TerraDrawSensorMode } from "./modes/sensor/sensor.mode";
import * as TerraDrawExtend from "./extend";
import { ValidationReasons } from "./validation-reasons";
type InstanceType<T extends new (...args: any[]) => any> = T extends new (...args: any[]) => infer R ? R : never;
type FinishListener = (id: FeatureId, context: OnFinishContext) => void;
type ChangeListener = (ids: FeatureId[], type: string, context?: OnChangeContext) => void;
type SelectListener = (id: FeatureId) => void;
type DeselectListener = () => void;
interface TerraDrawEventListeners {
ready: () => void;
finish: FinishListener;
change: ChangeListener;
select: SelectListener;
deselect: DeselectListener;
}
type TerraDrawEvents = keyof TerraDrawEventListeners;
declare class TerraDraw {
private _modes;
private _mode;
private _adapter;
private _enabled;
private _store;
private _eventListeners;
private _instanceSelectMode;
constructor(options: {
adapter: TerraDrawAdapter;
modes: TerraDrawBaseDrawMode<any>[];
idStrategy?: IdStrategy<FeatureId>;
tracked?: boolean;
});
private checkEnabled;
private getModeStyles;
private featuresAtLocation;
private getSelectMode;
/**
* @deprecated This method is scheduled for removal in the next major version. Instead use the 'updateModeOptions' method passing the
* styles property in the options object, and this will dynamically update the styles for the mode.
*
* Allows the setting of a style for a given mode
*
* @param mode - The mode you wish to set a style for
* @param styles - The styles you wish to set for the mode - this is
* the same as the initialisation style schema
*/
setModeStyles<Styling extends Record<string, number | HexColor>>(mode: string, styles: Styling): void;
/**
* Allow updating of the current options passed to the mode dynamically
* after the mode has been started. You can also use this method to update styles
* as these are passed from the options object.
* @param mode - the mode name you wish to update (the mode name is the public 'mode' property of the mode class)
* @param options - the options object - this allows _partial_ updating of the modes options (i.e. you do not need to pass the whole options object)
*/
updateModeOptions<Mode extends {
new (...args: any[]): any;
}>(mode: InstanceType<Mode>["mode"], options: ConstructorParameters<Mode>[0]): void;
/**
* Allows the user to get a snapshot (copy) of all given features
*
* @returns An array of all given Feature Geometries in the instances store
*/
getSnapshot(): GeoJSONStoreFeatures[];
/**
* Allows the user to get a snapshot (copy) of a given feature by id
*
* @returns A copy of the feature geometry in the instances store
*/
getSnapshotFeature(id: FeatureId): GeoJSONStoreFeatures | undefined;
/**
* Removes all data from the current store and ensures any rendered data is cleared
* from the map.
*/
clear(): void;
/**
* A property used to determine whether the instance is active or not. You
* can use the start method to set this to true, and stop method to set this to false.
* This is a read only property.
*
* @return true or false depending on if the instance is stopped or started
* @readonly
*/
get enabled(): boolean;
/**
* enabled is a read only property and will throw and error if you try and set it.
*/
set enabled(_: boolean);
/**
* A method for getting the current mode name
* @return the current mode name
*/
getMode(): string;
/**
* Get the state of the mode i.e. if we are currently unregistered, registered, drawing etc. This can
* be used to make decisions based on what the current mode is doing.
* @returns the current mode state as a string
*/
getModeState(): import("./common").TerraDrawModeState;
/**
* A method for setting the current mode by name. Under the hood this will stop
* the previous mode and start the new one.
* @param mode - The mode name you wish to start
*/
setMode(mode: string): void;
/**
* A method for removing features to the store
* @param ids
* @returns
*/
removeFeatures(ids: FeatureId[]): void;
/**
* Provides the ability to programmatically select a feature using the instances provided select mode.
* If not select mode is provided in the instance, an error will be thrown. If the instance is not currently
* in the select mode, it will switch to it.
* @param id - the id of the feature to select
*/
selectFeature(id: FeatureId): void;
/**
* Provides the ability to programmatically deselect a feature using the instances provided select mode.
* If not select mode is provided in the instance, an error will be thrown. If the instance is not currently
* in the select mode, it will switch to it.
* @param id - the id of the feature to deselect
*/
deselectFeature(id: FeatureId): void;
/**
* Returns the next feature id from the store - defaults to UUID4 unless you have
* set a custom idStrategy. This method can be useful if you are needing creating features
* outside of the Terra Draw instance but want to add them in to the store.
* @returns a id, either number of string based on whatever the configured idStrategy is
*
*/
getFeatureId(): FeatureId;
/**
* Returns true or false depending on if the Terra Draw instance has a feature with a given id
* @returns a boolean determining if the instance has a feature with the given id
*/
hasFeature(id: FeatureId): boolean;
/**
* A method for adding features to the store. This method will validate the features
* returning an array of validation results. Features must match one of the modes enabled
* in the instance.
* @param features - an array of GeoJSON features
* @returns an array of validation results
*/
addFeatures(features: GeoJSONStoreFeatures[]): StoreValidation[];
/**
* A method starting Terra Draw. It put the instance into a started state, and
* in registers the passed adapter giving it all the callbacks required to operate.
*/
start(): void;
/**
* Gets the features at a given longitude and latitude.
* Will return point and linestrings that are a given pixel distance
* away from the lng/lat and any polygons which contain it.
*/
getFeaturesAtLngLat(lngLat: {
lng: number;
lat: number;
}, options?: {
pointerDistance: number;
ignoreSelectFeatures: boolean;
}): GeoJSONStoreFeatures[];
/**
* Takes a given pointer event and will return point and linestrings that are
* a given pixel distance away from the longitude/latitude, and any polygons which contain it.
*/
getFeaturesAtPointerEvent(event: PointerEvent | MouseEvent, options?: {
pointerDistance?: number;
ignoreSelectFeatures?: boolean;
}): GeoJSONStoreFeatures[];
/**
* A method for stopping Terra Draw. Will clear the store, deregister the adapter and
* remove any rendered layers in the process.
*/
stop(): void;
/**
* Registers a Terra Draw event
*
* @param event - The name of the event you wish to listen for
* @param callback - The callback with you wish to be called when this event occurs
*
*/
on<T extends TerraDrawEvents>(event: T, callback: TerraDrawEventListeners[T]): void;
/**
* Unregisters a Terra Draw event
*
* @param event - The name of the event you wish to unregister
* @param callback - The callback you originally provided to the 'on' method
*
*/
off<T extends TerraDrawEvents>(event: TerraDrawEvents, callback: TerraDrawEventListeners[T]): void;
}
export { TerraDraw, IdStrategy, TerraDrawEvents, TerraDrawEventListeners, TerraDrawSelectMode, TerraDrawPointMode, TerraDrawLineStringMode, TerraDrawPolygonMode, TerraDrawCircleMode, TerraDrawFreehandMode, TerraDrawRenderMode, TerraDrawRectangleMode, TerraDrawAngledRectangleMode, TerraDrawSectorMode, TerraDrawSensorMode, TerraDrawExtend, BehaviorConfig, GeoJSONStoreFeatures, GeoJSONStoreGeometries, HexColor, TerraDrawMouseEvent, TerraDrawAdapterStyling, TerraDrawKeyboardEvent, TerraDrawChanges, TerraDrawStylingFunction, Project, Unproject, SetCursor, GetLngLatFromEvent, ValidateMinAreaSquareMeters, ValidateMaxAreaSquareMeters, ValidateNotSelfIntersecting, ValidationReasons, };