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.

374 lines
12 KiB

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