feat(command): support for emacs shortcuts (#416)

This commit is contained in:
Ayaka Rizumu 2022-12-14 19:09:18 +08:00 committed by GitHub
parent 762a936793
commit 57017a23f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -53,24 +53,32 @@ const intoView = (index: number) => {
if (el)
el.scrollIntoView({ block: 'nearest' })
}
function setActive(index: number) {
const len = result.length
active = (index + len) % len
intoView(active)
}
const onKeyDown = (e: KeyboardEvent) => {
switch (e.key) {
case 'p':
case 'ArrowUp': {
if (e.key === 'p' && !e.ctrlKey)
break
e.preventDefault()
active = Math.max(0, active - 1)
intoView(active)
setActive(active - 1)
break
}
case 'n':
case 'ArrowDown': {
if (e.key === 'n' && !e.ctrlKey)
break
e.preventDefault()
active = Math.min(result.length - 1, active + 1)
intoView(active)
setActive(active + 1)
break
}
@ -88,9 +96,7 @@ const onKeyDown = (e: KeyboardEvent) => {
case 'End': {
e.preventDefault()
active = result.length - 1
intoView(active)
setActive(result.length - 1)
break
}