From 5928a42638d1d113a56e1c6cf7b2692b1f86d295 Mon Sep 17 00:00:00 2001 From: Josh Larson Date: Thu, 14 Mar 2024 09:36:10 -0400 Subject: [PATCH] feat: Drop start and end points exactly where the cursor is (#2496) This also removes the closestPosition function, since we also removed its only invocation. --- assets/src/components/detours/detourMap.tsx | 9 +--- assets/src/util/math.ts | 45 ------------------- .../components/detours/detourMap.test.tsx | 8 +--- assets/tests/util/math.test.ts | 32 +------------ 4 files changed, 4 insertions(+), 90 deletions(-) diff --git a/assets/src/components/detours/detourMap.tsx b/assets/src/components/detours/detourMap.tsx index 976bf58c3..05f6b7b6e 100644 --- a/assets/src/components/detours/detourMap.tsx +++ b/assets/src/components/detours/detourMap.tsx @@ -5,7 +5,6 @@ import Leaflet from "leaflet" import Map from "../map" import { CustomControl } from "../map/controls/customControl" import { ReactMarker } from "../map/utilities/reactMarker" -import { closestPosition } from "../../util/math" import { ShapePoint, Stop } from "../../schedule" import { latLngLiteralToShapePoint, @@ -215,13 +214,7 @@ export const DetourMap = ({ : ["c-detour_map--original-route-shape__unfinished"] } onClick={(e) => { - const { position } = - closestPosition( - originalShape.map(shapePointToLatLngLiteral), - e.latlng - ) ?? {} - position && - onClickOriginalShape(latLngLiteralToShapePoint(position)) + onClickOriginalShape(latLngLiteralToShapePoint(e.latlng)) }} > {!startPoint && Click to start detour} diff --git a/assets/src/util/math.ts b/assets/src/util/math.ts index 5858bbc9d..0fbbcf1ca 100644 --- a/assets/src/util/math.ts +++ b/assets/src/util/math.ts @@ -1,5 +1,3 @@ -import { LatLng, LatLngLiteral } from "leaflet" - /** * A helper function to help clarify code when clamping a value between a range. * (Remove when tc39 implements `Math.clamp`) @@ -9,46 +7,3 @@ import { LatLng, LatLngLiteral } from "leaflet" */ export const clamp = (value: number, min: number, max: number): number => Math.min(Math.max(min, value), max) - -interface ClosestPosition { - position: LatLngLiteral - index: number - distance: number -} - -/** - * Finds the element in {@link positions} that is closest to {@link point} - * @param positions List of coordinates to search - * @param point Point of reference to check distance to - * @returns - * if {@link positions} is empty, returns `undefined` - * - * Otherwise, returns a {@link ClosestPosition} object containing - * - {@link ClosestPosition.position}: the closest element in {@link positions} to {@link point} - * - {@link ClosestPosition.index}: the index of the element in {@link positions} - * - {@link ClosestPosition.distance}: the distance to {@link point} - */ -export const closestPosition = ( - positions: LatLngLiteral[], - point: LatLng -): ClosestPosition | undefined => { - const positionsByDistance = positions - .map((currentPosition, index) => ({ - distance: point.distanceTo(currentPosition), - position: currentPosition, - index, - })) - .sort((lhs, rhs) => lhs.distance - rhs.distance) - - return positionsByDistance[0] ?? undefined - - /** - * In the future, we may want to be able to snap to the line _line_ formed - * by {@link positions}. - * (in the case of, e.g., a low resolution line formed by {@link positions}). - * - * Now that we have the sorted {@link positionsByDistance}, we can interpolate - * the two closest points with respect to {@link point} to find the closest - * point on the line. - */ -} diff --git a/assets/tests/components/detours/detourMap.test.tsx b/assets/tests/components/detours/detourMap.test.tsx index 97e07dd93..04dcce3cf 100644 --- a/assets/tests/components/detours/detourMap.test.tsx +++ b/assets/tests/components/detours/detourMap.test.tsx @@ -47,19 +47,15 @@ const DetourMapWithDefaults = ( describe("DetourMap", () => { test("when `originalShape` is clicked, fires `onClickOriginalShape`", async () => { const onClickOriginalShape = jest.fn() - const shapePoint = { lat: 0, lon: 0 } const { container } = render( - + ) fireEvent.click( container.querySelector(".c-detour_map--original-route-shape")! ) - expect(onClickOriginalShape).toHaveBeenNthCalledWith(1, shapePoint) + expect(onClickOriginalShape).toHaveBeenCalled() }) test("`originalShape` has unstarted class before being clicked", async () => { diff --git a/assets/tests/util/math.test.ts b/assets/tests/util/math.test.ts index 6e6ddd234..e519f60ca 100644 --- a/assets/tests/util/math.test.ts +++ b/assets/tests/util/math.test.ts @@ -1,6 +1,5 @@ import { describe, test, expect } from "@jest/globals" -import { clamp, closestPosition } from "../../src/util/math" -import { LatLngLiteral, latLng } from "leaflet" +import { clamp } from "../../src/util/math" describe("clamp", () => { test("when value is below minimum, returns minimum value", () => { @@ -15,32 +14,3 @@ describe("clamp", () => { expect(clamp(5, 0, 10)).toBe(5) }) }) - -describe("closestPosition", () => { - test("returns `undefined` if provided a empty list", () => { - expect(closestPosition([], latLng(0, 0))).toBeUndefined() - }) - - test("returns the closest point from the provided list", () => { - const point = latLng(0, 0) - - const closestPoint: LatLngLiteral = { - lat: point.lat + 1, - lng: 0, - } - const index = 3 - - // Generate range and offset so all are farther than `closestPoint` - const positions: LatLngLiteral[] = Array(5).map((_, idx) => ({ - lat: point.lat + closestPoint.lat + idx, - lng: 0, - })) - - positions[index] = closestPoint - - expect(closestPosition(positions, point)).toMatchObject({ - index, - position: closestPoint, - }) - }) -})