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 'flavours/glitch/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. letterbox: PropTypes.bool,
  81. fullwidth: PropTypes.bool,
  82. detailed: PropTypes.bool,
  83. intl: PropTypes.object.isRequired,
  84. };
  85. state = {
  86. currentTime: 0,
  87. duration: 0,
  88. paused: true,
  89. dragging: false,
  90. fullscreen: false,
  91. hovered: false,
  92. muted: false,
  93. revealed: !this.props.sensitive,
  94. };
  95. setPlayerRef = c => {
  96. this.player = c;
  97. }
  98. setVideoRef = c => {
  99. this.video = c;
  100. }
  101. setSeekRef = c => {
  102. this.seek = c;
  103. }
  104. handlePlay = () => {
  105. this.setState({ paused: false });
  106. }
  107. handlePause = () => {
  108. this.setState({ paused: true });
  109. }
  110. handleTimeUpdate = () => {
  111. this.setState({
  112. currentTime: Math.floor(this.video.currentTime),
  113. duration: Math.floor(this.video.duration),
  114. });
  115. }
  116. handleMouseDown = e => {
  117. document.addEventListener('mousemove', this.handleMouseMove, true);
  118. document.addEventListener('mouseup', this.handleMouseUp, true);
  119. document.addEventListener('touchmove', this.handleMouseMove, true);
  120. document.addEventListener('touchend', this.handleMouseUp, true);
  121. this.setState({ dragging: true });
  122. this.video.pause();
  123. this.handleMouseMove(e);
  124. }
  125. handleMouseUp = () => {
  126. document.removeEventListener('mousemove', this.handleMouseMove, true);
  127. document.removeEventListener('mouseup', this.handleMouseUp, true);
  128. document.removeEventListener('touchmove', this.handleMouseMove, true);
  129. document.removeEventListener('touchend', this.handleMouseUp, true);
  130. this.setState({ dragging: false });
  131. this.video.play();
  132. }
  133. handleMouseMove = throttle(e => {
  134. const { x } = getPointerPosition(this.seek, e);
  135. const currentTime = Math.floor(this.video.duration * x);
  136. this.video.currentTime = currentTime;
  137. this.setState({ currentTime });
  138. }, 60);
  139. togglePlay = () => {
  140. if (this.state.paused) {
  141. this.video.play();
  142. } else {
  143. this.video.pause();
  144. }
  145. }
  146. toggleFullscreen = () => {
  147. if (isFullscreen()) {
  148. exitFullscreen();
  149. } else {
  150. requestFullscreen(this.player);
  151. }
  152. }
  153. componentDidMount () {
  154. document.addEventListener('fullscreenchange', this.handleFullscreenChange, true);
  155. document.addEventListener('webkitfullscreenchange', this.handleFullscreenChange, true);
  156. document.addEventListener('mozfullscreenchange', this.handleFullscreenChange, true);
  157. document.addEventListener('MSFullscreenChange', this.handleFullscreenChange, true);
  158. }
  159. componentWillUnmount () {
  160. document.removeEventListener('fullscreenchange', this.handleFullscreenChange, true);
  161. document.removeEventListener('webkitfullscreenchange', this.handleFullscreenChange, true);
  162. document.removeEventListener('mozfullscreenchange', this.handleFullscreenChange, true);
  163. document.removeEventListener('MSFullscreenChange', this.handleFullscreenChange, true);
  164. }
  165. handleFullscreenChange = () => {
  166. this.setState({ fullscreen: isFullscreen() });
  167. }
  168. handleMouseEnter = () => {
  169. this.setState({ hovered: true });
  170. }
  171. handleMouseLeave = () => {
  172. this.setState({ hovered: false });
  173. }
  174. toggleMute = () => {
  175. this.video.muted = !this.video.muted;
  176. this.setState({ muted: this.video.muted });
  177. }
  178. toggleReveal = () => {
  179. if (this.state.revealed) {
  180. this.video.pause();
  181. }
  182. this.setState({ revealed: !this.state.revealed });
  183. }
  184. handleLoadedData = () => {
  185. if (this.props.startTime) {
  186. this.video.currentTime = this.props.startTime;
  187. this.video.play();
  188. }
  189. }
  190. handleProgress = () => {
  191. if (this.video.buffered.length > 0) {
  192. this.setState({ buffer: this.video.buffered.end(0) / this.video.duration * 100 });
  193. }
  194. }
  195. handleOpenVideo = () => {
  196. this.video.pause();
  197. this.props.onOpenVideo(this.video.currentTime);
  198. }
  199. handleCloseVideo = () => {
  200. this.video.pause();
  201. this.props.onCloseVideo();
  202. }
  203. render () {
  204. const { preview, src, width, height, startTime, onOpenVideo, onCloseVideo, intl, alt, letterbox, fullwidth, detailed } = this.props;
  205. const { currentTime, duration, buffer, dragging, paused, fullscreen, hovered, muted, revealed } = this.state;
  206. const progress = (currentTime / duration) * 100;
  207. return (
  208. <div className={classNames('video-player', { inactive: !revealed, detailed, inline: width && height && !fullscreen, fullscreen, letterbox, 'full-width': fullwidth })} style={{ width, height }} ref={this.setPlayerRef} onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave}>
  209. <video
  210. ref={this.setVideoRef}
  211. src={src}
  212. poster={preview}
  213. preload={startTime ? 'auto' : 'none'}
  214. loop
  215. role='button'
  216. tabIndex='0'
  217. aria-label={alt}
  218. width={width}
  219. height={height}
  220. onClick={this.togglePlay}
  221. onPlay={this.handlePlay}
  222. onPause={this.handlePause}
  223. onTimeUpdate={this.handleTimeUpdate}
  224. onLoadedData={this.handleLoadedData}
  225. onProgress={this.handleProgress}
  226. />
  227. <button className={classNames('video-player__spoiler', { active: !revealed })} onClick={this.toggleReveal}>
  228. <span className='video-player__spoiler__title'><FormattedMessage id='status.sensitive_warning' defaultMessage='Sensitive content' /></span>
  229. <span className='video-player__spoiler__subtitle'><FormattedMessage id='status.sensitive_toggle' defaultMessage='Click to view' /></span>
  230. </button>
  231. <div className={classNames('video-player__controls', { active: paused || hovered })}>
  232. <div className='video-player__seek' onMouseDown={this.handleMouseDown} ref={this.setSeekRef}>
  233. <div className='video-player__seek__buffer' style={{ width: `${buffer}%` }} />
  234. <div className='video-player__seek__progress' style={{ width: `${progress}%` }} />
  235. <span
  236. className={classNames('video-player__seek__handle', { active: dragging })}
  237. tabIndex='0'
  238. style={{ left: `${progress}%` }}
  239. />
  240. </div>
  241. <div className='video-player__buttons-bar'>
  242. <div className='video-player__buttons left'>
  243. <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>
  244. <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>
  245. {!onCloseVideo && <button aria-label={intl.formatMessage(messages.hide)} onClick={this.toggleReveal}><i className='fa fa-fw fa-eye' /></button>}
  246. {(detailed || fullscreen) &&
  247. <span>
  248. <span className='video-player__time-current'>{formatTime(currentTime)}</span>
  249. <span className='video-player__time-sep'>/</span>
  250. <span className='video-player__time-total'>{formatTime(duration)}</span>
  251. </span>
  252. }
  253. </div>
  254. <div className='video-player__buttons right'>
  255. {(!fullscreen && onOpenVideo) && <button aria-label={intl.formatMessage(messages.expand)} onClick={this.handleOpenVideo}><i className='fa fa-fw fa-expand' /></button>}
  256. {onCloseVideo && <button aria-label={intl.formatMessage(messages.close)} onClick={this.handleCloseVideo}><i className='fa fa-fw fa-compress' /></button>}
  257. <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>
  258. </div>
  259. </div>
  260. </div>
  261. </div>
  262. );
  263. }
  264. }