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.

207 lines
5.6 KiB

  1. import ImmutablePropTypes from 'react-immutable-proptypes';
  2. import PureRenderMixin from 'react-addons-pure-render-mixin';
  3. import IconButton from './icon_button';
  4. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  5. const messages = defineMessages({
  6. toggle_sound: { id: 'video_player.toggle_sound', defaultMessage: 'Toggle sound' },
  7. toggle_visible: { id: 'video_player.toggle_visible', defaultMessage: 'Toggle visibility' }
  8. });
  9. const videoStyle = {
  10. position: 'relative',
  11. zIndex: '1',
  12. width: '100%',
  13. height: '100%',
  14. objectFit: 'cover',
  15. top: '50%',
  16. transform: 'translateY(-50%)'
  17. };
  18. const muteStyle = {
  19. position: 'absolute',
  20. top: '10px',
  21. right: '10px',
  22. opacity: '0.8',
  23. zIndex: '5'
  24. };
  25. const spoilerStyle = {
  26. marginTop: '8px',
  27. textAlign: 'center',
  28. height: '100%',
  29. cursor: 'pointer',
  30. display: 'flex',
  31. alignItems: 'center',
  32. justifyContent: 'center',
  33. flexDirection: 'column',
  34. position: 'relative'
  35. };
  36. const spoilerSpanStyle = {
  37. display: 'block',
  38. fontSize: '14px'
  39. };
  40. const spoilerSubSpanStyle = {
  41. display: 'block',
  42. fontSize: '11px',
  43. fontWeight: '500'
  44. };
  45. const spoilerButtonStyle = {
  46. position: 'absolute',
  47. top: '6px',
  48. left: '8px',
  49. zIndex: '100'
  50. };
  51. const VideoPlayer = React.createClass({
  52. propTypes: {
  53. media: ImmutablePropTypes.map.isRequired,
  54. width: React.PropTypes.number,
  55. height: React.PropTypes.number,
  56. sensitive: React.PropTypes.bool,
  57. intl: React.PropTypes.object.isRequired,
  58. autoplay: React.PropTypes.bool
  59. },
  60. getDefaultProps () {
  61. return {
  62. width: 239,
  63. height: 110
  64. };
  65. },
  66. getInitialState () {
  67. return {
  68. visible: !this.props.sensitive,
  69. preview: true,
  70. muted: true,
  71. hasAudio: true
  72. };
  73. },
  74. mixins: [PureRenderMixin],
  75. handleClick () {
  76. this.setState({ muted: !this.state.muted });
  77. },
  78. handleVideoClick (e) {
  79. e.stopPropagation();
  80. const node = ReactDOM.findDOMNode(this).querySelector('video');
  81. if (node.paused) {
  82. node.play();
  83. } else {
  84. node.pause();
  85. }
  86. },
  87. handleOpen () {
  88. this.setState({ preview: !this.state.preview });
  89. },
  90. handleVisibility () {
  91. this.setState({
  92. visible: !this.state.visible,
  93. preview: true
  94. });
  95. },
  96. setRef (c) {
  97. this.video = c;
  98. },
  99. handleLoadedData () {
  100. if (('WebkitAppearance' in document.documentElement.style && this.video.audioTracks.length === 0) || this.video.mozHasAudio === false) {
  101. this.setState({ hasAudio: false });
  102. }
  103. },
  104. componentDidMount () {
  105. if (!this.video) {
  106. return;
  107. }
  108. this.video.addEventListener('loadeddata', this.handleLoadedData);
  109. },
  110. componentDidUpdate () {
  111. if (!this.video) {
  112. return;
  113. }
  114. this.video.addEventListener('loadeddata', this.handleLoadedData);
  115. },
  116. componentWillUnmount () {
  117. if (!this.video) {
  118. return;
  119. }
  120. this.video.removeEventListener('loadeddata', this.handleLoadedData);
  121. },
  122. render () {
  123. const { media, intl, width, height, sensitive, autoplay } = this.props;
  124. let spoilerButton = (
  125. <div style={spoilerButtonStyle} >
  126. <IconButton title={intl.formatMessage(messages.toggle_visible)} icon={this.state.visible ? 'eye' : 'eye-slash'} onClick={this.handleVisibility} />
  127. </div>
  128. );
  129. let muteButton = '';
  130. if (this.state.hasAudio) {
  131. muteButton = (
  132. <div style={muteStyle}>
  133. <IconButton title={intl.formatMessage(messages.toggle_sound)} icon={this.state.muted ? 'volume-off' : 'volume-up'} onClick={this.handleClick} />
  134. </div>
  135. );
  136. }
  137. if (!this.state.visible) {
  138. if (sensitive) {
  139. return (
  140. <div style={{...spoilerStyle, width: `${width}px`, height: `${height}px` }} className='media-spoiler' onClick={this.handleVisibility}>
  141. {spoilerButton}
  142. <span style={spoilerSpanStyle}><FormattedMessage id='status.sensitive_warning' defaultMessage='Sensitive content' /></span>
  143. <span style={spoilerSubSpanStyle}><FormattedMessage id='status.sensitive_toggle' defaultMessage='Click to view' /></span>
  144. </div>
  145. );
  146. } else {
  147. return (
  148. <div style={{...spoilerStyle, width: `${width}px`, height: `${height}px` }} className='media-spoiler' onClick={this.handleOpen}>
  149. {spoilerButton}
  150. <span style={spoilerSpanStyle}><FormattedMessage id='status.media_hidden' defaultMessage='Media hidden' /></span>
  151. <span style={spoilerSubSpanStyle}><FormattedMessage id='status.sensitive_toggle' defaultMessage='Click to view' /></span>
  152. </div>
  153. );
  154. }
  155. }
  156. if (this.state.preview && !autoplay) {
  157. return (
  158. <div style={{ cursor: 'pointer', position: 'relative', marginTop: '8px', width: `${width}px`, height: `${height}px`, background: `url(${media.get('preview_url')}) no-repeat center`, backgroundSize: 'cover' }} onClick={this.handleOpen}>
  159. {spoilerButton}
  160. <div style={{ position: 'absolute', top: '50%', left: '50%', fontSize: '36px', transform: 'translate(-50%, -50%)', padding: '5px', borderRadius: '100px', color: 'rgba(255, 255, 255, 0.8)' }}><i className='fa fa-play' /></div>
  161. </div>
  162. );
  163. }
  164. return (
  165. <div style={{ cursor: 'default', marginTop: '8px', overflow: 'hidden', width: `${width}px`, height: `${height}px`, boxSizing: 'border-box', background: '#000', position: 'relative' }}>
  166. {spoilerButton}
  167. {muteButton}
  168. <video ref={this.setRef} src={media.get('url')} autoPlay='true' loop={true} muted={this.state.muted} style={videoStyle} onClick={this.handleVideoClick} />
  169. </div>
  170. );
  171. }
  172. });
  173. export default injectIntl(VideoPlayer);