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.

210 lines
6.4 KiB

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