|
1 | 1 | /* Reporters and buttons for evaluating a feature's focus on the map. */ |
2 | 2 | import { Intent, Button } from "@blueprintjs/core"; |
3 | | -import { useMapInitialized, useMapRef, useMapStatus } from "./context"; |
| 3 | +import { useMapInitialized, useMapRef } from "./context"; |
4 | 4 | import classNames from "classnames"; |
5 | 5 | import { useState, useRef, useEffect } from "react"; |
6 | 6 | import bbox from "@turf/bbox"; |
7 | 7 | import styles from "./main.module.scss"; |
8 | 8 | import hyper from "@macrostrat/hyper"; |
9 | 9 |
|
10 | | -import mapboxgl, { |
| 10 | +import type GeoJSON from "geojson"; |
| 11 | +import type { |
11 | 12 | LngLatBoundsLike, |
12 | 13 | LngLatLike, |
13 | 14 | PaddingOptions, |
14 | | - FlyToOptions, |
| 15 | + AnimationOptions, |
| 16 | + CameraOptions, |
| 17 | + Map, |
15 | 18 | } from "mapbox-gl"; |
16 | 19 |
|
| 20 | +/** |
| 21 | + * FlyToOptions |
| 22 | + * For some reason, we have to shadow the mapboxgl.FlyToOptions type |
| 23 | + * */ |
| 24 | +export interface FlyToOptions extends AnimationOptions, CameraOptions { |
| 25 | + curve?: number | undefined; |
| 26 | + minZoom?: number | undefined; |
| 27 | + speed?: number | undefined; |
| 28 | + screenSpeed?: number | undefined; |
| 29 | + maxDuration?: number | undefined; |
| 30 | +} |
| 31 | + |
17 | 32 | const h = hyper.styled(styles); |
18 | 33 |
|
19 | 34 | export enum PositionFocusState { |
@@ -69,7 +84,7 @@ export function useMapEaseToCenter(position, padding) { |
69 | 84 | ); |
70 | 85 | const map = mapRef.current; |
71 | 86 | if (map == null) return; |
72 | | - let opts: mapboxgl.FlyToOptions = null; |
| 87 | + let opts: FlyToOptions = null; |
73 | 88 | if (position != prevPosition.current) { |
74 | 89 | opts ??= {}; |
75 | 90 | opts.center = position; |
@@ -117,7 +132,7 @@ export function useMapEaseToBounds( |
117 | 132 | if (bounds == prevPosition.current || padding == prevPadding.current) { |
118 | 133 | return; |
119 | 134 | } |
120 | | - let opts: mapboxgl.FlyToOptions = { |
| 135 | + let opts: FlyToOptions = { |
121 | 136 | padding, |
122 | 137 | duration: prevPadding.current == null ? 0 : 800, |
123 | 138 | }; |
@@ -182,7 +197,7 @@ export function useMapEaseTo(props: MapEaseToProps) { |
182 | 197 |
|
183 | 198 | const positionChanges = filterChanges(state, prevState.current); |
184 | 199 |
|
185 | | - let opts: mapboxgl.FlyToOptions = { |
| 200 | + let opts: FlyToOptions = { |
186 | 201 | padding, |
187 | 202 | duration: initialized ? duration : 0, |
188 | 203 | }; |
@@ -238,11 +253,7 @@ function stripNullKeys(obj: object) { |
238 | 253 | return newObj; |
239 | 254 | } |
240 | 255 |
|
241 | | -function moveMap( |
242 | | - map: mapboxgl.Map, |
243 | | - state: MapEaseToState, |
244 | | - opts: mapboxgl.FlyToOptions, |
245 | | -) { |
| 256 | +function moveMap(map: mapboxgl.Map, state: MapEaseToState, opts: FlyToOptions) { |
246 | 257 | const { bounds, center, zoom, padding } = state; |
247 | 258 | if (bounds != null) { |
248 | 259 | map.fitBounds(bounds, opts); |
@@ -279,8 +290,8 @@ function greatCircleDistance( |
279 | 290 | } |
280 | 291 |
|
281 | 292 | export function getFocusState( |
282 | | - map: mapboxgl.Map, |
283 | | - location: mapboxgl.LngLatLike | GeoJSON.Geometry | null, |
| 293 | + map: Map, |
| 294 | + location: LngLatLike | GeoJSON.Geometry | null, |
284 | 295 | ): PositionFocusState | null { |
285 | 296 | /** Determine whether the infomarker is positioned in the viewport */ |
286 | 297 | if (location == null) return null; |
@@ -342,9 +353,7 @@ export function getFocusState( |
342 | 353 | return PositionFocusState.OUT_OF_VIEW; |
343 | 354 | } |
344 | 355 |
|
345 | | -export function useFocusState( |
346 | | - position: mapboxgl.LngLatLike | GeoJSON.Geometry, |
347 | | -) { |
| 356 | +export function useFocusState(position: LngLatLike | GeoJSON.Geometry) { |
348 | 357 | const map = useMapRef(); |
349 | 358 | const [focusState, setFocusState] = useState<PositionFocusState | null>(null); |
350 | 359 | const isInitialized = useMapInitialized(); |
@@ -373,11 +382,7 @@ export function isCentered(focusState: PositionFocusState) { |
373 | 382 | } |
374 | 383 |
|
375 | 384 | function getCenterAndBestZoom( |
376 | | - input: |
377 | | - | [number, number] |
378 | | - | GeoJSON.Geometry |
379 | | - | GeoJSON.BBox |
380 | | - | mapboxgl.LngLatLike, |
| 385 | + input: [number, number] | GeoJSON.Geometry | GeoJSON.BBox | LngLatLike, |
381 | 386 | ) { |
382 | 387 | let box: GeoJSON.BBox; |
383 | 388 | let center: [number, number] | null = null; |
|
0 commit comments