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

feat: context object for onFinish to allow more control for developers #261

Merged
merged 1 commit into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 10 additions & 3 deletions guides/6.EVENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,16 @@ draw.on("change", (ids, type) => {
The other Terra Draw events are:

```typescript
draw.on("finish", (ids: string[]) => {
// Do something
//...
draw.on("finish", (id: string, context: { action: string, mode: string }) => {
if (action === 'draw') {
// Do something for draw finish event
} else if (action === 'dragFeature') {
// Do something for a drag finish event
} else if (action === 'dragCoordinate') {
//
}else if (action === 'dragCoordinateResize') {
//
}
});

draw.on("change", (ids: string[], type: string) => {
Expand Down
4 changes: 3 additions & 1 deletion src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export type GetLngLatFromEvent = (event: PointerEvent | MouseEvent) => {
lat: number;
} | null;

export type OnFinishContext = { mode: string; action: string };

export interface TerraDrawModeRegisterConfig {
mode: string;
store: GeoJSONStore;
Expand All @@ -81,7 +83,7 @@ export interface TerraDrawModeRegisterConfig {
onChange: StoreChangeHandler;
onSelect: (selectedId: string) => void;
onDeselect: (deselectedId: string) => void;
onFinish: (finishedId: string) => void;
onFinish: (finishedId: string, context: OnFinishContext) => void;
project: Project;
unproject: Unproject;
coordinatePrecision: number;
Expand Down
3 changes: 2 additions & 1 deletion src/modes/base.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { BehaviorConfig, TerraDrawModeBehavior } from "./base.behavior";
import {
HexColor,
OnFinishContext,
TerraDrawAdapterStyling,
TerraDrawKeyboardEvent,
TerraDrawModeRegisterConfig,
Expand Down Expand Up @@ -173,7 +174,7 @@ export abstract class TerraDrawBaseDrawMode<T extends CustomStyling> {
abstract cleanUp(): void;
abstract styleFeature(feature: GeoJSONStoreFeatures): TerraDrawAdapterStyling;

onFinish(finishedId: FeatureId) {}
onFinish(finishedId: FeatureId, context: OnFinishContext) {}
onDeselect(deselectedId: FeatureId) {}
onSelect(selectedId: FeatureId) {}
onKeyDown(event: TerraDrawKeyboardEvent) {}
Expand Down
8 changes: 8 additions & 0 deletions src/modes/circle/circle.mode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@ describe("TerraDrawCircleMode", () => {
expect(onChange).toBeCalledTimes(3);
expect(onChange).toBeCalledWith([expect.any(String)], "create");
expect(onFinish).toBeCalledTimes(1);
expect(onFinish).toHaveBeenNthCalledWith(1, expect.any(String), {
action: "draw",
mode: "circle",
});
});
});
});
Expand Down Expand Up @@ -357,6 +361,10 @@ describe("TerraDrawCircleMode", () => {
expect(onChange).toBeCalledTimes(1);
expect(onChange).toBeCalledWith([expect.any(String)], "create");
expect(onFinish).toBeCalledTimes(1);
expect(onFinish).toHaveBeenNthCalledWith(1, expect.any(String), {
action: "draw",
mode: "circle",
});
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/modes/circle/circle.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export class TerraDrawCircleMode extends TerraDrawBaseDrawMode<CirclePolygonStyl
}

// Ensure that any listerers are triggered with the main created geometry
this.onFinish(finishedId);
this.onFinish(finishedId, { mode: this.mode, action: "draw" });
}

/** @internal */
Expand Down
14 changes: 11 additions & 3 deletions src/modes/freehand/freehand.mode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,12 @@ describe("TerraDrawFreehandMode", () => {
features = store.copyAll();
expect(features.length).toBe(1);

expect(onChange).toBeCalledTimes(2);
expect(onFinish).toBeCalled();
expect(onChange).toHaveBeenCalledTimes(2);
expect(onFinish).toHaveBeenCalledTimes(1);
expect(onFinish).toHaveBeenNthCalledWith(1, expect.any(String), {
action: "draw",
mode: "freehand",
});
});
});
});
Expand Down Expand Up @@ -511,7 +515,11 @@ describe("TerraDrawFreehandMode", () => {
[expect.any(String)],
"delete",
);
expect(onFinish).toBeCalledTimes(1);
expect(onFinish).toHaveBeenCalledTimes(1);
expect(onFinish).toHaveBeenNthCalledWith(1, expect.any(String), {
action: "draw",
mode: "freehand",
});
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/modes/freehand/freehand.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class TerraDrawFreehandMode extends TerraDrawBaseDrawMode<FreehandPolygon
}

// Ensure that any listerers are triggered with the main created geometry
this.onFinish(finishedId);
this.onFinish(finishedId, { mode: this.mode, action: "draw" });
}

/** @internal */
Expand Down
4 changes: 4 additions & 0 deletions src/modes/greatcircle/great-circle.mode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,10 @@ describe("TerraDrawGreatCircleMode", () => {
});

expect(onFinish).toBeCalledTimes(1);
expect(onFinish).toHaveBeenNthCalledWith(1, expect.any(String), {
action: "draw",
mode: "greatcircle",
});
});

it("does not finish great circle when finish is set to null", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/modes/greatcircle/great-circle.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export class TerraDrawGreatCircleMode extends TerraDrawBaseDrawMode<GreateCircle
}

// Ensure that any listerers are triggered with the main created geometry
this.onFinish(finishedId);
this.onFinish(finishedId, { mode: this.mode, action: "draw" });
}

/** @internal */
Expand Down
6 changes: 5 additions & 1 deletion src/modes/linestring/linestring.mode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,11 @@ describe("TerraDrawLineStringMode", () => {
[2, 2],
]);

expect(onFinish).toBeCalledTimes(1);
expect(onFinish).toHaveBeenCalledTimes(1);
expect(onFinish).toHaveBeenNthCalledWith(1, expect.any(String), {
action: "draw",
mode: "linestring",
});
});

it("does not finish linestring when finish is set to null", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/modes/linestring/linestring.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export class TerraDrawLineStringMode extends TerraDrawBaseDrawMode<LineStringSty
}

// Ensure that any listerers are triggered with the main created geometry
this.onFinish(finishedId);
this.onFinish(finishedId, { mode: this.mode, action: "draw" });
}

private updateGeometries(
Expand Down
2 changes: 1 addition & 1 deletion src/modes/point/point.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class TerraDrawPointMode extends TerraDrawBaseDrawMode<PointModeStyling>
const [pointId] = this.store.create([{ geometry, properties }]);

// Ensure that any listerers are triggered with the main created geometry
this.onFinish(pointId);
this.onFinish(pointId, { mode: this.mode, action: "draw" });
}

/** @internal */
Expand Down
10 changes: 10 additions & 0 deletions src/modes/polygon/polygon.mode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,7 @@ describe("TerraDrawPolygonMode", () => {
describe("onKeyUp", () => {
let polygonMode: TerraDrawPolygonMode;
let store: GeoJSONStore;
let onFinish: jest.Mock;
let project: jest.Mock;
let unproject: jest.Mock;

Expand Down Expand Up @@ -1082,6 +1083,7 @@ describe("TerraDrawPolygonMode", () => {
store = mockConfig.store;
project = mockConfig.project;
unproject = mockConfig.project;
onFinish = mockConfig.onFinish;
polygonMode.register(mockConfig);
polygonMode.start();
});
Expand Down Expand Up @@ -1249,6 +1251,12 @@ describe("TerraDrawPolygonMode", () => {
heldKeys: [],
});

expect(onFinish).toHaveBeenCalledTimes(1);
expect(onFinish).toHaveBeenNthCalledWith(1, expect.any(String), {
action: "draw",
mode: "polygon",
});

// Creates a new polygon
polygonMode.onClick({
lng: 4,
Expand Down Expand Up @@ -1353,6 +1361,8 @@ describe("TerraDrawPolygonMode", () => {
heldKeys: [],
});

expect(onFinish).not.toHaveBeenCalled();

const features = store.copyAll();

// 2 Closing points and 1 Polygon
Expand Down
2 changes: 1 addition & 1 deletion src/modes/polygon/polygon.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export class TerraDrawPolygonMode extends TerraDrawBaseDrawMode<PolygonStyling>
this.setStarted();
}

this.onFinish(finishedId);
this.onFinish(finishedId, { mode: this.mode, action: "draw" });
}

/** @internal */
Expand Down
6 changes: 5 additions & 1 deletion src/modes/rectangle/rectangle.mode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,11 @@ describe("TerraDrawRectangleMode", () => {

expect(onChange).toBeCalledTimes(2);
expect(onChange).toBeCalledWith([expect.any(String)], "create");
expect(onFinish).toBeCalledTimes(1);
expect(onFinish).toHaveBeenCalledTimes(1);
expect(onFinish).toHaveBeenNthCalledWith(1, expect.any(String), {
action: "draw",
mode: "rectangle",
});
});
});
});
Expand Down
3 changes: 2 additions & 1 deletion src/modes/rectangle/rectangle.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ export class TerraDrawRectangleMode extends TerraDrawBaseDrawMode<RectanglePolyg
this.setStarted();
}

finishedId && this.onFinish(finishedId);
finishedId &&
this.onFinish(finishedId, { mode: this.mode, action: "draw" });
}

/** @internal */
Expand Down
15 changes: 12 additions & 3 deletions src/modes/select/select.mode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2868,7 +2868,10 @@ describe("TerraDrawSelectMode", () => {
);

expect(onFinish).toBeCalledTimes(1);
expect(onFinish).toBeCalledWith(expect.any(String));
expect(onFinish).toBeCalledWith(expect.any(String), {
action: "dragCoordinate",
mode: "select",
});
});

it("fires onFinish for dragged feature if it is currently being dragged", () => {
Expand Down Expand Up @@ -2988,7 +2991,10 @@ describe("TerraDrawSelectMode", () => {
);

expect(onFinish).toBeCalledTimes(1);
expect(onFinish).toBeCalledWith(expect.any(String));
expect(onFinish).toBeCalledWith(expect.any(String), {
action: "dragFeature",
mode: "select",
});
});

it("fires onFinish for resizeable if it is currently being dragged", () => {
Expand Down Expand Up @@ -3108,7 +3114,10 @@ describe("TerraDrawSelectMode", () => {
);

expect(onFinish).toBeCalledTimes(1);
expect(onFinish).toBeCalledWith(expect.any(String));
expect(onFinish).toBeCalledWith(expect.any(String), {
action: "dragCoordinateResize",
mode: "select",
});
});
});

Expand Down
15 changes: 12 additions & 3 deletions src/modes/select/select.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -762,11 +762,20 @@ export class TerraDrawSelectMode extends TerraDrawBaseSelectMode<SelectionStylin
// If we have finished dragging a coordinate or a feature
// lets fire an onFinish event which can be listened to
if (this.dragCoordinate.isDragging()) {
this.onFinish(this.selected[0]);
this.onFinish(this.selected[0], {
mode: this.mode,
action: "dragCoordinate",
});
} else if (this.dragFeature.isDragging()) {
this.onFinish(this.selected[0]);
this.onFinish(this.selected[0], {
mode: this.mode,
action: "dragFeature",
});
} else if (this.dragCoordinateResizeFeature.isDragging()) {
this.onFinish(this.selected[0]);
this.onFinish(this.selected[0], {
mode: this.mode,
action: "dragCoordinateResize",
});
}

this.dragCoordinate.stopDragging();
Expand Down
7 changes: 4 additions & 3 deletions src/terra-draw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
TerraDrawKeyboardEvent,
TerraDrawMouseEvent,
SELECT_PROPERTIES,
OnFinishContext,
} from "./common";
import { TerraDrawBaseAdapter } from "./adapters/common/base.adapter";
import {
Expand Down Expand Up @@ -52,7 +53,7 @@ import { ValidateMinAreaSquareMeters } from "./validations/min-size.validation";
import { ValidateMaxAreaSquareMeters } from "./validations/max-size.validation";
import { ValidateNotSelfIntersecting } from "./validations/not-self-intersecting.validation";

type FinishListener = (ids: FeatureId) => void;
type FinishListener = (id: FeatureId, context: OnFinishContext) => void;
type ChangeListener = (ids: FeatureId[], type: string) => void;
type SelectListener = (id: FeatureId) => void;
type DeselectListener = () => void;
Expand Down Expand Up @@ -164,13 +165,13 @@ class TerraDraw {
return { changed, unchanged };
};

const onFinish = (finishedId: FeatureId) => {
const onFinish = (finishedId: FeatureId, context: OnFinishContext) => {
if (!this._enabled) {
return;
}

this._eventListeners.finish.forEach((listener) => {
listener(finishedId);
listener(finishedId, context);
});
};

Expand Down
Loading