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.

198 lines
6.4 KiB

  1. import ImmutablePropTypes from 'react-immutable-proptypes';
  2. import PropTypes from 'prop-types';
  3. import IconButton from './icon_button';
  4. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  5. import { isIOS } from '../is_mobile';
  6. const messages = defineMessages({
  7. toggle_sound: { id: 'video_player.toggle_sound', defaultMessage: 'Toggle sound' },
  8. toggle_visible: { id: 'video_player.toggle_visible', defaultMessage: 'Toggle visibility' },
  9. expand_video: { id: 'video_player.expand', defaultMessage: 'Expand video' },
  10. expand_video: { id: 'video_player.video_error', defaultMessage: 'Video could not be played' }
  11. });
  12. class VideoPlayer extends React.PureComponent {
  13. constructor (props, context) {
  14. super(props, context);
  15. this.state = {
  16. visible: !this.props.sensitive,
  17. preview: true,
  18. muted: true,
  19. hasAudio: true,
  20. videoError: false
  21. };
  22. this.handleClick = this.handleClick.bind(this);
  23. this.handleVideoClick = this.handleVideoClick.bind(this);
  24. this.handleOpen = this.handleOpen.bind(this);
  25. this.handleVisibility = this.handleVisibility.bind(this);
  26. this.handleExpand = this.handleExpand.bind(this);
  27. this.setRef = this.setRef.bind(this);
  28. this.handleLoadedData = this.handleLoadedData.bind(this);
  29. this.handleVideoError = this.handleVideoError.bind(this);
  30. }
  31. handleClick () {
  32. this.setState({ muted: !this.state.muted });
  33. }
  34. handleVideoClick (e) {
  35. e.stopPropagation();
  36. const node = ReactDOM.findDOMNode(this).querySelector('video');
  37. if (node.paused) {
  38. node.play();
  39. } else {
  40. node.pause();
  41. }
  42. }
  43. handleOpen () {
  44. this.setState({ preview: !this.state.preview });
  45. }
  46. handleVisibility () {
  47. this.setState({
  48. visible: !this.state.visible,
  49. preview: true
  50. });
  51. }
  52. handleExpand () {
  53. this.video.pause();
  54. this.props.onOpenVideo(this.props.media, this.video.currentTime);
  55. }
  56. setRef (c) {
  57. this.video = c;
  58. }
  59. handleLoadedData () {
  60. if (('WebkitAppearance' in document.documentElement.style && this.video.audioTracks.length === 0) || this.video.mozHasAudio === false) {
  61. this.setState({ hasAudio: false });
  62. }
  63. }
  64. handleVideoError () {
  65. this.setState({ videoError: true });
  66. }
  67. componentDidMount () {
  68. if (!this.video) {
  69. return;
  70. }
  71. this.video.addEventListener('loadeddata', this.handleLoadedData);
  72. this.video.addEventListener('error', this.handleVideoError);
  73. }
  74. componentDidUpdate () {
  75. if (!this.video) {
  76. return;
  77. }
  78. this.video.addEventListener('loadeddata', this.handleLoadedData);
  79. this.video.addEventListener('error', this.handleVideoError);
  80. }
  81. componentWillUnmount () {
  82. if (!this.video) {
  83. return;
  84. }
  85. this.video.removeEventListener('loadeddata', this.handleLoadedData);
  86. this.video.removeEventListener('error', this.handleVideoError);
  87. }
  88. render () {
  89. const { media, intl, width, height, sensitive, autoplay } = this.props;
  90. let spoilerButton = (
  91. <div className='status__video-player-spoiler' style={{ display: !this.state.visible ? 'none' : 'block' }} >
  92. <IconButton overlay title={intl.formatMessage(messages.toggle_visible)} icon={this.state.visible ? 'eye' : 'eye-slash'} onClick={this.handleVisibility} />
  93. </div>
  94. );
  95. let expandButton = (
  96. <div className='status__video-player-expand'>
  97. <IconButton overlay title={intl.formatMessage(messages.expand_video)} icon='expand' onClick={this.handleExpand} />
  98. </div>
  99. );
  100. let muteButton = '';
  101. if (this.state.hasAudio) {
  102. muteButton = (
  103. <div className='status__video-player-mute'>
  104. <IconButton overlay title={intl.formatMessage(messages.toggle_sound)} icon={this.state.muted ? 'volume-off' : 'volume-up'} onClick={this.handleClick} />
  105. </div>
  106. );
  107. }
  108. if (!this.state.visible) {
  109. if (sensitive) {
  110. return (
  111. <div role='button' tabIndex='0' style={{ width: `${width}px`, height: `${height}px` }} className='media-spoiler' onClick={this.handleVisibility}>
  112. {spoilerButton}
  113. <span className='media-spoiler__warning'><FormattedMessage id='status.sensitive_warning' defaultMessage='Sensitive content' /></span>
  114. <span className='media-spoiler__trigger'><FormattedMessage id='status.sensitive_toggle' defaultMessage='Click to view' /></span>
  115. </div>
  116. );
  117. } else {
  118. return (
  119. <div role='button' tabIndex='0' style={{ width: `${width}px`, height: `${height}px` }} className='media-spoiler' onClick={this.handleVisibility}>
  120. {spoilerButton}
  121. <span className='media-spoiler__warning'><FormattedMessage id='status.media_hidden' defaultMessage='Media hidden' /></span>
  122. <span className='media-spoiler__trigger'><FormattedMessage id='status.sensitive_toggle' defaultMessage='Click to view' /></span>
  123. </div>
  124. );
  125. }
  126. }
  127. if (this.state.preview && !autoplay) {
  128. return (
  129. <div role='button' tabIndex='0' className='media-spoiler-video' style={{ width: `${width}px`, height: `${height}px`, background: `url(${media.get('preview_url')}) no-repeat center` }} onClick={this.handleOpen}>
  130. {spoilerButton}
  131. <div className='media-spoiler-video-play-icon'><i className='fa fa-play' /></div>
  132. </div>
  133. );
  134. }
  135. if (this.state.videoError) {
  136. return (
  137. <div style={{ width: `${width}px`, height: `${height}px` }} className='video-error-cover' >
  138. <span className='media-spoiler__warning'><FormattedMessage id='video_player.video_error' defaultMessage='Video could not be played' /></span>
  139. </div>
  140. );
  141. }
  142. return (
  143. <div className='status__video-player' style={{ width: `${width}px`, height: `${height}px` }}>
  144. {spoilerButton}
  145. {muteButton}
  146. {expandButton}
  147. <video className='status__video-player-video' role='button' tabIndex='0' ref={this.setRef} src={media.get('url')} autoPlay={!isIOS()} loop={true} muted={this.state.muted} onClick={this.handleVideoClick} />
  148. </div>
  149. );
  150. }
  151. }
  152. VideoPlayer.propTypes = {
  153. media: ImmutablePropTypes.map.isRequired,
  154. width: PropTypes.number,
  155. height: PropTypes.number,
  156. sensitive: PropTypes.bool,
  157. intl: PropTypes.object.isRequired,
  158. autoplay: PropTypes.bool,
  159. onOpenVideo: PropTypes.func.isRequired
  160. };
  161. VideoPlayer.defaultProps = {
  162. width: 239,
  163. height: 110
  164. };
  165. export default injectIntl(VideoPlayer);