logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

index.js (11370B)


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