Skip to content

Commit

Permalink
feat(client): added support for seeking a video forward and backward …
Browse files Browse the repository at this point in the history
…using the left and right arrows

re #6
  • Loading branch information
Will Moss committed Sep 9, 2024
1 parent ddb4485 commit 0154aa8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Erin has all these features implemented :
- Mask the videos you don't want to see in your feed\*
- Choose which feed you want to play\*\*
- Autoplay your feed without even swiping
- Seek forward and backward using your keyboard
- Simple lazy-loading mechanism for your videos
- Automatic clip naming based on file name
- Simple and optional security using a master password
Expand Down
22 changes: 22 additions & 0 deletions src/components/VideoFeed/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const VideoFeed = ({
// Member - Track the currently-visible video (0-indexed)
const currentVideoIndex = useRef(0);

// Member - Track the currently-visible video element
const currentVideoElement = useRef();

// Member - Reference to the feed element
const feedRef = useRef();

Expand Down Expand Up @@ -81,6 +84,7 @@ const VideoFeed = ({
onFocusVideo(videos[currentIndex], currentIndex);
videoElement.addEventListener("timeupdate", handleVideoTimeUpdate, true);
videoElement.addEventListener("ended", replayVideo, true);
currentVideoElement.current = videoElement;
progressRef.current.style.transform = `scaleX(0)`;
}
// Case when a video is off-screen or being scrolled in / out of the screen
Expand Down Expand Up @@ -163,6 +167,24 @@ const VideoFeed = ({
};
}, [videos]); // eslint-disable-line react-hooks/exhaustive-deps

// Hook - On very first mount - Set keyboard forward/backward shortcuts
const handleKeyboardSeeking = (e) => {
if (e.code === "ArrowRight") seekVideoForward();
else if (e.code === "ArrowLeft") seekVideoBackward();

return;
};
const seekVideoForward = () => {
currentVideoElement.current.currentTime += 5;
};
const seekVideoBackward = () => {
currentVideoElement.current.currentTime -= 5;
};
useEffect(() => {
document.addEventListener("keydown", handleKeyboardSeeking);
return () => document.removeEventListener("keydown", handleKeyboardSeeking);
}, []); // eslint-disable-line react-hooks/exhaustive-deps

initialIndex = _bufferSize === videos.length ? 0 : initialIndex;

return (
Expand Down

0 comments on commit 0154aa8

Please sign in to comment.