lbry-desktop/src/ui/component/viewers/videoViewer/view.jsx

80 lines
1.8 KiB
React
Raw Normal View History

2019-08-02 08:28:14 +02:00
// @flow
import React, { createRef, useEffect } from 'react';
import { stopContextMenu } from 'util/context-menu';
import videojs from 'video.js';
import 'video.js/dist/video-js.css';
import isUserTyping from 'util/detect-typing';
const SPACE_BAR_KEYCODE = 32;
const VIDEO_JS_OPTIONS = {
autoplay: true,
controls: true,
preload: 'auto',
playbackRates: [0.5, 1, 1.25, 1.5, 2],
};
type Props = {
source: string,
contentType: string,
2019-08-06 05:25:33 +02:00
muted: boolean,
2019-08-02 08:28:14 +02:00
};
function VideoViewer(props: Props) {
const { contentType, source } = props;
const videoRef = createRef();
// Handle any other effects separately to avoid re-mounting the video player when props change
useEffect(() => {
2019-08-06 05:25:33 +02:00
const videoNode = videoRef.current;
const videoJsOptions = {
...VIDEO_JS_OPTIONS,
sources: [
{
src: source,
type: contentType,
},
],
};
2019-08-02 08:28:14 +02:00
2019-08-06 05:25:33 +02:00
const player = videojs(videoNode, videoJsOptions);
return () => {
player.dispose();
};
2019-08-02 08:28:14 +02:00
}, [videoRef, source, contentType]);
useEffect(() => {
function handleKeyDown(e: KeyboardEvent) {
const videoNode = videoRef && videoRef.current;
if (!videoNode) return;
if (!isUserTyping() && e.keyCode === SPACE_BAR_KEYCODE) {
e.preventDefault();
const isPaused = videoNode.paused;
if (isPaused) {
videoNode.play();
return;
}
videoNode.pause();
}
}
window.addEventListener('keydown', handleKeyDown);
return () => {
window.removeEventListener('keydown', handleKeyDown);
};
}, [videoRef]);
return (
<div className="file-render__viewer" onContextMenu={stopContextMenu}>
<div data-vjs-player>
<video ref={videoRef} className="video-js" />
</div>
</div>
);
}
export default VideoViewer;