Skip to content

Commit 902904d

Browse files
ecstremaantfu
andauthored
feat: shortcuts auto repeating (#114)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
1 parent 020097a commit 902904d

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

packages/client/logic/shortcuts.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,40 @@
1-
import { Fn, not, and, whenever } from '@vueuse/core'
1+
import { Fn, not, and } from '@vueuse/core'
2+
import { watch } from 'vue'
23
import { fullscreen, magicKeys, shortcutsEnabled, isInputing, toggleOverview, showGotoDialog, showOverview, isOnFocus } from '../state'
34
import { toggleDark } from './dark'
45
import { next, nextSlide, prev, prevSlide } from './nav'
56

67
const _shortcut = and(not(isInputing), not(isOnFocus), shortcutsEnabled)
78

8-
export function shortcut(key: string, fn: Fn) {
9-
return whenever(and(magicKeys[key], _shortcut), fn, { flush: 'sync' })
9+
export function shortcut(key: string, fn: Fn, autoRepeat = false) {
10+
const source = and(magicKeys[key], _shortcut)
11+
let count = 0
12+
let timer: any
13+
const trigger = () => {
14+
clearTimeout(timer)
15+
if (!source.value) {
16+
count = 0
17+
return
18+
}
19+
if (autoRepeat) {
20+
timer = setTimeout(trigger, Math.max(1000 - count * 250, 150))
21+
count++
22+
}
23+
fn()
24+
}
25+
26+
return watch(source, trigger, { flush: 'sync' })
1027
}
1128

1229
export function registerShotcuts() {
1330
// global shortcuts
14-
shortcut('space', next)
15-
shortcut('right', next)
16-
shortcut('left', prev)
17-
shortcut('up', () => prevSlide(false))
18-
shortcut('down', nextSlide)
19-
shortcut('shift_left', () => prevSlide(false))
20-
shortcut('shift_right', nextSlide)
31+
shortcut('space', next, true)
32+
shortcut('right', next, true)
33+
shortcut('left', prev, true)
34+
shortcut('up', () => prevSlide(false), true)
35+
shortcut('down', nextSlide, true)
36+
shortcut('shift_left', () => prevSlide(false), true)
37+
shortcut('shift_right', nextSlide, true)
2138
shortcut('d', toggleDark)
2239
shortcut('f', () => fullscreen.toggle())
2340
shortcut('o', toggleOverview)

0 commit comments

Comments
 (0)