|
1 |
| -import { Fn, not, and, whenever } from '@vueuse/core' |
| 1 | +import { Fn, not, and } from '@vueuse/core' |
| 2 | +import { watch } from 'vue' |
2 | 3 | import { fullscreen, magicKeys, shortcutsEnabled, isInputing, toggleOverview, showGotoDialog, showOverview, isOnFocus } from '../state'
|
3 | 4 | import { toggleDark } from './dark'
|
4 | 5 | import { next, nextSlide, prev, prevSlide } from './nav'
|
5 | 6 |
|
6 | 7 | const _shortcut = and(not(isInputing), not(isOnFocus), shortcutsEnabled)
|
7 | 8 |
|
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' }) |
10 | 27 | }
|
11 | 28 |
|
12 | 29 | export function registerShotcuts() {
|
13 | 30 | // 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) |
21 | 38 | shortcut('d', toggleDark)
|
22 | 39 | shortcut('f', () => fullscreen.toggle())
|
23 | 40 | shortcut('o', toggleOverview)
|
|
0 commit comments