From 8e1d18d06bb06b7c7e5033693c4c42f4f5ff92eb Mon Sep 17 00:00:00 2001 From: Nicolas Mowen Date: Fri, 22 Mar 2024 21:11:50 -0600 Subject: [PATCH] Move keyboard controls to video controls (#10617) --- web/src/components/player/HlsVideoPlayer.tsx | 52 -------------------- web/src/components/player/VideoControls.tsx | 35 +++++++++++++ 2 files changed, 35 insertions(+), 52 deletions(-) diff --git a/web/src/components/player/HlsVideoPlayer.tsx b/web/src/components/player/HlsVideoPlayer.tsx index f8cb3b34c..e40754339 100644 --- a/web/src/components/player/HlsVideoPlayer.tsx +++ b/web/src/components/player/HlsVideoPlayer.tsx @@ -1,14 +1,12 @@ import { MutableRefObject, ReactNode, - useCallback, useEffect, useRef, useState, } from "react"; import Hls from "hls.js"; import { isDesktop, isMobile } from "react-device-detect"; -import useKeyboardListener from "@/hooks/use-keyboard-listener"; import { TransformComponent, TransformWrapper } from "react-zoom-pan-pinch"; import VideoControls from "./VideoControls"; @@ -87,56 +85,6 @@ export default function HlsVideoPlayer({ const [controls, setControls] = useState(isMobile); const [controlsOpen, setControlsOpen] = useState(false); - const onKeyboardShortcut = useCallback( - (key: string, down: boolean, repeat: boolean) => { - if (!videoRef.current) { - return; - } - - switch (key) { - case "ArrowLeft": - if (down) { - const currentTime = videoRef.current.currentTime; - - if (currentTime) { - videoRef.current.currentTime = Math.max(0, currentTime - 5); - } - } - break; - case "ArrowRight": - if (down) { - const currentTime = videoRef.current.currentTime; - - if (currentTime) { - videoRef.current.currentTime = currentTime + 5; - } - } - break; - case "m": - if (down && !repeat && videoRef.current) { - videoRef.current.muted = !videoRef.current.muted; - } - break; - case " ": - if (down && videoRef.current) { - if (videoRef.current.paused) { - videoRef.current.play(); - } else { - videoRef.current.pause(); - } - } - break; - } - }, - // only update when preview only changes - // eslint-disable-next-line react-hooks/exhaustive-deps - [videoRef.current], - ); - useKeyboardListener( - ["ArrowLeft", "ArrowRight", "m", " "], - onKeyboardShortcut, - ); - return (
{ + switch (key) { + case "ArrowLeft": + if (down) { + onSeek(-10); + } + break; + case "ArrowRight": + if (down) { + onSeek(10); + } + break; + case "m": + if (down && !repeat && video) { + video.muted = !video.muted; + } + break; + case " ": + if (down) { + onPlayPause(!isPlaying); + } + break; + } + }, + // only update when preview only changes + // eslint-disable-next-line react-hooks/exhaustive-deps + [video, isPlaying, onSeek], + ); + useKeyboardListener( + ["ArrowLeft", "ArrowRight", "m", " "], + onKeyboardShortcut, + ); + if (!show) { return; }