-
-
Notifications
You must be signed in to change notification settings - Fork 160
/
the-keyboard-listeners.tsx
66 lines (51 loc) · 1.9 KB
/
the-keyboard-listeners.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import React, { useEffect } from 'react'
import { useRecoilValue, useSetRecoilState } from 'recoil'
import { browsersAtom, isUrlHistoryOpenAtom } from '../atoms'
import { urlIdSelector } from '../selectors'
import { copyUrl, escapePressed, selectBrowser } from '../sendToMain'
const TheKeyboardListeners: React.FC = ({ children }) => {
const isUrlHistoryOpen = useRecoilValue(isUrlHistoryOpenAtom)
const setIsUrlHistoryOpen = useSetRecoilState(isUrlHistoryOpenAtom)
const urlId = useRecoilValue(urlIdSelector)
const browsers = useRecoilValue(browsersAtom)
useEffect(() => {
const handler = (event: KeyboardEvent) => {
// TODO need a way to turn on and off keyboard entry when the
// functionality requires it. Use an atom?
const isEscape = event.code === 'Escape'
if (isEscape) {
if (isUrlHistoryOpen) {
setIsUrlHistoryOpen(false)
} else {
escapePressed()
}
return
}
const isCopy = event.code === 'KeyC' && event.metaKey
if (isCopy) {
event.preventDefault()
copyUrl(urlId)
return
}
const matchAlpha = event.code.match(/^Key([A-Z])$/u)
// Browser hotkey
if (matchAlpha) {
const key = matchAlpha[1].toLowerCase()
const browserId = browsers.find((browser) => browser.hotKey === key)?.id
selectBrowser(urlId, browserId, event.altKey)
}
// Open favourite (first) browser
if (event.code === 'Space' || event.code === 'Enter') {
event.preventDefault()
const browserId = browsers[0].id
selectBrowser(urlId, browserId, event.altKey)
}
}
document.addEventListener('keydown', handler)
return function cleanup() {
document.removeEventListener('keydown', handler)
}
}, [urlId, browsers, setIsUrlHistoryOpen, isUrlHistoryOpen])
return <div>{children}</div>
}
export default TheKeyboardListeners