Skip to content

Commit

Permalink
fix: ensure circle mode works on touch devices (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesLMilner authored Dec 26, 2023
1 parent 4825301 commit ebdaed6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ describe("TerraDrawCircleMode", () => {
circleMode.register(mockConfig);
circleMode.start();
});

it("adds a circle to store if registered", () => {
circleMode.onClick({
lng: 0,
Expand Down Expand Up @@ -180,7 +181,7 @@ describe("TerraDrawCircleMode", () => {
features = store.copyAll();
expect(features.length).toBe(1);

expect(onChange).toBeCalledTimes(1);
expect(onChange).toBeCalledTimes(2);
expect(onChange).toBeCalledWith([expect.any(String)], "create");

expect(onFinish).toBeCalledTimes(1);
Expand Down
40 changes: 24 additions & 16 deletions src/modes/circle/circle.mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,29 +128,18 @@ export class TerraDrawCircleMode extends TerraDrawBaseDrawMode<CirclePolygonStyl
this.clickCount++;
this.setDrawing();
} else {
if (this.clickCount === 1 && this.center && this.currentCircleId) {
this.createCircle(event);
}

// Finish drawing
this.close();
}
}

/** @internal */
onMouseMove(event: TerraDrawMouseEvent) {
if (this.clickCount === 1 && this.center && this.currentCircleId) {
const distanceKm = haversineDistanceKilometers(this.center, [
event.lng,
event.lat,
]);

const updatedCircle = circle({
center: this.center,
radiusKilometers: distanceKm,
coordinatePrecision: this.coordinatePrecision,
});

this.store.updateGeometry([
{ id: this.currentCircleId, geometry: updatedCircle.geometry },
]);
}
this.createCircle(event);
}

/** @internal */
Expand Down Expand Up @@ -238,4 +227,23 @@ export class TerraDrawCircleMode extends TerraDrawBaseDrawMode<CirclePolygonStyl
return false;
}
}

private createCircle(event: TerraDrawMouseEvent) {
if (this.clickCount === 1 && this.center && this.currentCircleId) {
const distanceKm = haversineDistanceKilometers(this.center, [
event.lng,
event.lat,
]);

const updatedCircle = circle({
center: this.center,
radiusKilometers: distanceKm,
coordinatePrecision: this.coordinatePrecision,
});

this.store.updateGeometry([
{ id: this.currentCircleId, geometry: updatedCircle.geometry },
]);
}
}
}

0 comments on commit ebdaed6

Please sign in to comment.