({
isDrag = true,
bounds,
drawBounds,
onChange,
onDrawChange,
}: {
isDrag: boolean;
bounds: LatLngBounds | null;
drawBounds: LatLngBounds | null;
onChange: (bounds: LatLngBounds) => void;
onDrawChange: (bounds: LatLngBounds) => void;
})
| 16 | }); |
| 17 | |
| 18 | function RectangleSelector({ |
| 19 | isDrag = true, |
| 20 | |
| 21 | bounds, |
| 22 | drawBounds, |
| 23 | |
| 24 | onChange, |
| 25 | onDrawChange, |
| 26 | }: { |
| 27 | isDrag: boolean; |
| 28 | |
| 29 | bounds: LatLngBounds | null; |
| 30 | drawBounds: LatLngBounds | null; |
| 31 | |
| 32 | onChange: (bounds: LatLngBounds) => void; |
| 33 | onDrawChange: (bounds: LatLngBounds) => void; |
| 34 | }) { |
| 35 | const [firstPoint, setFirstPoint] = useState<LatLng | null>(null); |
| 36 | |
| 37 | const lastLatlngRef = useRef<LatLng | null>(null); |
| 38 | |
| 39 | const adjustLng = (latlng: LatLng): LatLng => { |
| 40 | const adjustedLng = ((((latlng.lng + 180) % 360) + 360) % 360) - 180; |
| 41 | return new L.LatLng(latlng.lat, adjustedLng); |
| 42 | }; |
| 43 | |
| 44 | const map = useMapEvents({ |
| 45 | mousedown(e) { |
| 46 | if (!isDrag) { |
| 47 | setFirstPoint(e.latlng); |
| 48 | } |
| 49 | }, |
| 50 | mousemove(e) { |
| 51 | if (firstPoint) { |
| 52 | lastLatlngRef.current = adjustLng(e.latlng); |
| 53 | onDrawChange(new L.LatLngBounds(firstPoint, e.latlng)); |
| 54 | onChange( |
| 55 | new L.LatLngBounds(adjustLng(firstPoint), adjustLng(e.latlng)) |
| 56 | ); |
| 57 | } |
| 58 | }, |
| 59 | mouseup(e) { |
| 60 | if (firstPoint) { |
| 61 | onDrawChange(new L.LatLngBounds(firstPoint, e.latlng)); |
| 62 | onChange( |
| 63 | new L.LatLngBounds(adjustLng(firstPoint), adjustLng(e.latlng)) |
| 64 | ); |
| 65 | setFirstPoint(null); |
| 66 | } |
| 67 | }, |
| 68 | }); |
| 69 | |
| 70 | useEffect(() => { |
| 71 | const container = map.getContainer(); |
| 72 | const handleTouchStart = (e: TouchEvent) => { |
| 73 | if (!isDrag && e.touches.length > 0) { |
| 74 | const touch = e.touches[0]; |
| 75 | const latlng = map.mouseEventToLatLng(touch as any); |
nothing calls this directly
no outgoing calls
no test coverage detected