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 leaflet from 'leaflet'; |
||||||
import { type Ref, useEffect } from 'preact/hooks'; |
import { type Ref, useEffect } from 'preact/hooks'; |
||||||
import type { Coordinates } from '../../../shared/types'; |
import type { Coordinates } from '../../shared/types'; |
||||||
|
|
||||||
export const useMapClick = ( |
export const useMapClick = ( |
||||||
mapRef: Readonly<Ref<leaflet.Map | undefined>>, |
mapRef: Readonly<Ref<leaflet.Map | undefined>>, |
@ -1,10 +1,9 @@ |
|||||||
import { type Ref, useEffect } from 'preact/hooks'; |
import { type Ref, useEffect } from 'preact/hooks'; |
||||||
import Sortable from 'sortablejs'; |
import Sortable from 'sortablejs'; |
||||||
import type { ReorderListParams } from '../../types'; |
|
||||||
|
|
||||||
export const useSortable = ( |
export const useSortable = ( |
||||||
listContainerRef: Readonly<Ref<HTMLElement>>, |
listContainerRef: Readonly<Ref<HTMLElement>>, |
||||||
onListReorder: (params: ReorderListParams) => void, |
onListReorder: (params: { oldIndex: number; newIndex: number }) => void, |
||||||
) => |
) => |
||||||
useEffect(() => { |
useEffect(() => { |
||||||
if (!listContainerRef.current) { |
if (!listContainerRef.current) { |
@ -1,28 +1,13 @@ |
|||||||
import leaflet from 'leaflet'; |
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 type { Marker } from '../../types'; |
||||||
import { getLayersForMarkers } from './getLayersForMarkers'; |
import { getLayersForMarkers } from './getLayersForMarkers'; |
||||||
|
|
||||||
export const useMarkersOnMap = ( |
export const useMarkersOnMap = ( |
||||||
mapRef: Readonly<Ref<leaflet.Map | undefined>>, |
mapRef: Readonly<Ref<leaflet.Map | undefined>>, |
||||||
markers: Marker[], |
markers: Marker[], |
||||||
) => |
) => { |
||||||
useEffect(() => { |
const layers = useMemo(() => getLayersForMarkers(markers), [markers]); |
||||||
const map = mapRef.current; |
useLayers(mapRef, layers); |
||||||
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]); |
|
||||||
|
Loading…
Reference in new issue