Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -397,16 +397,17 @@ export class TerraDrawAngledRectangleMode extends TerraDrawBaseDrawMode<PolygonS

/** @internal */
cleanUp() {
try {
if (this.currentId) {
this.mutateFeature.deleteFeature(this.currentId);
}
} catch (error) {}
const currentId = this.currentId;

this.currentId = undefined;
this.currentCoordinate = 0;
if (this.state === "drawing") {
this.setStarted();
}

if (currentId && this.readFeature.hasFeature(currentId)) {
this.mutateFeature.deleteFeature(currentId);
}
}

/** @internal */
Expand Down
15 changes: 8 additions & 7 deletions packages/terra-draw/src/modes/circle/circle.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { Polygon } from "geojson";
import { calculateWebMercatorDistortion } from "../../geometry/shape/web-mercator-distortion";
import { BehaviorConfig } from "../base.behavior";
import { MutateFeatureBehavior, Mutations } from "../mutate-feature.behavior";
import { ReadFeatureBehavior } from "../read-feature.behavior";

type TerraDrawCircleModeKeyEvents = {
cancel: KeyboardEvent["key"] | null;
Expand Down Expand Up @@ -79,7 +80,8 @@ export class TerraDrawCircleMode extends TerraDrawBaseDrawMode<CirclePolygonStyl
private drawType: DrawType | undefined;

// Behaviors
public mutateFeature!: MutateFeatureBehavior;
private readFeature!: ReadFeatureBehavior;
private mutateFeature!: MutateFeatureBehavior;

/**
* Create a new circle mode instance
Expand Down Expand Up @@ -299,7 +301,7 @@ export class TerraDrawCircleMode extends TerraDrawBaseDrawMode<CirclePolygonStyl

/** @internal */
cleanUp() {
const cleanUpId = this.currentCircleId;
const currentId = this.currentCircleId;

this.center = undefined;
this.currentCircleId = undefined;
Expand All @@ -309,11 +311,9 @@ export class TerraDrawCircleMode extends TerraDrawBaseDrawMode<CirclePolygonStyl
this.setStarted();
}

try {
if (cleanUpId !== undefined) {
this.mutateFeature.deleteFeature(cleanUpId);
}
} catch {}
if (currentId !== undefined && this.readFeature.hasFeature(currentId)) {
this.mutateFeature.deleteFeature(currentId);
}
}

/** @internal */
Expand Down Expand Up @@ -449,6 +449,7 @@ export class TerraDrawCircleMode extends TerraDrawBaseDrawMode<CirclePolygonStyl
}

registerBehaviors(config: BehaviorConfig) {
this.readFeature = new ReadFeatureBehavior(config);
this.mutateFeature = new MutateFeatureBehavior(config, {
validate: this.validate,
onFinish: (featureId, context) => {
Expand Down
7 changes: 6 additions & 1 deletion packages/terra-draw/src/modes/closing-points.behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ export class ClosingPointsBehavior extends TerraDrawModeBehavior {

public delete() {
if (this.ids.length) {
this.mutateFeatureBehavior.deleteFeatures(this.ids);
const existingIds = this.ids.filter((id) =>
this.readFeatureBehavior.hasFeature(id),
);
if (existingIds.length) {
this.mutateFeatureBehavior.deleteFeatures(existingIds);
}
this._startEndPoints = [];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,11 @@ export class TerraDrawFreehandLineStringMode extends TerraDrawBaseDrawMode<Freeh
this.setStarted();
}

try {
if (cleanUpId !== undefined) {
this.mutateFeature.deleteFeature(cleanUpId);
}
if (cleanUpId !== undefined && this.readFeature.hasFeature(cleanUpId)) {
this.mutateFeature.deleteFeature(cleanUpId);
}

this.closingPoints.delete();
} catch (error) {}
this.closingPoints.delete();
}

/** @internal */
Expand Down
17 changes: 9 additions & 8 deletions packages/terra-draw/src/modes/freehand/freehand.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,14 +320,15 @@ export class TerraDrawFreehandMode extends TerraDrawBaseDrawMode<FreehandPolygon
this.setStarted();
}

try {
if (cleanUpId !== undefined) {
this.mutateFeature.deleteFeature(cleanUpId);
}
if (cleanUpClosingPointId !== undefined) {
this.mutateFeature.deleteFeature(cleanUpClosingPointId);
}
} catch (error) {}
if (cleanUpId !== undefined && this.readFeature.hasFeature(cleanUpId)) {
this.mutateFeature.deleteFeature(cleanUpId);
}
if (
cleanUpClosingPointId !== undefined &&
this.readFeature.hasFeature(cleanUpClosingPointId)
) {
this.mutateFeature.deleteFeature(cleanUpClosingPointId);
}
}

/** @internal */
Expand Down
26 changes: 17 additions & 9 deletions packages/terra-draw/src/modes/linestring/linestring.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,13 @@ export class TerraDrawLineStringMode extends TerraDrawBaseDrawMode<LineStringSty

/** @internal */
onClick(event: TerraDrawMouseEvent) {
if (
this.currentId !== undefined &&
!this.readFeature.hasFeature(this.currentId)
) {
this.cleanUp();
}

if (
(event.button === "right" &&
this.allowPointerEvent(this.pointerEvents.rightClick, event)) ||
Expand Down Expand Up @@ -750,15 +757,16 @@ export class TerraDrawLineStringMode extends TerraDrawBaseDrawMode<LineStringSty
this.setStarted();
}

try {
if (cleanUpId !== undefined) {
this.mutateFeature.deleteFeature(cleanUpId);
}
if (snappedPointId !== undefined) {
this.mutateFeature.deleteFeature(snappedPointId);
}
this.closingPoints.delete();
} catch (error) {}
if (cleanUpId !== undefined && this.readFeature.hasFeature(cleanUpId)) {
this.mutateFeature.deleteFeature(cleanUpId);
}
if (
snappedPointId !== undefined &&
this.readFeature.hasFeature(snappedPointId)
) {
this.mutateFeature.deleteFeature(snappedPointId);
}
this.closingPoints.delete();
}

/** @internal */
Expand Down
3 changes: 2 additions & 1 deletion packages/terra-draw/src/modes/marker/marker.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Z_INDEX,
UrlStyling,
MARKER_URL_DEFAULT,
FinishActions,
} from "../../common";
import {
FeatureId,
Expand Down Expand Up @@ -279,7 +280,7 @@ export class TerraDrawMarkerMode extends TerraDrawBaseDrawMode<MarkerModeStyling
mode: this.mode,
[COMMON_PROPERTIES.MARKER]: true,
},
context: { updateType: UpdateTypes.Finish },
context: { updateType: UpdateTypes.Finish, action: FinishActions.Draw },
});
}

