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.

318 lines
11 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  4. import { throttle } from 'lodash';
  5. import classNames from 'classnames';
  6. import { isFullscreen, requestFullscreen, exitFullscreen } from '../ui/util/fullscreen';
  7. import { displaySensitiveMedia } from '../../initial_state';
  8. const messages = defineMessages({
  9. play: { id: 'video.play', defaultMessage: 'Play' },
  10. pause: { id: 'video.pause', defaultMessage: 'Pause' },
  11. mute: { id: 'video.mute', defaultMessage: 'Mute sound' },
  12. unmute: { id: 'video.unmute', defaultMessage: 'Unmute sound' },
  13. hide: { id: 'video.hide', defaultMessage: 'Hide video' },
  14. expand: { id: 'video.expand', defaultMessage: 'Expand video' },
  15. close: { id: 'video.close', defaultMessage: 'Close video' },
  16. fullscreen: { id: 'video.fullscreen', defaultMessage: 'Full screen' },
  17. exit_fullscreen: { id: 'video.exit_fullscreen', defaultMessage: 'Exit full screen' },
  18. });
  19. const formatTime = secondsNum => {
  20. let hours = Math.floor(secondsNum / 3600);
  21. let minutes = Math.floor((secondsNum - (hours * 3600)) / 60);
  22. let seconds = secondsNum - (hours * 3600) - (minutes * 60);
  23. if (hours < 10) hours = '0' + hours;
  24. if (minutes < 10) minutes = '0' + minutes;
  25. if (seconds < 10) seconds = '0' + seconds;
  26. return (hours === '00' ? '' : `${hours}:`) + `${minutes}:${seconds}`;
  27. };
  28. const findElementPosition = el => {
  29. let box;
  30. if (el.getBoundingClientRect && el.parentNode) {
  31. box = el.getBoundingClientRect();
  32. }
  33. if (!box) {
  34. return {
  35. left: 0,
  36. top: 0,
  37. };
  38. }
  39. const docEl = document.documentElement;
  40. const body = document.body;
  41. const clientLeft = docEl.clientLeft || body.clientLeft || 0;
  42. const scrollLeft = window.pageXOffset || body.scrollLeft;
  43. const left = (box.left + scrollLeft) - clientLeft;
  44. const clientTop = docEl.clientTop || body.clientTop || 0;
  45. const scrollTop = window.pageYOffset || body.scrollTop;
  46. const top = (box.top + scrollTop) - clientTop;
  47. return {
  48. left: Math.round(left),
  49. top: Math.round(top),
  50. };
  51. };
  52. const getPointerPosition = (el, event) => {
  53. const position = {};
  54. const box = findElementPosition(el);
  55. const boxW = el.offsetWidth;
  56. const boxH = el.offsetHeight;
  57. const boxY = box.top;
  58. const boxX = box.left;
  59. let pageY = event.pageY;
  60. let pageX = event.pageX;
  61. if (event.changedTouches) {
  62. pageX = event.changedTouches[0].pageX;
  63. pageY = event.changedTouches[0].pageY;
  64. }
  65. position.y = Math.max(0, Math.min(1, ((boxY - pageY) + boxH) / boxH));
  66. position.x = Math.max(0, Math.min(1, (pageX - boxX) / boxW));
  67. return position;
  68. };
  69. @injectIntl
  70. export default class Video extends React.PureComponent {
  71. static propTypes = {
  72. preview: PropTypes.string,
  73. src: PropTypes.string.isRequired,
  74. alt: PropTypes.string,
  75. width: PropTypes.number,
  76. height: PropTypes.number,
  77. sensitive: PropTypes.bool,
  78. startTime: PropTypes.number,
  79. onOpenVideo: PropTypes.func,
  80. onCloseVideo: PropTypes.func,
  81. detailed: PropTypes.bool,
  82. intl: PropTypes.object.isRequired,
  83. };
  84. state = {
  85. currentTime: 0,
  86. duration: 0,
  87. paused: true,
  88. dragging: false,
  89. fullscreen: false,
  90. hovered: false,
  91. muted: false,
  92. revealed: !this.props.sensitive || displaySensitiveMedia,
  93. };
  94. setPlayerRef = c => {
  95. this.player = c;
  96. }
  97. setVideoRef = c => {
  98. this.video = c;
  99. }
  100. setSeekRef = c => {
  101. this.seek = c;
  102. }
  103. handlePlay = () => {
  104. this.setState({ paused: false });
  105. }
  106. handlePause = () => {
  107. this.setState({ paused: true });
  108. }
  109. handleTimeUpdate = () => {
  110. this.setState({
  111. currentTime: Math.floor(this.video.currentTime),
  112. duration: Math.floor(this.video.duration),
  113. });
  114. }
  115. handleMouseDown = e => {
  116. document.addEventListener('mousemove', this.handleMouseMove, true);
  117. document.addEventListener('mouseup', this.handleMouseUp, true);
  118. document.addEventListener('touchmove', this.handleMouseMove, true);
  119. document.addEventListener('touchend', this.handleMouseUp, true);
  120. this.setState({ dragging: true });
  121. this.video.pause();
  122. this.handleMouseMove(e);
  123. }
  124. handleMouseUp = () => {
  125. document.removeEventListener('mousemove', this.handleMouseMove, true);
  126. document.removeEventListener('mouseup', this.handleMouseUp, true);
  127. document.removeEventListener('touchmove', this.handleMouseMove, true);
  128. document.removeEventListener('touchend', this.handleMouseUp, true);
  129. this.setState({ dragging: false });
  130. this.video.play();
  131. }
  132. handleMouseMove = throttle(e => {
  133. const { x } = getPointerPosition(this.seek, e);
  134. const currentTime = Math.floor(this.video.duration * x);
  135. this.video.currentTime = currentTime;
  136. this.setState({ currentTime });
  137. }, 60);
  138. togglePlay = () => {
  139. if (this.state.paused) {
  140. this.video.play();
  141. } else {
  142. this.video.pause();
  143. }
  144. }
  145. toggleFullscreen = () => {
  146. if (isFullscreen()) {
  147. exitFullscreen();
  148. } else {
  149. requestFullscreen(this.player);
  150. }
  151. }
  152. componentDidMount () {
  153. document.addEventListener('fullscreenchange', this.handleFullscreenChange, true);
  154. document.addEventListener('webkitfullscreenchange', this.handleFullscreenChange, true);
  155. document.addEventListener('mozfullscreenchange', this.handleFullscreenChange, true);
  156. document.addEventListener('MSFullscreenChange', this.handleFullscreenChange, true);
  157. }
  158. componentWillUnmount () {
  159. document.removeEventListener('fullscreenchange', this.handleFullscreenChange, true);
  160. document.removeEventListener('webkitfullscreenchange', this.handleFullscreenChange, true);
  161. document.removeEventListener('mozfullscreenchange', this.handleFullscreenChange, true);
  162. document.removeEventListener('MSFullscreenChange', this.handleFullscreenChange, true);
  163. }
  164. handleFullscreenChange = () => {
  165. this.setState({ fullscreen: isFullscreen() });
  166. }
  167. handleMouseEnter = () => {
  168. this.setState({ hovered: true });
  169. }
  170. handleMouseLeave = () => {
  171. this.setState({ hovered: false });
  172. }
  173. toggleMute = () => {
  174. this.video.muted = !this.video.muted;
  175. this.setState({ muted: this.video.muted });
  176. }
  177. toggleReveal = () => {
  178. if (this.state.revealed) {
  179. this.video.pause();
  180. }
  181. this.setState({ revealed: !this.state.revealed });
  182. }
  183. handleLoadedData = () => {
  184. if (this.props.startTime) {
  185. this.video.currentTime = this.props.startTime;
  186. this.video.play();
  187. }
  188. }
  189. handleProgress = () => {
  190. if (this.video.buffered.length > 0) {
  191. this.setState({ buffer: this.video.buffered.end(0) / this.video.duration * 100 });
  192. }
  193. }
  194. handleOpenVideo = () => {
  195. this.video.pause();
  196. this.props.onOpenVideo(this.video.currentTime);
  197. }
  198. handleCloseVideo = () => {
  199. this.video.pause();
  200. this.props.onCloseVideo();
  201. }
  202. render () {
  203. const { preview, src, width, height, startTime, onOpenVideo, onCloseVideo, intl, alt, detailed } = this.props;
  204. const { currentTime, duration, buffer, dragging, paused, fullscreen, hovered, muted, revealed } = this.state;
  205. const progress = (currentTime / duration) * 100;
  206. return (
  207. <div className={classNames('video-player', { inactive: !revealed, detailed, inline: width && height && !fullscreen, fullscreen })} style={{ width, height }} ref={this.setPlayerRef} onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave}>
  208. <video
  209. ref={this.setVideoRef}
  210. src={src}
  211. poster={preview}
  212. preload={startTime ? 'auto' : 'none'}
  213. loop
  214. role='button'
  215. tabIndex='0'
  216. aria-label={alt}
  217. width={width}
  218. height={height}
  219. onClick={this.togglePlay}
  220. onPlay={this.handlePlay}
  221. onPause={this.handlePause}
  222. onTimeUpdate={this.handleTimeUpdate}
  223. onLoadedData={this.handleLoadedData}
  224. onProgress={this.handleProgress}
  225. />
  226. <button type='button' className={classNames('video-player__spoiler', { active: !revealed })} onClick={this.toggleReveal}>
  227. <span className='video-player__spoiler__title'><FormattedMessage id='status.sensitive_warning' defaultMessage='Sensitive content' /></span>
  228. <span className='video-player__spoiler__subtitle'><FormattedMessage id='status.sensitive_toggle' defaultMessage='Click to view' /></span>
  229. </button>
  230. <div className={classNames('video-player__controls', { active: paused || hovered })}>
  231. <div className='video-player__seek' onMouseDown={this.handleMouseDown} ref={this.setSeekRef}>
  232. <div className='video-player__seek__buffer' style={{ width: `${buffer}%` }} />
  233. <div className='video-player__seek__progress' style={{ width: `${progress}%` }} />
  234. <span
  235. className={classNames('video-player__seek__handle', { active: dragging })}
  236. tabIndex='0'
  237. style={{ left: `${progress}%` }}
  238. />
  239. </div>
  240. <div className='video-player__buttons-bar'>
  241. <div className='video-player__buttons left'>
  242. <button type='button' aria-label={intl.formatMessage(paused ? messages.play : messages.pause)} onClick={this.togglePlay}><i className={classNames('fa fa-fw', { 'fa-play': paused, 'fa-pause': !paused })} /></button>
  243. <button type='button' aria-label={intl.formatMessage(muted ? messages.unmute : messages.mute)} onClick={this.toggleMute}><i className={classNames('fa fa-fw', { 'fa-volume-off': muted, 'fa-volume-up': !muted })} /></button>
  244. {!onCloseVideo && <button type='button' aria-label={intl.formatMessage(messages.hide)} onClick={this.toggleReveal}><i className='fa fa-fw fa-eye' /></button>}
  245. {(detailed || fullscreen) &&
  246. <span>
  247. <span className='video-player__time-current'>{formatTime(currentTime)}</span>
  248. <span className='video-player__time-sep'>/</span>
  249. <span className='video-player__time-total'>{formatTime(duration)}</span>
  250. </span>
  251. }
  252. </div>
  253. <div className='video-player__buttons right'>
  254. {(!fullscreen && onOpenVideo) && <button type='button' aria-label={intl.formatMessage(messages.expand)} onClick={this.handleOpenVideo}><i className='fa fa-fw fa-expand' /></button>}
  255. {onCloseVideo && <button type='button' aria-label={intl.formatMessage(messages.close)} onClick={this.handleCloseVideo}><i className='fa fa-fw fa-compress' /></button>}
  256. <button type='button' aria-label={intl.formatMessage(fullscreen ? messages.exit_fullscreen : messages.fullscreen)} onClick={this.toggleFullscreen}><i className={classNames('fa fa-fw', { 'fa-arrows-alt': !fullscreen, 'fa-compress': fullscreen })} /></button>
  257. </div>
  258. </div>
  259. </div>
  260. </div>
  261. );
  262. }
  263. }