diff --git a/src/modes/angled-rectangle/angled-rectangle.mode.ts b/src/modes/angled-rectangle/angled-rectangle.mode.ts index 69bf3c10..aa0763db 100644 --- a/src/modes/angled-rectangle/angled-rectangle.mode.ts +++ b/src/modes/angled-rectangle/angled-rectangle.mode.ts @@ -13,8 +13,6 @@ import { BaseModeOptions, CustomStyling, } from "../base.mode"; -import { PixelDistanceBehavior } from "../pixel-distance.behavior"; -import { BehaviorConfig } from "../base.behavior"; import { coordinatesIdentical } from "../../geometry/coordinates-identical"; import { getDefaultStyling } from "../../util/styling"; import { FeatureId, GeoJSONStoreFeatures } from "../../store/store"; @@ -64,7 +62,6 @@ export class TerraDrawAngledRectangleMode extends TerraDrawBaseDrawMode; private mouseMove = false; @@ -113,11 +110,6 @@ export class TerraDrawAngledRectangleMode extends TerraDrawBaseDrawMode { - describe("constructor", () => { - it("constructs", () => { - const config = mockBehaviorConfig("test"); - new ClosingPointsBehavior(config, new PixelDistanceBehavior(config)); - }); - }); - - describe("api", () => { - let startEndPointBehavior: ClosingPointsBehavior; - let config: BehaviorConfig; - - beforeEach(() => { - config = mockBehaviorConfig("test"); - startEndPointBehavior = new ClosingPointsBehavior( - config, - new PixelDistanceBehavior(config), - ); - }); - - describe("ids", () => { - it("get returns empty array", () => { - expect(startEndPointBehavior.ids).toStrictEqual([]); - }); - - it("set fails", () => { - startEndPointBehavior.ids = ["test"]; - - expect(startEndPointBehavior.ids).toStrictEqual([]); - }); - }); - - describe("create", () => { - it("throws error if not enough coordinates", () => { - expect(() => { - startEndPointBehavior.create( - [ - [0, 0], - [0, 1], - ], - "polygon", - ); - }).toThrowError(); - }); - - it("creates correctly when enough coordinates are present", () => { - startEndPointBehavior.create( - [ - [0, 0], - [0, 1], - [1, 1], - [1, 0], - [0, 0], - ], - "polygon", - ); - - expect(startEndPointBehavior.ids.length).toBe(2); - expect(startEndPointBehavior.ids[0]).toBeUUID4(); - expect(startEndPointBehavior.ids[1]).toBeUUID4(); - }); - - it("create can't be run twice", () => { - startEndPointBehavior.create( - [ - [0, 0], - [0, 1], - [1, 1], - [1, 0], - [0, 0], - ], - "polygon", - ); - - expect(() => { - startEndPointBehavior.create( - [ - [0, 0], - [0, 1], - [1, 1], - [1, 0], - [0, 0], - ], - "polygon", - ); - }).toThrowError(); - }); - }); - - describe("delete", () => { - it("without closing points has no effect", () => { - expect(startEndPointBehavior.ids.length).toBe(0); - startEndPointBehavior.delete(); - expect(startEndPointBehavior.ids.length).toBe(0); - }); - - it("with closing points deletes them", () => { - startEndPointBehavior.create( - [ - [0, 0], - [0, 1], - [1, 1], - [1, 0], - [0, 0], - ], - "polygon", - ); - - expect(startEndPointBehavior.ids.length).toBe(2); - startEndPointBehavior.delete(); - expect(startEndPointBehavior.ids.length).toBe(0); - }); - }); - - describe("update", () => { - it("throws error if nothing created", () => { - expect(() => { - startEndPointBehavior.update([ - [0, 0], - [0, 1], - [1, 1], - [1, 0], - [0, 0], - ]); - }).toThrowError(); - }); - - it("updates geometries correctly", () => { - jest.spyOn(config.store, "updateGeometry"); - - startEndPointBehavior.create( - [ - [0, 0], - [0, 1], - [1, 1], - [0, 0], - ], - "polygon", - ); - - expect(config.store.updateGeometry).toHaveBeenCalledTimes(0); - - startEndPointBehavior.update([ - [0, 0], - [0, 1], - [1, 1], - [1, 0], - [0, 0], - ]); - - expect(config.store.updateGeometry).toHaveBeenCalledTimes(1); - }); - }); - - describe("isClosingPoint", () => { - it("returns isClosing as true when in vicinity of closing point", () => { - jest.spyOn(config, "project"); - - startEndPointBehavior.create( - [ - [0, 0], - [0, 1], - [1, 1], - [1, 0], - [0, 0], - ], - "polygon", - ); - - (config.project as jest.Mock).mockReturnValueOnce({ - x: 0, - y: 0, - }); - - (config.project as jest.Mock).mockReturnValueOnce({ - x: 50, - y: 50, - }); - - const { isClosing, isPreviousClosing } = - startEndPointBehavior.isClosingPoint( - mockDrawEvent({ containerX: 0, containerY: 0 }), - ); - - expect(isClosing).toBe(true); - expect(isPreviousClosing).toBe(false); - }); - - it("returns isPreviousClosing as true when in vicinity of closing point", () => { - jest.spyOn(config, "project"); - - startEndPointBehavior.create( - [ - [0, 0], - [0, 1], - [1, 1], - [1, 0], - [0, 0], - ], - "polygon", - ); - - (config.project as jest.Mock).mockReturnValueOnce({ - x: 50, - y: 50, - }); - - (config.project as jest.Mock).mockReturnValueOnce({ - x: 0, - y: 0, - }); - - const { isClosing, isPreviousClosing } = - startEndPointBehavior.isClosingPoint( - mockDrawEvent({ containerX: 0, containerY: 0 }), - ); - - expect(isClosing).toBe(false); - expect(isPreviousClosing).toBe(true); - }); - - it("returns both as false when in vicinity of closing point", () => { - jest.spyOn(config, "project"); - - startEndPointBehavior.create( - [ - [0, 0], - [0, 1], - [1, 1], - [1, 0], - [0, 0], - ], - "polygon", - ); - - (config.project as jest.Mock).mockReturnValueOnce({ - x: 50, - y: 50, - }); - - (config.project as jest.Mock).mockReturnValueOnce({ - x: 100, - y: 100, - }); - - const { isClosing, isPreviousClosing } = - startEndPointBehavior.isClosingPoint( - mockDrawEvent({ containerX: 0, containerY: 0 }), - ); - - expect(isClosing).toBe(false); - expect(isPreviousClosing).toBe(false); - }); - }); - }); -}); diff --git a/src/modes/angled-rectangle/behaviors/closing-points.behavior.ts b/src/modes/angled-rectangle/behaviors/closing-points.behavior.ts deleted file mode 100644 index 7b636c50..00000000 --- a/src/modes/angled-rectangle/behaviors/closing-points.behavior.ts +++ /dev/null @@ -1,112 +0,0 @@ -import { Point, Position } from "geojson"; -import { BehaviorConfig, TerraDrawModeBehavior } from "../../base.behavior"; -import { POLYGON_PROPERTIES, TerraDrawMouseEvent } from "../../../common"; -import { PixelDistanceBehavior } from "../../pixel-distance.behavior"; - -export class ClosingPointsBehavior extends TerraDrawModeBehavior { - constructor( - readonly config: BehaviorConfig, - private readonly pixelDistance: PixelDistanceBehavior, - ) { - super(config); - } - - private _startEndPoints: string[] = []; - - get ids() { - return this._startEndPoints.concat(); - } - - set ids(_: string[]) {} - - public create(selectedCoords: Position[], mode: string) { - if (this.ids.length) { - throw new Error("Opening and closing points already created"); - } - - if (selectedCoords.length <= 3) { - throw new Error("Requires at least 4 coordinates"); - } - - this._startEndPoints = this.store.create( - // Opening coordinate - [ - { - geometry: { - type: "Point", - coordinates: selectedCoords[0], - } as Point, - properties: { - mode, - [POLYGON_PROPERTIES.CLOSING_POINT]: true, - }, - }, - // Final coordinate - { - geometry: { - type: "Point", - coordinates: selectedCoords[selectedCoords.length - 2], - } as Point, - properties: { - mode, - [POLYGON_PROPERTIES.CLOSING_POINT]: true, - }, - }, - ], - ); - } - - public delete() { - if (this.ids.length) { - this.store.delete(this.ids); - this._startEndPoints = []; - } - } - - public update(updatedCoordinates: Position[]) { - if (this.ids.length !== 2) { - throw new Error("No closing points to update"); - } - - this.store.updateGeometry( - // Opening coordinate - [ - { - id: this.ids[0], - geometry: { - type: "Point", - coordinates: updatedCoordinates[0], - } as Point, - }, - // Final coordinate - { - id: this.ids[1], - geometry: { - type: "Point", - coordinates: updatedCoordinates[updatedCoordinates.length - 3], - } as Point, - }, - ], - ); - } - - public isClosingPoint(event: TerraDrawMouseEvent) { - const opening = this.store.getGeometryCopy(this.ids[0]); - const closing = this.store.getGeometryCopy(this.ids[1]); - - const distance = this.pixelDistance.measure( - event, - opening.coordinates as Position, - ); - - const distancePrevious = this.pixelDistance.measure( - event, - closing.coordinates as Position, - ); - - const isClosing = distance < this.pointerDistance; - const isPreviousClosing = distancePrevious < this.pointerDistance; - - return { isClosing, isPreviousClosing }; - } -}