Expand Down
16 changes: 10 additions & 6 deletions packages/terra-draw/src/modes/mutate-feature.behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ export class MutateFeatureBehavior extends TerraDrawModeBehavior {
}: {
coordinates: Position;
properties: JSONObject;
context?: {
updateType: UpdateTypes.Finish;
};
context?: FinishContext;
}) {
// Create point is slightly different in that creating can also be the finish action
// because there is only one step to creating a point.
Expand Down Expand Up @@ -238,6 +236,9 @@ export class MutateFeatureBehavior extends TerraDrawModeBehavior {
}

public deleteFeatures(featureIds: FeatureId[]) {
if (featureIds.length === 0) {
return;
}
this.store.delete(featureIds);
}

Expand Down Expand Up @@ -330,23 +331,26 @@ export class MutateFeatureBehavior extends TerraDrawModeBehavior {
return null;
}

const feature = this.buildFeatureWithGeometry<G>(featureId);

// Handle special case where there are no coordinate mutations but we want to validate on finish
if (context.updateType === UpdateTypes.Finish) {
if (!coordinateMutations) {
if (
!this.validateGeometryWithUpdateType({
geometry: this.store.getGeometryCopy(featureId),
properties: this.store.getPropertiesCopy(featureId),
geometry: feature.geometry,
properties: feature.properties,
updateType: context.updateType,
})
) {
return null;
}
}

this.options.onFinish(featureId, context as FinishContext);
}

return this.buildFeatureWithGeometry<G>(featureId);
return feature;
}

public epsilonOffset() {
Expand Down
3 changes: 2 additions & 1 deletion packages/terra-draw/src/modes/point/point.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
UpdateTypes,
COMMON_PROPERTIES,
Z_INDEX,
FinishActions,
} from "../../common";
import {
FeatureId,
Expand Down Expand Up @@ -299,7 +300,7 @@ export class TerraDrawPointMode extends TerraDrawBaseDrawMode<PointModeStyling>
mode: this.mode,
[COMMON_PROPERTIES.MARKER]: true,
},
context: { updateType: UpdateTypes.Finish },
context: { updateType: UpdateTypes.Finish, action: FinishActions.Draw },
});
}

Expand Down
38 changes: 21 additions & 17 deletions packages/terra-draw/src/modes/polygon/polygon.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1009,24 +1009,28 @@ export class TerraDrawPolygonMode extends TerraDrawBaseDrawMode<PolygonStyling>
this.setStarted();
}

