From 2c96b302727087bfbe5437a8d9ae2305067be5fe Mon Sep 17 00:00:00 2001 From: Julien Calixte Date: Sat, 25 Jul 2020 13:48:56 +0200 Subject: [PATCH] =?UTF-8?q?:sparkles:=20(timer)=20play=20and=20pause=20w?= =?UTF-8?q?=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useTimer.ts | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/hooks/useTimer.ts b/src/hooks/useTimer.ts index eee8243..ffc8e4f 100644 --- a/src/hooks/useTimer.ts +++ b/src/hooks/useTimer.ts @@ -1,4 +1,10 @@ -import { ref, computed, watch } from '@vue/composition-api' +import { + ref, + computed, + watch, + onMounted, + onUnmounted +} from '@vue/composition-api' import { StopwatchState } from '@/types/StopwatchState' import { notify } from '@/utils/notification' import { useInterval } from './useInterval' @@ -84,6 +90,23 @@ export const useTimer = () => { isDev1Turn.value = true } + const handleSpacebar = (event: KeyboardEvent) => { + if (event.target !== document.body) { + return + } + if (event.charCode === 32) { + timeState.value === 'started' ? pause() : start() + } + } + + onMounted(() => { + window.addEventListener('keypress', handleSpacebar) + }) + + onUnmounted(() => { + window.removeEventListener('keypress', handleSpacebar) + }) + return { interval, start,