38 lines
904 B
TypeScript
38 lines
904 B
TypeScript
import maplibregl from "maplibre-gl";
|
|
import { Geometry } from "@/lib/useEditorState";
|
|
|
|
type ModeGetter = () => "idle" | "draw" | "select" | "add-point";
|
|
|
|
export function initPoint(
|
|
map: maplibregl.Map,
|
|
getMode: ModeGetter,
|
|
onComplete: (geometry: Geometry) => void
|
|
) {
|
|
/**
|
|
* Add a new point when in add-point mode.
|
|
*/
|
|
function onClick(e: maplibregl.MapLayerMouseEvent) {
|
|
if (getMode() !== "add-point") return;
|
|
|
|
const geometry = {
|
|
type: "Point",
|
|
coordinates: [e.lngLat.lng, e.lngLat.lat],
|
|
};
|
|
|
|
onComplete?.(geometry);
|
|
}
|
|
|
|
function onMove() {
|
|
if (getMode() !== "add-point") return;
|
|
map.getCanvas().style.cursor = "crosshair";
|
|
}
|
|
|
|
map.on("click", onClick);
|
|
map.on("mousemove", onMove);
|
|
|
|
return () => {
|
|
map.off("click", onClick);
|
|
map.off("mousemove", onMove);
|
|
};
|
|
}
|