Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions packages/client/logic/shortcuts.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
import { Fn, not, and, whenever } from '@vueuse/core'
import { Fn, not, and } from '@vueuse/core'
import { watch } from 'vue'
import { fullscreen, magicKeys, shortcutsEnabled, isInputing, toggleOverview, showGotoDialog, showOverview, isOnFocus } from '../state'
import { toggleDark } from './dark'
import { next, nextSlide, prev, prevSlide } from './nav'

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

export function shortcut(key: string, fn: Fn) {
return whenever(and(magicKeys[key], _shortcut), fn, { flush: 'sync' })
export function shortcut(key: string, fn: Fn, autoRepeat = false) {
const source = and(magicKeys[key], _shortcut)
let count = 0
let timer: any
const trigger = () => {
clearTimeout(timer)
if (!source.value) {
count = 0
return
}
if (autoRepeat) {
timer = setTimeout(trigger, Math.max(1000 - count * 250, 150))
count++
}
fn()
}

return watch(source, trigger, { flush: 'sync' })
}

export function registerShotcuts() {
// global shortcuts
shortcut('space', next)
shortcut('right', next)
shortcut('left', prev)
shortcut('up', () => prevSlide(false))
shortcut('down', nextSlide)
shortcut('shift_left', () => prevSlide(false))
shortcut('shift_right', nextSlide)
shortcut('space', next, true)
shortcut('right', next, true)
shortcut('left', prev, true)
shortcut('up', () => prevSlide(false), true)
shortcut('down', nextSlide, true)
shortcut('shift_left', () => prevSlide(false), true)
shortcut('shift_right', nextSlide, true)
shortcut('d', toggleDark)
shortcut('f', () => fullscreen.toggle())
shortcut('o', toggleOverview)
Expand Down