Skip to content

Commit eadd86c

Browse files
ratrunkarussell
authored andcommitted
Hide paths while pressing the 'h' key, fixes #428
1 parent d8fc31a commit eadd86c

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

src/layers/UsePathsLayer.tsx

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { Feature, Map } from 'ol'
22
import { Path } from '@/api/graphhopper'
3-
import { FeatureCollection } from 'geojson'
4-
import { useEffect } from 'react'
3+
import { useEffect, useRef, useState } from 'react'
54
import VectorLayer from 'ol/layer/Vector'
65
import VectorSource from 'ol/source/Vector'
7-
import { GeoJSON } from 'ol/format'
86
import { Stroke, Style } from 'ol/style'
97
import { fromLonLat } from 'ol/proj'
108
import { Select } from 'ol/interaction'
@@ -21,18 +19,43 @@ const selectedPathLayerKey = 'selectedPathLayer'
2119
const accessNetworkLayerKey = 'accessNetworkLayer'
2220

2321
export default function usePathsLayer(map: Map, paths: Path[], selectedPath: Path, queryPoints: QueryPoint[]) {
22+
const [showPaths, setShowPaths] = useState(true)
23+
2424
useEffect(() => {
2525
removeCurrentPathLayers(map)
26-
addUnselectedPathsLayer(
27-
map,
28-
paths.filter(p => p != selectedPath),
29-
)
30-
addSelectedPathsLayer(map, selectedPath)
31-
addAccessNetworkLayer(map, selectedPath, queryPoints)
26+
if (showPaths) {
27+
addUnselectedPathsLayer(
28+
map,
29+
paths.filter(p => p != selectedPath),
30+
)
31+
addSelectedPathsLayer(map, selectedPath)
32+
addAccessNetworkLayer(map, selectedPath, queryPoints)
33+
}
3234
return () => {
3335
removeCurrentPathLayers(map)
3436
}
35-
}, [map, paths, selectedPath])
37+
}, [map, paths, selectedPath, showPaths])
38+
39+
useEffect(() => {
40+
const target = map.getTargetElement()
41+
if (!target) return
42+
const handleKeyDown = (e: KeyboardEvent) => {
43+
if (e.key === 'h') setShowPaths(false)
44+
}
45+
46+
const handleKeyUp = (e: KeyboardEvent) => {
47+
if (e.key === 'h') setShowPaths(true)
48+
}
49+
50+
target.tabIndex = 0
51+
52+
target.addEventListener('keydown', handleKeyDown)
53+
target.addEventListener('keyup', handleKeyUp)
54+
return () => {
55+
target.removeEventListener('keydown', handleKeyDown)
56+
target.removeEventListener('keyup', handleKeyUp)
57+
}
58+
}, []) // run only once when component is initialized
3659
}
3760

3861
function removeCurrentPathLayers(map: Map) {

0 commit comments

Comments
 (0)