-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #465 from terrestris/ol-hooks
feat: add some ol related hooks
- Loading branch information
Showing
8 changed files
with
457 additions
and
4 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import OlMap from 'ol/Map'; | ||
import React from 'react'; | ||
|
||
const MapContext = React.createContext<OlMap|null>(null); | ||
export default MapContext; |
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,13 @@ | ||
import { | ||
useContext | ||
} from 'react'; | ||
|
||
import OlMap from 'ol/Map'; | ||
|
||
import MapContext from '../contexts/MapContext'; | ||
|
||
export const useMap = (): (OlMap | undefined) => { | ||
return useContext(MapContext); | ||
}; | ||
|
||
export default useMap; |
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,60 @@ | ||
import { | ||
DependencyList, | ||
useEffect, | ||
useState | ||
} from 'react'; | ||
|
||
import { | ||
isNil | ||
} from 'lodash'; | ||
|
||
import { | ||
Interaction | ||
} from 'ol/interaction'; | ||
|
||
import useMap from './useMap'; | ||
|
||
/** | ||
* This hook adds an interaction to the map and removes/updates it if the dependency array changes. | ||
* It accepts an optional active parameter that toggles the active state of the interaction. If it is undefined the | ||
* active state will not get changed. | ||
* @param constructor returns an interaction to be added to the map, will be called again, if the interaction needs | ||
* to be updated | ||
* @param dependencies | ||
* @param active | ||
*/ | ||
export const useOlInteraction = <InteractionType extends Interaction> ( | ||
constructor: () => InteractionType|undefined, | ||
dependencies: DependencyList, | ||
active?: undefined|boolean | ||
): InteractionType|undefined => { | ||
const map = useMap(); | ||
const [interaction, setInteraction] = useState<InteractionType>(); | ||
useEffect(() => { | ||
if (!map) { | ||
return undefined; | ||
} | ||
|
||
const newInteraction = constructor(); | ||
if (!newInteraction) { | ||
return undefined; | ||
} | ||
|
||
setInteraction(newInteraction); | ||
map.addInteraction(newInteraction); | ||
|
||
return () => { | ||
map.removeInteraction(newInteraction); | ||
setInteraction(undefined); | ||
}; | ||
}, [...dependencies, map]); | ||
|
||
useEffect(() => { | ||
if (!interaction || isNil(active)) { | ||
return; | ||
} | ||
interaction.setActive(active); | ||
}, [interaction, active]); | ||
|
||
return interaction; | ||
}; |
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,58 @@ | ||
import { | ||
DependencyList, | ||
useEffect, | ||
useState | ||
} from 'react'; | ||
|
||
import { | ||
isNil | ||
} from 'lodash'; | ||
import BaseLayer from 'ol/layer/Base'; | ||
|
||
import useMap from './useMap'; | ||
|
||
/** | ||
* This hook adds a layer to the map and removes/updates it if the dependency array changes. | ||
* It accepts an optional visible parameter that toggles the visible state of the layer. If it is undefined the | ||
* visible state will not get changed. | ||
* @param constructor returns a layer to be added to the map, will be called again, if the layer needs | ||
* to be updated | ||
* @param dependencies | ||
* @param visible | ||
*/ | ||
export const useOlLayer = <LayerType extends BaseLayer>( | ||
constructor: () => LayerType|undefined, | ||
dependencies: DependencyList, | ||
visible?: undefined|boolean | ||
): LayerType|undefined => { | ||
const map = useMap(); | ||
const [layer, setLayer] = useState<LayerType>(); | ||
useEffect(() => { | ||
if (!map) { | ||
return undefined; | ||
} | ||
|
||
const newLayer = constructor(); | ||
|
||
if (!newLayer) { | ||
return undefined; | ||
} | ||
|
||
map.addLayer(newLayer); | ||
setLayer(newLayer); | ||
return () => { | ||
map.removeLayer(newLayer); | ||
setLayer(undefined); | ||
}; | ||
}, [map, ...dependencies]); | ||
|
||
useEffect(() => { | ||
if (!layer || isNil(visible)) { | ||
return; | ||
} | ||
|
||
layer.setVisible(visible); | ||
}, [layer, visible]); | ||
|
||
return layer; | ||
}; |
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,40 @@ | ||
import { | ||
DependencyList, | ||
useEffect | ||
} from 'react'; | ||
|
||
import { | ||
Observable | ||
} from 'ol'; | ||
import { | ||
EventsKey | ||
} from 'ol/events'; | ||
import { | ||
unByKey | ||
} from 'ol/Observable'; | ||
|
||
/** | ||
* This hook unregisters listeners if the dependency array changes | ||
*/ | ||
export const useOlListener = <ObservableType extends Observable>( | ||
observable: ObservableType|ObservableType[]|undefined, | ||
observe: (o: ObservableType) => EventsKey|EventsKey[], | ||
dependencies: DependencyList, | ||
active?: boolean | ||
): void => { | ||
useEffect(() => { | ||
if (!observable || active === false) { | ||
return undefined; | ||
} | ||
const observables = Array.isArray(observable) ? observable : [observable]; | ||
const keys: EventsKey[] = observables.flatMap(o => { | ||
const k = observe(o); | ||
return Array.isArray(k) ? k : [k]; | ||
}); | ||
return () => { | ||
for (const key of keys) { | ||
unByKey(key); | ||
} | ||
}; | ||
}, [observable, active, ...dependencies]); | ||
}; |
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,33 @@ | ||
import { | ||
useEffect, | ||
useRef | ||
} from 'react'; | ||
|
||
import OlInteractionLink from 'ol/interaction/Link'; | ||
|
||
import OlMap from 'ol/Map'; | ||
|
||
export const usePermalink = (map: OlMap|undefined) => { | ||
const linkInteractionRef = useRef<OlInteractionLink>(); | ||
|
||
useEffect(() => { | ||
if (!map) { | ||
return undefined; | ||
} | ||
|
||
if (!linkInteractionRef.current) { | ||
linkInteractionRef.current = new OlInteractionLink({ | ||
params: ['x', 'y', 'z', 'r'] | ||
}); | ||
map.addInteraction(linkInteractionRef.current); | ||
} | ||
|
||
return () => { | ||
if (linkInteractionRef.current) { | ||
map.removeInteraction(linkInteractionRef.current); | ||
} | ||
}; | ||
}, [map]); | ||
}; | ||
|
||
export default usePermalink; |