fix: refit Mol map after responsive resize
This commit is contained in:
@@ -42,6 +42,8 @@ def test_map_rectangle_drag_is_wired_to_automatic_analysis() -> None:
|
|||||||
assert "onMapBboxSelectRef.current?.(bbox)" in geomap
|
assert "onMapBboxSelectRef.current?.(bbox)" in geomap
|
||||||
assert "new ResizeObserver" in geomap
|
assert "new ResizeObserver" in geomap
|
||||||
assert "resizeObserver.observe(containerRef.current)" in geomap
|
assert "resizeObserver.observe(containerRef.current)" in geomap
|
||||||
|
assert "fitDataOnChangeRef.current" in geomap
|
||||||
|
assert "map.fitBounds(bounds, { padding: 40, duration: 0 })" in geomap
|
||||||
assert "resizeObserver.disconnect()" in geomap
|
assert "resizeObserver.disconnect()" in geomap
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -128,9 +128,16 @@ function GeoMap({
|
|||||||
const onMapBboxSelectRef = useRef<GeoMapProps['onMapBboxSelect']>(onMapBboxSelect)
|
const onMapBboxSelectRef = useRef<GeoMapProps['onMapBboxSelect']>(onMapBboxSelect)
|
||||||
const onViewportChangeRef = useRef<GeoMapProps['onViewportChange']>(onViewportChange)
|
const onViewportChangeRef = useRef<GeoMapProps['onViewportChange']>(onViewportChange)
|
||||||
const bboxSelectionModeRef = useRef(bboxSelectionMode)
|
const bboxSelectionModeRef = useRef(bboxSelectionMode)
|
||||||
|
const areaDataRef = useRef<GeoJSON.FeatureCollection | null>(areaData)
|
||||||
|
const dataRef = useRef<GeoJSON.FeatureCollection | null>(data)
|
||||||
|
const fitDataOnChangeRef = useRef(fitDataOnChange)
|
||||||
const lastFittedAreaRef = useRef<GeoJSON.FeatureCollection | null>(null)
|
const lastFittedAreaRef = useRef<GeoJSON.FeatureCollection | null>(null)
|
||||||
const [mapStyleReady, setMapStyleReady] = useState(false)
|
const [mapStyleReady, setMapStyleReady] = useState(false)
|
||||||
|
|
||||||
|
areaDataRef.current = areaData
|
||||||
|
dataRef.current = data
|
||||||
|
fitDataOnChangeRef.current = fitDataOnChange
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
onFeatureSelectRef.current = onFeatureSelect
|
onFeatureSelectRef.current = onFeatureSelect
|
||||||
}, [onFeatureSelect])
|
}, [onFeatureSelect])
|
||||||
@@ -177,6 +184,13 @@ function GeoMap({
|
|||||||
})
|
})
|
||||||
const resizeObserver = new ResizeObserver(() => {
|
const resizeObserver = new ResizeObserver(() => {
|
||||||
map.resize()
|
map.resize()
|
||||||
|
const fitCollection = fitDataOnChangeRef.current
|
||||||
|
? mergeFeatureCollections([areaDataRef.current, dataRef.current])
|
||||||
|
: areaDataRef.current
|
||||||
|
const bounds = fitCollection ? collectCoordinates(fitCollection) : null
|
||||||
|
if (bounds && map.isStyleLoaded()) {
|
||||||
|
map.fitBounds(bounds, { padding: 40, duration: 0 })
|
||||||
|
}
|
||||||
})
|
})
|
||||||
resizeObserver.observe(containerRef.current)
|
resizeObserver.observe(containerRef.current)
|
||||||
map.addControl(new maplibregl.NavigationControl(), 'top-right')
|
map.addControl(new maplibregl.NavigationControl(), 'top-right')
|
||||||
|
|||||||
Reference in New Issue
Block a user