The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

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