fix toggleFullScreen method of player

This commit is contained in:
btzr-io 2019-06-04 20:26:57 -06:00
parent 3c30a167c0
commit e49b06a9f7
5 changed files with 92 additions and 57 deletions

View file

@ -4,7 +4,7 @@ import * as ICONS from 'constants/icons';
import * as React from 'react'; import * as React from 'react';
import Button from 'component/button'; import Button from 'component/button';
import Tooltip from 'component/common/tooltip'; import Tooltip from 'component/common/tooltip';
import { requestFullscreen } from 'util/full-screen'; import { requestFullscreen, fullscreenElement } from 'util/full-screen';
type FileInfo = { type FileInfo = {
claim_id: string, claim_id: string,
@ -16,14 +16,19 @@ type Props = {
openModal: (id: string, { uri: string }) => void, openModal: (id: string, { uri: string }) => void,
claimIsMine: boolean, claimIsMine: boolean,
fileInfo: FileInfo, fileInfo: FileInfo,
viewerContainer: React.Ref,
showFullscreen: boolean, showFullscreen: boolean,
}; };
class FileActions extends React.PureComponent<Props> { class FileActions extends React.PureComponent<Props> {
maximizeViewer = () => { maximizeViewer = () => {
// Get viewer container const { viewerContainer } = this.props;
const viewer = document.getElementsByClassName('content__embedded')[0]; const isFullscreen = fullscreenElement();
requestFullscreen(viewer); // Request fullscreen if viewer is ready
// And if there is no fullscreen element active
if (!isFullscreen && viewerContainer && viewerContainer.current !== null) {
requestFullscreen(viewerContainer.current);
}
}; };
render() { render() {

View file

@ -1,14 +1,20 @@
// @flow // @flow
import '@babel/polyfill'; import '@babel/polyfill';
import * as React from 'react'; import * as React from 'react';
// @if TARGET='app' // @if TARGET='app'
import { remote } from 'electron';
import fs from 'fs'; import fs from 'fs';
import { remote } from 'electron';
// @endif // @endif
import path from 'path'; import path from 'path';
import player from 'render-media'; import player from 'render-media';
import FileRender from 'component/fileRender'; import FileRender from 'component/fileRender';
import LoadingScreen from 'component/common/loading-screen'; import LoadingScreen from 'component/common/loading-screen';
import { fullscreenElement, requestFullscreen, exitFullscreen } from 'util/full-screen';
// Shorcut key code for fullscreen (f)
const F_KEYCODE = 70;
type Props = { type Props = {
contentType: string, contentType: string,
@ -23,6 +29,8 @@ type Props = {
onFinishCb: ?() => void, onFinishCb: ?() => void,
savePosition: number => void, savePosition: number => void,
changeVolume: number => void, changeVolume: number => void,
viewerContainer: React.Ref,
searchBarFocused: boolean,
}; };
type State = { type State = {
@ -69,7 +77,6 @@ class MediaPlayer extends React.PureComponent<Props, State> {
this.mediaContainer = React.createRef(); this.mediaContainer = React.createRef();
(this: any).togglePlay = this.togglePlay.bind(this); (this: any).togglePlay = this.togglePlay.bind(this);
(this: any).toggleFullScreen = this.toggleFullScreen.bind(this);
} }
componentDidMount() { componentDidMount() {
@ -91,25 +98,62 @@ class MediaPlayer extends React.PureComponent<Props, State> {
componentWillUnmount() { componentWillUnmount() {
const mediaElement = this.mediaContainer.current.children[0]; const mediaElement = this.mediaContainer.current.children[0];
document.removeEventListener('keydown', this.togglePlay); document.removeEventListener('keydown', this.handleKeyDown);
if (mediaElement) { if (mediaElement) {
mediaElement.removeEventListener('click', this.togglePlay); mediaElement.removeEventListener('click', this.togglePlay);
mediaElement.removeEventListener('dbclick', this.handleDoubleClick);
} }
} }
toggleFullScreen() { handleKeyDown = (event: SyntheticKeyboardEvent<*>) => {
const mediaElement = this.mediaContainer.current; const { searchBarFocused } = this.props;
if (mediaElement) {
// $FlowFixMe if (!searchBarFocused) {
if (document.webkitIsFullScreen) { // Handle fullscreen shortcut key (f)
// $FlowFixMe if (event.keyCode === F_KEYCODE) {
document.webkitExitFullscreen(); this.toggleFullscreen();
} else {
mediaElement.webkitRequestFullScreen();
} }
// Handle toggle play
this.togglePlay(event);
} }
} };
handleDoubleClick = (event: SyntheticInputEvent<*>) => {
// Prevent pause / play
event.preventDefault();
event.stopPropagation();
// Trigger fullscreen mode
this.toggleFullscreen();
};
toggleFullscreen = () => {
const { viewerContainer } = this.props;
const isFullscreen = fullscreenElement();
const isPlayableType = this.playableType();
if (!isFullscreen) {
// Enter fullscreen mode if content is not playable
// Otherwise it should be handle internally on the video player
// or it will break the toggle fullscreen button
if (!isPlayableType && viewerContainer && viewerContainer.current !== null) {
requestFullscreen(viewerContainer.current);
}
// Request fullscreen mode for the media player (renderMedia)
// Don't use this with the new player
// @if TARGET='app'
else {
const mediaContainer = this.mediaContainer.current;
const mediaElement = mediaContainer && mediaContainer.children[0];
if (mediaElement) {
requestFullscreen(mediaElement);
}
}
// @endif
} else {
exitFullscreen();
}
};
async playMedia() { async playMedia() {
const container = this.mediaContainer.current; const container = this.mediaContainer.current;
@ -165,7 +209,8 @@ class MediaPlayer extends React.PureComponent<Props, State> {
); );
} }
document.addEventListener('keydown', this.togglePlay); document.addEventListener('keydown', this.handleKeyDown);
const mediaElement = container.children[0]; const mediaElement = container.children[0];
if (mediaElement) { if (mediaElement) {
if (position) { if (position) {
@ -188,7 +233,7 @@ class MediaPlayer extends React.PureComponent<Props, State> {
changeVolume(mediaElement.volume); changeVolume(mediaElement.volume);
}); });
mediaElement.volume = volume; mediaElement.volume = volume;
mediaElement.addEventListener('dblclick', this.toggleFullScreen); mediaElement.addEventListener('dblclick', this.handleDoubleClick);
} }
// @endif // @endif

View file

@ -5,7 +5,6 @@ import classnames from 'classnames';
import analytics from 'analytics'; import analytics from 'analytics';
import LoadingScreen from 'component/common/loading-screen'; import LoadingScreen from 'component/common/loading-screen';
import PlayButton from './internal/play-button'; import PlayButton from './internal/play-button';
import { requestFullscreen, exitFullscreen } from 'util/full-screen';
const Player = React.lazy(() => const Player = React.lazy(() =>
import( import(
@ -14,7 +13,6 @@ const Player = React.lazy(() =>
) )
); );
const F_KEYCODE = 70;
const SPACE_BAR_KEYCODE = 32; const SPACE_BAR_KEYCODE = 32;
type Props = { type Props = {
@ -54,6 +52,7 @@ type Props = {
nsfw: boolean, nsfw: boolean,
thumbnail: ?string, thumbnail: ?string,
isPlayableType: boolean, isPlayableType: boolean,
viewerContainer: React.Ref,
}; };
class FileViewer extends React.PureComponent<Props> { class FileViewer extends React.PureComponent<Props> {
@ -68,8 +67,6 @@ class FileViewer extends React.PureComponent<Props> {
// Don't add these variables to state because we don't need to re-render when their values change // Don't add these variables to state because we don't need to re-render when their values change
(this: any).startTime = undefined; (this: any).startTime = undefined;
(this: any).playTime = undefined; (this: any).playTime = undefined;
(this: any).container = React.createRef();
} }
componentDidMount() { componentDidMount() {
@ -138,34 +135,6 @@ class FileViewer extends React.PureComponent<Props> {
event.preventDefault(); // prevent page scroll event.preventDefault(); // prevent page scroll
this.playContent(); this.playContent();
} }
// Handle fullscreen shortcut key (f)
if (event.keyCode === F_KEYCODE) {
this.toggleFullscreen();
}
}
}
toggleFullscreen() {
const { isPlayableType } = this.props;
if (!document.webkitFullscreenElement) {
// Enter fullscreen mode if content is not playable
// Otherwise it should be handle internally on the video player
// or it will break the toggle fullscreen button
if (!isPlayableType) {
requestFullscreen(this.container.current);
}
// Request fullscreen mode for the video player
// Don't use this with the new player
// @if TARGET='app'
else {
const video = document.getElementsByTagName('video')[0];
video && requestFullscreen(video);
}
// @endif
} else {
exitFullscreen();
} }
} }
@ -260,6 +229,8 @@ class FileViewer extends React.PureComponent<Props> {
obscureNsfw, obscureNsfw,
mediaType, mediaType,
insufficientCredits, insufficientCredits,
viewerContainer,
searchBarFocused,
thumbnail, thumbnail,
nsfw, nsfw,
} = this.props; } = this.props;
@ -295,7 +266,7 @@ class FileViewer extends React.PureComponent<Props> {
const layoverStyle = !shouldObscureNsfw && thumbnail ? { backgroundImage: `url("${thumbnail}")` } : {}; const layoverStyle = !shouldObscureNsfw && thumbnail ? { backgroundImage: `url("${thumbnail}")` } : {};
return ( return (
<div className={classnames('video', {}, className)} ref={this.container}> <div className={classnames('video', {}, className)} ref={viewerContainer}>
{isPlaying && ( {isPlaying && (
<div className="content__view"> <div className="content__view">
{!isReadyToPlay ? ( {!isReadyToPlay ? (
@ -320,6 +291,8 @@ class FileViewer extends React.PureComponent<Props> {
onStartCb={this.onFileStartCb} onStartCb={this.onFileStartCb}
onFinishCb={this.onFileFinishCb} onFinishCb={this.onFileFinishCb}
playingUri={playingUri} playingUri={playingUri}
searchBarFocused={searchBarFocused}
viewerContainer={viewerContainer}
/> />
</Suspense> </Suspense>
)} )}

View file

@ -62,16 +62,21 @@ class FilePage extends React.Component<Props> {
'application', 'application',
]; ];
constructor(props: Props) {
super(props);
(this: any).viewerContainer = React.createRef();
}
componentDidMount() { componentDidMount() {
const { const {
uri, uri,
claim,
fetchFileInfo, fetchFileInfo,
fetchCostInfo, fetchCostInfo,
setViewed, setViewed,
isSubscribed, isSubscribed,
claimIsMine, claimIsMine,
fetchViewCount, fetchViewCount,
claim,
} = this.props; } = this.props;
if (isSubscribed) { if (isSubscribed) {
@ -198,11 +203,12 @@ class FilePage extends React.Component<Props> {
)} )}
{showFile && ( {showFile && (
<FileViewer <FileViewer
insufficientCredits={insufficientCredits}
className="content__embedded"
uri={uri} uri={uri}
className="content__embedded"
mediaType={mediaType} mediaType={mediaType}
isPlayableType={isPlayableType} isPlayableType={isPlayableType}
viewerContainer={this.viewerContainer}
insufficientCredits={insufficientCredits}
/> />
)} )}
{!showFile && {!showFile &&
@ -288,7 +294,12 @@ class FilePage extends React.Component<Props> {
<div className="media__action-group--large"> <div className="media__action-group--large">
<FileDownloadLink uri={uri} /> <FileDownloadLink uri={uri} />
<FileActions uri={uri} claimId={claim.claim_id} showFullscreen={isPreviewType} /> <FileActions
uri={uri}
claimId={claim.claim_id}
showFullscreen={isPreviewType}
viewerContainer={this.viewerContainer}
/>
</div> </div>
</div> </div>

View file

@ -26,7 +26,8 @@ const getPrefix = () => {
export const fullscreenElement = () => { export const fullscreenElement = () => {
const index = getPrefix(); const index = getPrefix();
return prefixes.fullscreenElement[index]; const prefix = prefixes.fullscreenElement[index];
return document[prefix];
}; };
export const requestFullscreen = elem => { export const requestFullscreen = elem => {