From e1d3f5e3b38c695334325fd2ba3baa547254a6c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Marche?= <35939574+Marr11317@users.noreply.github.com> Date: Sun, 16 May 2021 02:28:58 -0400 Subject: [PATCH 1/2] Add autoRepeating shortcuts Currently, keeping right pressed does nothing more than just pressing it once. The event should auto repeat if the key combination is still pressed. This PR is an untested work. I have not built slidev, I just wanted to try it out and ask if you thought it was a good idea. I actually modified the code in github's default, so it might not even build. --- packages/client/logic/shortcuts.ts | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/packages/client/logic/shortcuts.ts b/packages/client/logic/shortcuts.ts index 4d799bb95d..46babcbdbf 100644 --- a/packages/client/logic/shortcuts.ts +++ b/packages/client/logic/shortcuts.ts @@ -5,19 +5,31 @@ 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: boolean = false) { + let count = 0; + const f = () => { + if (autoRepeat && magicKeys.current.has(key)) { + setTimeout(() => f(), Math.max(1000 - count * 40, 200)) + count++ + } + else { + count = 0 + } + fn() + } + if (!autoRepeat) + return whenever(and(magicKeys[key], _shortcut), f, { 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) From a9bda8e968fe3f65fd2c26bdfdf477881101319d Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Mon, 17 May 2021 10:51:27 +0800 Subject: [PATCH 2/2] chore: update --- packages/client/logic/shortcuts.ts | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/packages/client/logic/shortcuts.ts b/packages/client/logic/shortcuts.ts index 46babcbdbf..61fd917db7 100644 --- a/packages/client/logic/shortcuts.ts +++ b/packages/client/logic/shortcuts.ts @@ -1,24 +1,29 @@ -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, autoRepeat: boolean = false) { - let count = 0; - const f = () => { - if (autoRepeat && magicKeys.current.has(key)) { - setTimeout(() => f(), Math.max(1000 - count * 40, 200)) - count++ - } - else { +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() } - if (!autoRepeat) - return whenever(and(magicKeys[key], _shortcut), f, { flush: 'sync' }) + + return watch(source, trigger, { flush: 'sync' }) } export function registerShotcuts() {