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.

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