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.

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