|
| 1 | +/* eslint-disable no-undef */ |
| 2 | + |
| 3 | +import { |
| 4 | + forwardRef, |
| 5 | + useContext, |
| 6 | + useEffect, |
| 7 | + useImperativeHandle, |
| 8 | + useMemo, |
| 9 | + useRef, |
| 10 | +} from "react"; |
| 11 | + |
| 12 | +import { GoogleMapsContext, useMapsLibrary } from "@vis.gl/react-google-maps"; |
| 13 | + |
| 14 | +import type { Ref } from "react"; |
| 15 | + |
| 16 | +type PolylineEventProps = { |
| 17 | + onClick?: (e: google.maps.MapMouseEvent) => void; |
| 18 | + onDrag?: (e: google.maps.MapMouseEvent) => void; |
| 19 | + onDragStart?: (e: google.maps.MapMouseEvent) => void; |
| 20 | + onDragEnd?: (e: google.maps.MapMouseEvent) => void; |
| 21 | + onMouseOver?: (e: google.maps.MapMouseEvent) => void; |
| 22 | + onMouseOut?: (e: google.maps.MapMouseEvent) => void; |
| 23 | +}; |
| 24 | + |
| 25 | +type PolylineCustomProps = { |
| 26 | + /** |
| 27 | + * this is an encoded string for the path, will be decoded and used as a path |
| 28 | + */ |
| 29 | + encodedPath?: string; |
| 30 | +}; |
| 31 | + |
| 32 | +export type PolylineProps = google.maps.PolylineOptions & |
| 33 | + PolylineEventProps & |
| 34 | + PolylineCustomProps; |
| 35 | + |
| 36 | +export type PolylineRef = Ref<google.maps.Polyline | null>; |
| 37 | + |
| 38 | +function usePolyline(props: PolylineProps) { |
| 39 | + const { |
| 40 | + onClick, |
| 41 | + onDrag, |
| 42 | + onDragStart, |
| 43 | + onDragEnd, |
| 44 | + onMouseOver, |
| 45 | + onMouseOut, |
| 46 | + encodedPath, |
| 47 | + ...polylineOptions |
| 48 | + } = props; |
| 49 | + // This is here to avoid triggering the useEffect below when the callbacks change (which happen if the user didn't memoize them) |
| 50 | + const callbacks = useRef<Record<string, (e: unknown) => void>>({}); |
| 51 | + Object.assign(callbacks.current, { |
| 52 | + onClick, |
| 53 | + onDrag, |
| 54 | + onDragStart, |
| 55 | + onDragEnd, |
| 56 | + onMouseOver, |
| 57 | + onMouseOut, |
| 58 | + }); |
| 59 | + |
| 60 | + const geometryLibrary = useMapsLibrary("geometry"); |
| 61 | + |
| 62 | + const polyline = useRef(new google.maps.Polyline()).current; |
| 63 | + // update PolylineOptions (note the dependencies aren't properly checked |
| 64 | + // here, we just assume that setOptions is smart enough to not waste a |
| 65 | + // lot of time updating values that didn't change) |
| 66 | + useMemo(() => { |
| 67 | + polyline.setOptions(polylineOptions); |
| 68 | + }, [polyline, polylineOptions]); |
| 69 | + |
| 70 | + const map = useContext(GoogleMapsContext)?.map; |
| 71 | + |
| 72 | + // update the path with the encodedPath |
| 73 | + useMemo(() => { |
| 74 | + if (!encodedPath || !geometryLibrary) return; |
| 75 | + const path = geometryLibrary.encoding.decodePath(encodedPath); |
| 76 | + polyline.setPath(path); |
| 77 | + }, [polyline, encodedPath, geometryLibrary]); |
| 78 | + |
| 79 | + // create polyline instance and add to the map once the map is available |
| 80 | + useEffect(() => { |
| 81 | + if (!map) { |
| 82 | + if (map === undefined) |
| 83 | + console.error("<Polyline> has to be inside a Map component."); |
| 84 | + |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + polyline.setMap(map); |
| 89 | + |
| 90 | + return () => { |
| 91 | + polyline.setMap(null); |
| 92 | + }; |
| 93 | + }, [map]); |
| 94 | + |
| 95 | + // attach and re-attach event-handlers when any of the properties change |
| 96 | + useEffect(() => { |
| 97 | + if (!polyline) return; |
| 98 | + |
| 99 | + // Add event listeners |
| 100 | + const gme = google.maps.event; |
| 101 | + [ |
| 102 | + ["click", "onClick"], |
| 103 | + ["drag", "onDrag"], |
| 104 | + ["dragstart", "onDragStart"], |
| 105 | + ["dragend", "onDragEnd"], |
| 106 | + ["mouseover", "onMouseOver"], |
| 107 | + ["mouseout", "onMouseOut"], |
| 108 | + ].forEach(([eventName, eventCallback]) => { |
| 109 | + gme.addListener(polyline, eventName, (e: google.maps.MapMouseEvent) => { |
| 110 | + const callback = callbacks.current[eventCallback]; |
| 111 | + if (callback) callback(e); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + return () => { |
| 116 | + gme.clearInstanceListeners(polyline); |
| 117 | + }; |
| 118 | + }, [polyline]); |
| 119 | + |
| 120 | + return polyline; |
| 121 | +} |
| 122 | + |
| 123 | +/** |
| 124 | + * Component to render a polyline on a map |
| 125 | + */ |
| 126 | +export const Polyline = forwardRef((props: PolylineProps, ref: PolylineRef) => { |
| 127 | + const polyline = usePolyline(props); |
| 128 | + |
| 129 | + useImperativeHandle(ref, () => polyline, []); |
| 130 | + |
| 131 | + return null; |
| 132 | +}); |
| 133 | + |
| 134 | +Polyline.displayName = "Polyline"; |
0 commit comments