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

zio/stable
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
1 changed files with 16 additions and 10 deletions

View File

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