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.

203 lines
6.2 KiB

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