parent
47b2aeb48b
commit
ed1f4bc47c
@ -0,0 +1,24 @@ |
||||
import leaflet from 'leaflet'; |
||||
import { type Ref, useEffect } from 'preact/hooks'; |
||||
|
||||
export const useLayers = ( |
||||
mapRef: Readonly<Ref<leaflet.Map | undefined>>, |
||||
layers: leaflet.Layer[], |
||||
) => |
||||
useEffect(() => { |
||||
const map = mapRef.current; |
||||
if (!map) { |
||||
return; |
||||
} |
||||
|
||||
for (const layer of layers) { |
||||
layer.addTo(map); |
||||
} |
||||
|
||||
return () => { |
||||
for (const layer of layers) { |
||||
layer.removeFrom(map); |
||||
layer.remove(); |
||||
} |
||||
}; |
||||
}, [mapRef, layers]); |
@ -1,6 +1,6 @@ |
||||
import leaflet from 'leaflet'; |
||||
import { type Ref, useEffect } from 'preact/hooks'; |
||||
import type { Coordinates } from '../../../shared/types'; |
||||
import type { Coordinates } from '../../shared/types'; |
||||
|
||||
export const useMapClick = ( |
||||
mapRef: Readonly<Ref<leaflet.Map | undefined>>, |
@ -1,10 +1,9 @@ |
||||
import { type Ref, useEffect } from 'preact/hooks'; |
||||
import Sortable from 'sortablejs'; |
||||
import type { ReorderListParams } from '../../types'; |
||||
|
||||
export const useSortable = ( |
||||
listContainerRef: Readonly<Ref<HTMLElement>>, |
||||
onListReorder: (params: ReorderListParams) => void, |
||||
onListReorder: (params: { oldIndex: number; newIndex: number }) => void, |
||||
) => |
||||
useEffect(() => { |
||||
if (!listContainerRef.current) { |
@ -1,28 +1,13 @@ |
||||
import leaflet from 'leaflet'; |
||||
import { type Ref, useEffect } from 'preact/hooks'; |
||||
import { type Ref, useMemo } from 'preact/hooks'; |
||||
import { useLayers } from '../../../hooks/leaflet/useLayers'; |
||||
import type { Marker } from '../../types'; |
||||
import { getLayersForMarkers } from './getLayersForMarkers'; |
||||
|
||||
export const useMarkersOnMap = ( |
||||
mapRef: Readonly<Ref<leaflet.Map | undefined>>, |
||||
markers: Marker[], |
||||
) => |
||||
useEffect(() => { |
||||
const map = mapRef.current; |
||||
if (!map) { |
||||
return; |
||||
} |
||||
|
||||
const layers = getLayersForMarkers(markers); |
||||
|
||||
for (const layer of layers) { |
||||
layer.addTo(map); |
||||
} |
||||
|
||||
return () => { |
||||
for (const layer of layers) { |
||||
layer.removeFrom(map); |
||||
layer.remove(); |
||||
} |
||||
}; |
||||
}, [mapRef, markers]); |
||||
) => { |
||||
const layers = useMemo(() => getLayersForMarkers(markers), [markers]); |
||||
useLayers(mapRef, layers); |
||||
}; |
||||
|
Loading…
Reference in new issue