|
| 1 | +import { useCallback, useEffect, useState } from "react" |
| 2 | +import { Source, Layer, useMap } from "react-map-gl/maplibre" |
| 3 | +import turfLength from "@turf/length" |
| 4 | +import { |
| 5 | + point as turfPoint, |
| 6 | + lineString as turfLineString, |
| 7 | + featureCollection as turfFeatureCollection, |
| 8 | +} from "@turf/helpers" |
| 9 | + |
| 10 | +import useCssCustomProperties from "../../js/use-css-custom-properties" |
| 11 | +import { generateMapLibreId } from "../../js/maplibre" |
| 12 | + |
| 13 | +export default function MaplibreRuler({ onLengthUpdate }) { |
| 14 | + const { map } = useMap() |
| 15 | + const [geojson, setGeojson] = useState(turfFeatureCollection([])) |
| 16 | + |
| 17 | + const cssCustomProps = useCssCustomProperties([ |
| 18 | + `--color-black`, |
| 19 | + `--color-white`, |
| 20 | + ]) |
| 21 | + |
| 22 | + const mapClick = useCallback( |
| 23 | + (e) => { |
| 24 | + const features = map.queryRenderedFeatures(e.point, { |
| 25 | + layers: [`fu-ruler-points`], |
| 26 | + }) |
| 27 | + |
| 28 | + const newGeojson = structuredClone(geojson) |
| 29 | + |
| 30 | + // remove the line as it's always the last feature |
| 31 | + if (newGeojson.features.length > 1) newGeojson.features.pop() |
| 32 | + |
| 33 | + if (features.length) { |
| 34 | + // if a point feature was clicked, remove it |
| 35 | + newGeojson.features = newGeojson.features.filter( |
| 36 | + (p) => p.id != features[0].id |
| 37 | + ) |
| 38 | + } else { |
| 39 | + // add it otherwise |
| 40 | + newGeojson.features.push( |
| 41 | + turfPoint([e.lngLat.lng, e.lngLat.lat], null, { |
| 42 | + id: generateMapLibreId(), |
| 43 | + }) |
| 44 | + ) |
| 45 | + } |
| 46 | + |
| 47 | + if (newGeojson.features.length > 1) { |
| 48 | + const lineString = turfLineString( |
| 49 | + newGeojson.features.map((point) => point.geometry.coordinates) |
| 50 | + ) |
| 51 | + newGeojson.features.push(lineString) |
| 52 | + |
| 53 | + if (onLengthUpdate) |
| 54 | + onLengthUpdate(turfLength(lineString, { units: `kilometers` })) |
| 55 | + } else onLengthUpdate?.(0) |
| 56 | + |
| 57 | + setGeojson(newGeojson) |
| 58 | + }, |
| 59 | + [geojson, map, onLengthUpdate] |
| 60 | + ) |
| 61 | + |
| 62 | + const mapMouseMove = useCallback( |
| 63 | + (e) => { |
| 64 | + // TODO: implement projection |
| 65 | + const features = map.queryRenderedFeatures(e.point, { |
| 66 | + layers: [`fu-ruler-points`], |
| 67 | + }) |
| 68 | + |
| 69 | + map.getCanvas().style.cursor = features.length ? `pointer` : `crosshair` |
| 70 | + }, |
| 71 | + [map] |
| 72 | + ) |
| 73 | + |
| 74 | + useEffect(() => { |
| 75 | + map.getMap().doubleClickZoom.disable() // fix: const { enableMapInteractions, disableMapInteractions } = useToggleMapInteractions([`doubleClickZoom`]) |
| 76 | + map.on(`click`, mapClick) |
| 77 | + map.on(`mousemove`, mapMouseMove) |
| 78 | + |
| 79 | + return () => { |
| 80 | + map.getMap().doubleClickZoom.enable() // fix |
| 81 | + map.off(`click`, mapClick) |
| 82 | + map.off(`mousemove`, mapMouseMove) |
| 83 | + map.getCanvas().style.cursor = null |
| 84 | + } |
| 85 | + }, [map, mapClick, mapMouseMove]) |
| 86 | + |
| 87 | + return ( |
| 88 | + <> |
| 89 | + <Source id="fu-ruler" type="geojson" data={geojson} /> |
| 90 | + |
| 91 | + <Layer |
| 92 | + source="fu-ruler" |
| 93 | + id="fu-ruler-points" |
| 94 | + type="circle" |
| 95 | + paint={{ |
| 96 | + "circle-radius": 6, |
| 97 | + "circle-color": cssCustomProps[`--color-black`], |
| 98 | + }} |
| 99 | + filter={[`in`, `$type`, `Point`]} |
| 100 | + /> |
| 101 | + |
| 102 | + <Layer |
| 103 | + source="fu-ruler" |
| 104 | + id="fu-ruler-lines" |
| 105 | + type="line" |
| 106 | + layout={{ |
| 107 | + "line-cap": `round`, |
| 108 | + "line-join": `round`, |
| 109 | + }} |
| 110 | + paint={{ |
| 111 | + "line-width": 3, |
| 112 | + "line-color": cssCustomProps[`--color-black`], |
| 113 | + }} |
| 114 | + filter={[`in`, `$type`, `LineString`]} |
| 115 | + /> |
| 116 | + </> |
| 117 | + ) |
| 118 | +} |
0 commit comments