Skip to content

Commit

Permalink
fix: ensure that base config parameters can be configured for adapters (
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesLMilner authored Jan 14, 2024
1 parent 6b62cec commit 8e1775b
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 35 deletions.
13 changes: 7 additions & 6 deletions src/adapters/arcgis-maps-sdk.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
TerraDrawChanges,
TerraDrawStylingFunction,
} from "../common";
import { TerraDrawBaseAdapter } from "./common/base.adapter";
import { BaseAdapterConfig, TerraDrawBaseAdapter } from "./common/base.adapter";
import MapView from "@arcgis/core/views/MapView";
import Point from "@arcgis/core/geometry/Point";
import Polyline from "@arcgis/core/geometry/Polyline";
Expand Down Expand Up @@ -44,11 +44,12 @@ export class TerraDrawArcGISMapsSDKAdapter extends TerraDrawBaseAdapter {
private _dragHandler: undefined | IHandle;
private _doubleClickHandler: undefined | IHandle;

constructor(config: {
map: MapView;
lib: InjectableArcGISMapsSDK;
coordinatePrecision?: number;
}) {
constructor(
config: {
map: MapView;
lib: InjectableArcGISMapsSDK;
} & BaseAdapterConfig,
) {
super(config);

this._mapView = config.map;
Expand Down
14 changes: 8 additions & 6 deletions src/adapters/common/base.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ type BasePointerListener = (event: PointerEvent) => void;
type BaseKeyboardListener = (event: KeyboardEvent) => void;
type BaseMouseListener = (event: MouseEvent) => void;

export type BaseAdapterConfig = {
coordinatePrecision?: number;
minPixelDragDistanceDrawing?: number;
minPixelDragDistance?: number;
minPixelDragDistanceSelecting?: number;
};

export abstract class TerraDrawBaseAdapter {
constructor(config: {
coordinatePrecision?: number;
minPixelDragDistanceDrawing?: number;
minPixelDragDistance?: number;
minPixelDragDistanceSelecting?: number;
}) {
constructor(config: BaseAdapterConfig) {
this._minPixelDragDistance =
typeof config.minPixelDragDistance === "number"
? config.minPixelDragDistance
Expand Down
4 changes: 4 additions & 0 deletions src/adapters/google-maps.adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ describe("TerraDrawGoogleMapsAdapter", () => {
})),
} as any,
map: createMockGoogleMap(),
minPixelDragDistance: 1,
minPixelDragDistanceSelecting: 8,
minPixelDragDistanceDrawing: 8,
coordinatePrecision: 9,
});

expect(adapter).toBeDefined();
Expand Down
13 changes: 7 additions & 6 deletions src/adapters/google-maps.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import {
TerraDrawCallbacks,
} from "../common";
import { GeoJsonObject } from "geojson";
import { TerraDrawBaseAdapter } from "./common/base.adapter";
import { BaseAdapterConfig, TerraDrawBaseAdapter } from "./common/base.adapter";

export class TerraDrawGoogleMapsAdapter extends TerraDrawBaseAdapter {
constructor(config: {
lib: typeof google.maps;
map: google.maps.Map;
coordinatePrecision?: number;
}) {
constructor(
config: {
lib: typeof google.maps;
map: google.maps.Map;
} & BaseAdapterConfig,
) {
super(config);
this._lib = config.lib;
this._map = config.map;
Expand Down
4 changes: 4 additions & 0 deletions src/adapters/leaflet.adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ describe("TerraDrawLeafletAdapter", () => {
geoJSON: jest.fn(),
} as any,
map: createLeafletMap() as L.Map,
minPixelDragDistance: 1,
minPixelDragDistanceSelecting: 8,
minPixelDragDistanceDrawing: 8,
coordinatePrecision: 9,
});

expect(adapter).toBeDefined();
Expand Down
14 changes: 7 additions & 7 deletions src/adapters/leaflet.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import {
} from "../common";
import L from "leaflet";
import { GeoJSONStoreFeatures } from "../store/store";
import { TerraDrawBaseAdapter } from "./common/base.adapter";
import { BaseAdapterConfig, TerraDrawBaseAdapter } from "./common/base.adapter";

export class TerraDrawLeafletAdapter extends TerraDrawBaseAdapter {
constructor(config: {
lib: typeof L;
map: L.Map;
coordinatePrecision?: number;
minPixelDragDistance?: number;
}) {
constructor(
config: {
lib: typeof L;
map: L.Map;
} & BaseAdapterConfig,
) {
super(config);

this._lib = config.lib;
Expand Down
4 changes: 4 additions & 0 deletions src/adapters/mapbox-gl.adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ describe("TerraDrawMapboxGLAdapter", () => {
it("instantiates the adapter correctly", () => {
const adapter = new TerraDrawMapboxGLAdapter({
map: createMapboxGLMap() as mapboxgl.Map,
minPixelDragDistance: 1,
minPixelDragDistanceSelecting: 8,
minPixelDragDistanceDrawing: 8,
coordinatePrecision: 9,
});

expect(adapter).toBeDefined();
Expand Down
4 changes: 2 additions & 2 deletions src/adapters/mapbox-gl.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import mapboxgl, {
PointLike,
} from "mapbox-gl";
import { GeoJSONStoreFeatures, GeoJSONStoreGeometries } from "../store/store";
import { TerraDrawBaseAdapter } from "./common/base.adapter";
import { BaseAdapterConfig, TerraDrawBaseAdapter } from "./common/base.adapter";

export class TerraDrawMapboxGLAdapter extends TerraDrawBaseAdapter {
constructor(config: { map: mapboxgl.Map; coordinatePrecision?: number }) {
constructor(config: { map: mapboxgl.Map } & BaseAdapterConfig) {
super(config);

this._map = config.map;
Expand Down
4 changes: 4 additions & 0 deletions src/adapters/maplibre-gl.adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ describe("TerraDrawMapLibreGLAdapter", () => {
it("instantiates the adapter correctly", () => {
const adapter = new TerraDrawMapLibreGLAdapter({
map: createMapLibreGLMap() as maplibregl.Map,
minPixelDragDistance: 1,
minPixelDragDistanceSelecting: 8,
minPixelDragDistanceDrawing: 8,
coordinatePrecision: 9,
});

expect(adapter).toBeDefined();
Expand Down
4 changes: 2 additions & 2 deletions src/adapters/maplibre-gl.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import {
} from "../common";
import { Map } from "maplibre-gl";
import { TerraDrawMapboxGLAdapter } from "./mapbox-gl.adapter";
import { TerraDrawBaseAdapter } from "./common/base.adapter";
import { BaseAdapterConfig, TerraDrawBaseAdapter } from "./common/base.adapter";

export class TerraDrawMapLibreGLAdapter extends TerraDrawBaseAdapter {
private mapboxglAdapter: TerraDrawMapboxGLAdapter;

constructor(config: { map: Map; coordinatePrecision?: number }) {
constructor(config: { map: Map } & BaseAdapterConfig) {
super(config);

// At the moment the APIs of MapboxGL and MapLibre are so compatible that
Expand Down
4 changes: 4 additions & 0 deletions src/adapters/openlayers.adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ describe("TerraDrawOpenLayersAdapter", () => {
VectorSource: jest.fn(),
VectorLayer: jest.fn(),
} as any,
minPixelDragDistance: 1,
minPixelDragDistanceSelecting: 8,
minPixelDragDistanceDrawing: 8,
coordinatePrecision: 9,
});

expect(adapter).toBeDefined();
Expand Down
13 changes: 7 additions & 6 deletions src/adapters/openlayers.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import VectorSource from "ol/source/Vector";
import VectorLayer from "ol/layer/Vector";
import { fromLonLat, toLonLat } from "ol/proj";
import Geometry from "ol/geom/Geometry";
import { TerraDrawBaseAdapter } from "./common/base.adapter";
import { BaseAdapterConfig, TerraDrawBaseAdapter } from "./common/base.adapter";

type InjectableOL = {
Circle: typeof CircleGeom;
Expand All @@ -31,11 +31,12 @@ type InjectableOL = {
};

export class TerraDrawOpenLayersAdapter extends TerraDrawBaseAdapter {
constructor(config: {
map: Map;
lib: InjectableOL;
coordinatePrecision?: number;
}) {
constructor(
config: {
map: Map;
lib: InjectableOL;
} & BaseAdapterConfig,
) {
super(config);

this._map = config.map;
Expand Down

0 comments on commit 8e1775b

Please sign in to comment.