The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

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