(mapInstance: Mapbox)
| 32 | } & Omit<MapInstance, (typeof skipMethods)[number]>; |
| 33 | |
| 34 | export default function createRef(mapInstance: Mapbox): MapRef | null { |
| 35 | if (!mapInstance) { |
| 36 | return null; |
| 37 | } |
| 38 | |
| 39 | const map = mapInstance.map; |
| 40 | const ref: any = { |
| 41 | getMap: () => map, |
| 42 | |
| 43 | // Overwrite getters to use our shadow transform |
| 44 | getCenter: () => mapInstance.transform.center, |
| 45 | getZoom: () => mapInstance.transform.zoom, |
| 46 | getBearing: () => mapInstance.transform.bearing, |
| 47 | getPitch: () => mapInstance.transform.pitch, |
| 48 | getPadding: () => mapInstance.transform.padding, |
| 49 | getBounds: () => mapInstance.transform.getBounds(), |
| 50 | project: (lnglat: LngLatLike) => { |
| 51 | const tr = map.transform; |
| 52 | map.transform = mapInstance.transform; |
| 53 | const result = map.project(lnglat); |
| 54 | map.transform = tr; |
| 55 | return result; |
| 56 | }, |
| 57 | unproject: (point: PointLike) => { |
| 58 | const tr = map.transform; |
| 59 | map.transform = mapInstance.transform; |
| 60 | const result = map.unproject(point); |
| 61 | map.transform = tr; |
| 62 | return result; |
| 63 | }, |
| 64 | // options diverge between mapbox and maplibre |
| 65 | queryTerrainElevation: (lnglat: LngLatLike, options?: any) => { |
| 66 | const tr = map.transform; |
| 67 | map.transform = mapInstance.transform; |
| 68 | const result = map.queryTerrainElevation(lnglat, options); |
| 69 | map.transform = tr; |
| 70 | return result; |
| 71 | }, |
| 72 | queryRenderedFeatures: (geometry?: any, options?: any) => { |
| 73 | const tr = map.transform; |
| 74 | map.transform = mapInstance.transform; |
| 75 | const result = map.queryRenderedFeatures(geometry, options); |
| 76 | map.transform = tr; |
| 77 | return result; |
| 78 | } |
| 79 | }; |
| 80 | |
| 81 | for (const key of getMethodNames(map)) { |
| 82 | // @ts-expect-error |
| 83 | if (!(key in ref) && !skipMethods.includes(key)) { |
| 84 | ref[key] = map[key].bind(map); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return ref; |
| 89 | } |
| 90 | |
| 91 | function getMethodNames(obj: Object) { |
no test coverage detected
searching dependent graphs…