try {
if (cleanUpId) {
this.coordinatePoints.deletePointsByFeatureIds([cleanUpId]);
}
if (cleanUpId) {
this.coordinatePoints.deletePointsByFeatureIds([cleanUpId]);
}

if (cleanUpId !== undefined) {
this.mutateFeature.deleteFeature(cleanUpId);
}
if (editedPointId !== undefined) {
this.mutateFeature.deleteFeature(editedPointId);
}
if (snappedPointId !== undefined) {
this.mutateFeature.deleteFeature(snappedPointId);
}
if (this.closingPoints.ids.length) {
this.closingPoints.delete();
}
} catch (error) {}
if (cleanUpId !== undefined && this.readFeature.hasFeature(cleanUpId)) {
this.mutateFeature.deleteFeature(cleanUpId);
}
if (
editedPointId !== undefined &&
this.readFeature.hasFeature(editedPointId)
) {
this.mutateFeature.deleteFeature(editedPointId);
}
if (
snappedPointId !== undefined &&
this.readFeature.hasFeature(snappedPointId)
) {
this.mutateFeature.deleteFeature(snappedPointId);
}
if (this.closingPoints.ids.length) {
this.closingPoints.delete();
}
}

/** @internal */
Expand Down
6 changes: 5 additions & 1 deletion packages/terra-draw/src/modes/read-feature.behavior.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BehaviorConfig, TerraDrawModeBehavior } from "./base.behavior";
import { FeatureId } from "../extend";
import { GeoJSONStoreGeometries } from "../store/store";
import { GeoJSONStoreGeometries, JSONObject } from "../store/store";
import { Position, Point } from "geojson";
import { coordinatesIdentical } from "../geometry/coordinates-identical";

Expand Down Expand Up @@ -74,4 +74,8 @@ export class ReadFeatureBehavior extends TerraDrawModeBehavior {
public hasFeature(featureId: FeatureId) {
return this.store.has(featureId);
}

public getAllFeatureIdsWhere(equals: (properties: JSONObject) => boolean) {
return this.store.copyAllWhere(equals).map(({ id }) => id as FeatureId);
}
}
11 changes: 6 additions & 5 deletions packages/terra-draw/src/modes/rectangle/rectangle.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
import { ValidateNonIntersectingPolygonFeature } from "../../validations/polygon.validation";
import { BehaviorConfig } from "../base.behavior";
import { MutateFeatureBehavior, Mutations } from "../mutate-feature.behavior";
import { ReadFeatureBehavior } from "../read-feature.behavior";

type TerraDrawRectangleModeKeyEvents = {
cancel: KeyboardEvent["key"] | null;
Expand Down Expand Up @@ -70,6 +71,7 @@ export class TerraDrawRectangleMode extends TerraDrawBaseDrawMode<RectanglePolyg

// Behaviors
private mutateFeature!: MutateFeatureBehavior;
private readFeature!: ReadFeatureBehavior;

constructor(
options?: TerraDrawRectangleModeOptions<RectanglePolygonStyling>,
Expand Down Expand Up @@ -319,11 +321,9 @@ export class TerraDrawRectangleMode extends TerraDrawBaseDrawMode<RectanglePolyg
this.setStarted();
}

try {
if (cleanUpId !== undefined) {
this.mutateFeature.deleteFeature(cleanUpId);
}
} catch {}
if (cleanUpId !== undefined && this.readFeature.hasFeature(cleanUpId)) {
this.mutateFeature.deleteFeature(cleanUpId);
}
}

/** @internal */
Expand Down Expand Up @@ -390,6 +390,7 @@ export class TerraDrawRectangleMode extends TerraDrawBaseDrawMode<RectanglePolyg
}

registerBehaviors(config: BehaviorConfig) {
this.readFeature = new ReadFeatureBehavior(config);
this.mutateFeature = new MutateFeatureBehavior(config, {
validate: this.validate,
onFinish: (featureId, context) => {
Expand Down
11 changes: 6 additions & 5 deletions packages/terra-draw/src/modes/sector/sector.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,17 +428,18 @@ export class TerraDrawSectorMode extends TerraDrawBaseDrawMode<SectorPolygonStyl

/** @internal */
cleanUp() {
try {
if (this.currentId) {
this.mutateFeature.deleteFeature(this.currentId);
}
} catch (error) {}
const currentId = this.currentId;

this.currentId = undefined;
this.direction = undefined;
this.currentCoordinate = 0;
if (this.state === "drawing") {
this.setStarted();
}

if (currentId && this.readFeature.hasFeature(currentId)) {
this.mutateFeature.deleteFeature(currentId);
}
}

/** @internal */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ describe("CoordinatePointBehavior", () => {

coordinatePointBehavior.deletePointsByFeatureIds([featureId]);

expect(config.store.delete).toHaveBeenCalledTimes(1);
expect(config.store.delete).toHaveBeenNthCalledWith(1, []);
// Ensure no delete calls were made because we have already deleted the points
expect(config.store.delete).toHaveBeenCalledTimes(0);

const propertiesAfterDelete = config.store.getPropertiesCopy(featureId);
expect(propertiesAfterDelete.coordinatePointIds).toBe(null);
Expand Down
Loading
Loading