Skip to content

Commit b69c2c9

Browse files
authored
fix(terra-draw): trigger finish event when inserting or deleting coordinate in select mode (#709)
1 parent 576d155 commit b69c2c9

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

packages/terra-draw/src/modes/select/select.mode.spec.ts

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ describe("TerraDrawSelectMode", () => {
437437
);
438438
});
439439

440-
it("creates midpoints when flag enabled", () => {
440+
it("creates midpoints when flag enabled and feature selected", () => {
441441
setSelectMode({
442442
flags: {
443443
polygon: {
@@ -507,6 +507,57 @@ describe("TerraDrawSelectMode", () => {
507507
);
508508
});
509509

510+
it("inserts a midpoint coordinate when flag enabled and midpoint clicked", () => {
511+
setSelectMode({
512+
flags: {
513+
polygon: {
514+
feature: {
515+
draggable: false,
516+
coordinates: { draggable: false, midpoints: true },
517+
},
518+
},
519+
},
520+
});
521+
522+
addPolygonToStore([
523+
[0, 0],
524+
[0, 1],
525+
[1, 1],
526+
[1, 0],
527+
[0, 0],
528+
]);
529+
530+
expect(onChange).toHaveBeenNthCalledWith(
531+
1,
532+
[expect.any(String)],
533+
"create",
534+
undefined,
535+
);
536+
537+
// Store the ids of the created feature
538+
const idOne = onChange.mock.calls[0][0] as string[];
539+
540+
// Select polygon
541+
selectMode.onClick(MockCursorEvent({ lng: 0.5, lat: 0.5 }));
542+
543+
expect(onSelect).toHaveBeenCalledTimes(1);
544+
expect(onSelect).toHaveBeenNthCalledWith(1, idOne[0]);
545+
546+
// Polygon selected set to true
547+
expect(onChange).toHaveBeenNthCalledWith(2, idOne, "update", {
548+
target: "properties",
549+
});
550+
551+
// Create midpoint by clicking on it
552+
selectMode.onClick(MockCursorEvent({ lng: 0, lat: 0.5 }));
553+
554+
expect(onFinish).toHaveBeenCalledTimes(1);
555+
expect(onFinish).toHaveBeenNthCalledWith(1, idOne[0], {
556+
action: "insertMidpoint",
557+
mode: "select",
558+
});
559+
});
560+
510561
describe("switch selected", () => {
511562
it("without selection points flag", () => {
512563
setSelectMode({
@@ -987,6 +1038,8 @@ describe("TerraDrawSelectMode", () => {
9871038
// Only called for checking distance to selection points,
9881039
// should hit early return otherwise
9891040
expect(store.getGeometryCopy).toHaveBeenCalledTimes(4);
1041+
1042+
expect(onFinish).not.toHaveBeenCalled();
9901043
});
9911044

9921045
it("returns early if creates a invalid polygon by deleting coordinate", () => {
@@ -1087,6 +1140,11 @@ describe("TerraDrawSelectMode", () => {
10871140

10881141
expect(store.delete).toHaveBeenCalledTimes(1);
10891142
expect(store.updateGeometry).toHaveBeenCalledTimes(1);
1143+
expect(onFinish).toHaveBeenCalledTimes(1);
1144+
expect(onFinish).toHaveBeenCalledWith(expect.any(String), {
1145+
action: "deleteCoordinate",
1146+
mode: "select",
1147+
});
10901148
});
10911149
});
10921150

@@ -2618,6 +2676,12 @@ describe("TerraDrawSelectMode", () => {
26182676
MockCursorEvent({ lng: 0, lat: 0.5 }),
26192677
setMapDraggability,
26202678
);
2679+
2680+
expect(onFinish).toHaveBeenCalledTimes(1);
2681+
expect(onFinish).toHaveBeenCalledWith(expect.any(String), {
2682+
action: "insertMidpoint",
2683+
mode: "select",
2684+
});
26212685
});
26222686
});
26232687
});

packages/terra-draw/src/modes/select/select.mode.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,11 @@ export class TerraDrawSelectMode extends TerraDrawBaseSelectMode<SelectionStylin
459459
) {
460460
this.midPoints.create(coordinates, featureId, this.coordinatePrecision);
461461
}
462+
463+
this.onFinish(featureId, {
464+
action: "deleteCoordinate",
465+
mode: this.mode,
466+
});
462467
}
463468

464469
private select(featureId: FeatureId, fromCursor = true) {
@@ -543,6 +548,11 @@ export class TerraDrawSelectMode extends TerraDrawBaseSelectMode<SelectionStylin
543548
this.coordinatePrecision,
544549
);
545550

551+
this.onFinish(this.selected[0], {
552+
action: "insertMidpoint",
553+
mode: this.mode,
554+
});
555+
546556
return;
547557
}
548558

@@ -743,6 +753,11 @@ export class TerraDrawSelectMode extends TerraDrawBaseSelectMode<SelectionStylin
743753
this.coordinatePrecision,
744754
);
745755

756+
this.onFinish(this.selected[0], {
757+
action: "insertMidpoint",
758+
mode: this.mode,
759+
});
760+
746761
const draggableCoordinateIndexAfterInsert =
747762
this.dragCoordinate.getDraggableIndex(event, selectedId);
748763

0 commit comments

Comments
 (0)