You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
200 lines
6.1 KiB
200 lines
6.1 KiB
7 years ago
|
// Package imports //
|
||
8 years ago
|
import React from 'react';
|
||
8 years ago
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
||
8 years ago
|
import PropTypes from 'prop-types';
|
||
8 years ago
|
import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
|
||
7 years ago
|
|
||
|
// Mastodon imports //
|
||
|
import IconButton from '../../../mastodon/components/icon_button';
|
||
|
import { isIOS } from '../../../mastodon/is_mobile';
|
||
8 years ago
|
|
||
|
const messages = defineMessages({
|
||
8 years ago
|
toggle_sound: { id: 'video_player.toggle_sound', defaultMessage: 'Toggle sound' },
|
||
8 years ago
|
toggle_visible: { id: 'video_player.toggle_visible', defaultMessage: 'Toggle visibility' },
|
||
8 years ago
|
expand_video: { id: 'video_player.expand', defaultMessage: 'Expand video' },
|
||
8 years ago
|
});
|
||
8 years ago
|
|
||
8 years ago
|
@injectIntl
|
||
7 years ago
|
export default class StatusVideoPlayer extends React.PureComponent {
|
||
8 years ago
|
|
||
8 years ago
|
static propTypes = {
|
||
|
media: ImmutablePropTypes.map.isRequired,
|
||
7 years ago
|
letterbox: PropTypes.bool,
|
||
7 years ago
|
fullwidth: PropTypes.bool,
|
||
8 years ago
|
height: PropTypes.number,
|
||
|
sensitive: PropTypes.bool,
|
||
|
intl: PropTypes.object.isRequired,
|
||
|
autoplay: PropTypes.bool,
|
||
8 years ago
|
onOpenVideo: PropTypes.func.isRequired,
|
||
8 years ago
|
};
|
||
|
|
||
|
static defaultProps = {
|
||
8 years ago
|
height: 110,
|
||
8 years ago
|
};
|
||
|
|
||
|
state = {
|
||
|
visible: !this.props.sensitive,
|
||
|
preview: true,
|
||
|
muted: true,
|
||
|
hasAudio: true,
|
||
8 years ago
|
videoError: false,
|
||
8 years ago
|
};
|
||
|
|
||
|
handleClick = () => {
|
||
8 years ago
|
this.setState({ muted: !this.state.muted });
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
handleVideoClick = (e) => {
|
||
8 years ago
|
e.stopPropagation();
|
||
|
|
||
8 years ago
|
const node = this.video;
|
||
8 years ago
|
|
||
|
if (node.paused) {
|
||
|
node.play();
|
||
|
} else {
|
||
|
node.pause();
|
||
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
handleOpen = () => {
|
||
8 years ago
|
this.setState({ preview: !this.state.preview });
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
handleVisibility = () => {
|
||
8 years ago
|
this.setState({
|
||
|
visible: !this.state.visible,
|
||
8 years ago
|
preview: true,
|
||
8 years ago
|
});
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
handleExpand = () => {
|
||
8 years ago
|
this.video.pause();
|
||
|
this.props.onOpenVideo(this.props.media, this.video.currentTime);
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
setRef = (c) => {
|
||
8 years ago
|
this.video = c;
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
handleLoadedData = () => {
|
||
8 years ago
|
if (('WebkitAppearance' in document.documentElement.style && this.video.audioTracks.length === 0) || this.video.mozHasAudio === false) {
|
||
|
this.setState({ hasAudio: false });
|
||
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
handleVideoError = () => {
|
||
8 years ago
|
this.setState({ videoError: true });
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
componentDidMount () {
|
||
|
if (!this.video) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
this.video.addEventListener('loadeddata', this.handleLoadedData);
|
||
8 years ago
|
this.video.addEventListener('error', this.handleVideoError);
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
|
componentDidUpdate () {
|
||
|
if (!this.video) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
this.video.addEventListener('loadeddata', this.handleLoadedData);
|
||
8 years ago
|
this.video.addEventListener('error', this.handleVideoError);
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
|
componentWillUnmount () {
|
||
|
if (!this.video) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
this.video.removeEventListener('loadeddata', this.handleLoadedData);
|
||
8 years ago
|
this.video.removeEventListener('error', this.handleVideoError);
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
render () {
|
||
7 years ago
|
const { media, intl, letterbox, fullwidth, height, sensitive, autoplay } = this.props;
|
||
8 years ago
|
|
||
8 years ago
|
let spoilerButton = (
|
||
8 years ago
|
<div className={`status__video-player-spoiler ${this.state.visible ? 'status__video-player-spoiler--visible' : ''}`}>
|
||
8 years ago
|
<IconButton overlay title={intl.formatMessage(messages.toggle_visible)} icon={this.state.visible ? 'eye' : 'eye-slash'} onClick={this.handleVisibility} />
|
||
8 years ago
|
</div>
|
||
|
);
|
||
|
|
||
8 years ago
|
let expandButton = (
|
||
8 years ago
|
<div className='status__video-player-expand'>
|
||
8 years ago
|
<IconButton overlay title={intl.formatMessage(messages.expand_video)} icon='expand' onClick={this.handleExpand} />
|
||
8 years ago
|
</div>
|
||
|
);
|
||
|
|
||
8 years ago
|
let muteButton = '';
|
||
|
|
||
|
if (this.state.hasAudio) {
|
||
|
muteButton = (
|
||
8 years ago
|
<div className='status__video-player-mute'>
|
||
8 years ago
|
<IconButton overlay title={intl.formatMessage(messages.toggle_sound)} icon={this.state.muted ? 'volume-off' : 'volume-up'} onClick={this.handleClick} />
|
||
8 years ago
|
</div>
|
||
|
);
|
||
|
}
|
||
|
|
||
8 years ago
|
if (!this.state.visible) {
|
||
|
if (sensitive) {
|
||
|
return (
|
||
7 years ago
|
<div role='button' tabIndex='0' style={{ height: `${height}px` }} className={`media-spoiler ${fullwidth ? 'full-width' : ''}`} onClick={this.handleVisibility}>
|
||
8 years ago
|
{spoilerButton}
|
||
8 years ago
|
<span className='media-spoiler__warning'><FormattedMessage id='status.sensitive_warning' defaultMessage='Sensitive content' /></span>
|
||
|
<span className='media-spoiler__trigger'><FormattedMessage id='status.sensitive_toggle' defaultMessage='Click to view' /></span>
|
||
8 years ago
|
</div>
|
||
|
);
|
||
|
} else {
|
||
|
return (
|
||
7 years ago
|
<div role='button' tabIndex='0' style={{ height: `${height}px` }} className={`media-spoiler ${fullwidth ? 'full-width' : ''}`} onClick={this.handleVisibility}>
|
||
8 years ago
|
{spoilerButton}
|
||
8 years ago
|
<span className='media-spoiler__warning'><FormattedMessage id='status.media_hidden' defaultMessage='Media hidden' /></span>
|
||
|
<span className='media-spoiler__trigger'><FormattedMessage id='status.sensitive_toggle' defaultMessage='Click to view' /></span>
|
||
8 years ago
|
</div>
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
if (this.state.preview && !autoplay) {
|
||
8 years ago
|
return (
|
||
7 years ago
|
<div role='button' tabIndex='0' className={`media-spoiler-video ${fullwidth ? 'full-width' : ''}`} style={{ height: `${height}px`, backgroundImage: `url(${media.get('preview_url')})` }} onClick={this.handleOpen}>
|
||
8 years ago
|
{spoilerButton}
|
||
8 years ago
|
<div className='media-spoiler-video-play-icon'><i className='fa fa-play' /></div>
|
||
8 years ago
|
</div>
|
||
|
);
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
if (this.state.videoError) {
|
||
|
return (
|
||
7 years ago
|
<div style={{ height: `${height}px` }} className='video-error-cover' >
|
||
8 years ago
|
<span className='media-spoiler__warning'><FormattedMessage id='video_player.video_error' defaultMessage='Video could not be played' /></span>
|
||
8 years ago
|
</div>
|
||
|
);
|
||
|
}
|
||
|
|
||
8 years ago
|
return (
|
||
7 years ago
|
<div className={`status__video-player ${fullwidth ? 'full-width' : ''}`} style={{ height: `${height}px` }}>
|
||
8 years ago
|
{spoilerButton}
|
||
8 years ago
|
{muteButton}
|
||
8 years ago
|
{expandButton}
|
||
8 years ago
|
|
||
|
<video
|
||
7 years ago
|
className={`status__video-player-video${letterbox ? ' letterbox' : ''}`}
|
||
8 years ago
|
role='button'
|
||
|
tabIndex='0'
|
||
|
ref={this.setRef}
|
||
|
src={media.get('url')}
|
||
|
autoPlay={!isIOS()}
|
||
8 years ago
|
loop
|
||
8 years ago
|
muted={this.state.muted}
|
||
|
onClick={this.handleVideoClick}
|
||
|
/>
|
||
8 years ago
|
</div>
|
||
|
);
|
||
|
}
|
||
|
|
||
8 years ago
|
}
|