mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 17:41:51 +00:00
4b7c96f4c5
Use street-level zoom in both renderers while keeping the selected pin between the map panels. Lift the selected Leaflet marker out of a dense group when needed, and keep a late route fit from overriding the selection. Closes #3306
106 lines
3.9 KiB
TypeScript
106 lines
3.9 KiB
TypeScript
import type L from 'leaflet';
|
|
import { useEffect, useRef } from 'react';
|
|
import { useMap } from 'react-leaflet';
|
|
import type { Place } from '../../types';
|
|
import { frameLng } from '../../utils/lngFrame';
|
|
import type { ViewportPadding } from '../../utils/mapViewport';
|
|
import { useLngFrame } from './lngFrameContext';
|
|
import { isUnderChrome } from './mapChromePadding';
|
|
import { PLACE_FOCUS_ZOOM, selectedPlaceTarget } from './selectedPlaceTarget';
|
|
|
|
interface SelectionControllerProps {
|
|
/** False while the traveller locked the map (#2010): a pick changes nothing on it. */
|
|
follow: boolean;
|
|
places: Place[];
|
|
selectedPlaceId: number | null;
|
|
dayPlaces: Place[];
|
|
selectedPlace: Place | null;
|
|
paddingBox: ViewportPadding;
|
|
revealSelectedPlace: () => void;
|
|
}
|
|
|
|
/**
|
|
* Where the Leaflet map goes when a place is selected: there, and out from under the
|
|
* panels. The GL twin is `useSelectionCameraGL`.
|
|
*/
|
|
export function SelectionController({
|
|
follow,
|
|
places,
|
|
selectedPlaceId,
|
|
dayPlaces,
|
|
selectedPlace,
|
|
paddingBox,
|
|
revealSelectedPlace,
|
|
}: SelectionControllerProps) {
|
|
const map = useMap();
|
|
const frame = useLngFrame();
|
|
const prev = useRef<number | null>(null);
|
|
/** The panel geometry the last pan was measured against. */
|
|
const pannedWith = useRef(paddingBox);
|
|
const target = selectedPlaceTarget(selectedPlaceId, places, dayPlaces, selectedPlace);
|
|
const lat = target?.lat ?? null;
|
|
const lng = target?.lng == null ? null : frameLng(frame, target.lng);
|
|
|
|
useEffect(() => {
|
|
if (!follow || selectedPlaceId == null) return;
|
|
let frame: number | null = null;
|
|
const revealAfterMove = () => {
|
|
if (frame !== null) cancelAnimationFrame(frame);
|
|
frame = requestAnimationFrame(() => revealSelectedPlace());
|
|
};
|
|
map.on('moveend', revealAfterMove);
|
|
return () => {
|
|
map.off('moveend', revealAfterMove);
|
|
if (frame !== null) cancelAnimationFrame(frame);
|
|
};
|
|
}, [map, follow, selectedPlaceId, revealSelectedPlace]);
|
|
|
|
useEffect(() => {
|
|
const isNew = selectedPlaceId !== prev.current;
|
|
prev.current = selectedPlaceId;
|
|
if (!isNew || !follow || !selectedPlaceId || lat == null || lng == null) return;
|
|
pannedWith.current = paddingBox;
|
|
const zoom = map.getZoom();
|
|
const focusZoom = Math.max(zoom, PLACE_FOCUS_ZOOM);
|
|
const center = paddedCenter(map, [lat, lng], paddingBox, focusZoom);
|
|
if (zoom < focusZoom) {
|
|
map.setView(center, focusZoom, { animate: false });
|
|
} else {
|
|
map.panTo(center, { animate: true });
|
|
}
|
|
}, [selectedPlaceId, follow, lat, lng, map, paddingBox]);
|
|
|
|
// The place card only learns its height after it laid out, and grows again when its
|
|
// details arrive. If it grew over the pin the selection placed, bring the pin back out;
|
|
// a pin still in the open part of the map stays where the traveller left it (#2991).
|
|
useEffect(() => {
|
|
if (pannedWith.current === paddingBox) return;
|
|
pannedWith.current = paddingBox;
|
|
if (!follow || !selectedPlaceId || lat == null || lng == null) return;
|
|
if (typeof map.latLngToContainerPoint !== 'function') return;
|
|
if (isUnderChrome(map.latLngToContainerPoint([lat, lng]), map.getSize(), paddingBox)) {
|
|
map.panTo(paddedCenter(map, [lat, lng], paddingBox, map.getZoom()), { animate: true });
|
|
}
|
|
}, [paddingBox, follow, selectedPlaceId, lat, lng, map]);
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Offset the centre at the destination zoom so the pin lands in the middle of the
|
|
* visible map area, above the bottom card and between the side panels.
|
|
*/
|
|
function paddedCenter(
|
|
map: L.Map,
|
|
latlng: [number, number],
|
|
padding: ViewportPadding,
|
|
zoom: number
|
|
): L.LatLngExpression {
|
|
if (typeof map.project !== 'function' || typeof map.unproject !== 'function') {
|
|
return latlng;
|
|
}
|
|
const { top, right, bottom, left } = padding;
|
|
const point = map.project(latlng, zoom).add([(right - left) / 2, (bottom - top) / 2]);
|
|
return map.unproject(point, zoom);
|
|
}
|