-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
203 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import MapboxDraw from '@mapbox/mapbox-gl-draw'; | ||
import { useControl } from 'react-map-gl'; | ||
|
||
// import type { MapRef } from 'react-map-gl'; | ||
|
||
type DrawControlProps = ConstructorParameters<typeof MapboxDraw>[0] & { | ||
onCreate?: (evt: { features: object[] }) => void; | ||
onUpdate?: (evt: { features: object[]; action: string }) => void; | ||
onDelete?: (evt: { features: object[] }) => void; | ||
onSelectionChange?: (evt: { selectedFeatures: object[] }) => void; | ||
}; | ||
|
||
export default function DrawControl(props: DrawControlProps) { | ||
useControl<MapboxDraw>( | ||
() => new MapboxDraw(props), | ||
({ map }: { map: any }) => { | ||
map.on('draw.create', props.onCreate); | ||
map.on('draw.update', props.onUpdate); | ||
map.on('draw.delete', props.onDelete); | ||
map.on('draw.selectionchange', props.onSelectionChange); | ||
}, | ||
({ map }: { map: any }) => { | ||
map.off('draw.create', props.onCreate); | ||
map.off('draw.update', props.onUpdate); | ||
map.off('draw.delete', props.onDelete); | ||
map.off('draw.selectionchange', props.onSelectionChange); | ||
}, | ||
{ | ||
position: 'top-left' | ||
} | ||
); | ||
|
||
return null; | ||
} |
51 changes: 51 additions & 0 deletions
51
app/scripts/components/common/map/controls/hooks/use-aois.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { useAtom } from "jotai"; | ||
import { useCallback } from "react"; | ||
import { aoiFeaturesAtom } from "$components/exploration/atoms/atoms"; | ||
|
||
export default function useAois() { | ||
const [features, setFeatures] = useAtom(aoiFeaturesAtom); | ||
|
||
const onUpdate = useCallback( | ||
(e) => { | ||
let newFeatures = [...features]; | ||
e.features.forEach((f) => { | ||
const existingFeature = newFeatures.find( | ||
(feature) => feature.id === f.id | ||
); | ||
if (existingFeature) { | ||
existingFeature.geometry = f.geometry; | ||
} else { | ||
newFeatures = [...newFeatures, f]; | ||
} | ||
}); | ||
setFeatures(newFeatures); | ||
}, | ||
[features, setFeatures] | ||
); | ||
|
||
const onDelete = useCallback( | ||
(e) => { | ||
let newFeatures = [...features]; | ||
e.features.forEach((f) => { | ||
newFeatures = newFeatures.filter((feature) => feature.id !== f.id); | ||
}); | ||
setFeatures(newFeatures); | ||
}, | ||
[features, setFeatures] | ||
); | ||
|
||
const onSelectionChange = useCallback( | ||
(e) => { | ||
const newFeatures = features.map((feature) => { | ||
if (e.features.find((f) => f.id === feature.id)) { | ||
return { ...feature, selected: true }; | ||
} else { | ||
return { ...feature, selected: false }; | ||
} | ||
}); | ||
setFeatures(newFeatures); | ||
}, | ||
[features, setFeatures] | ||
); | ||
return { features, onUpdate, onDelete, onSelectionChange }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters