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.

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