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.

220 lines
6.7 KiB

  1. import React from 'react';
  2. import ReactSwipeableViews from 'react-swipeable-views';
  3. import ImmutablePropTypes from 'react-immutable-proptypes';
  4. import PropTypes from 'prop-types';
  5. import Video from '../../video';
  6. import ExtendedVideoPlayer from '../../../components/extended_video_player';
  7. import classNames from 'classnames';
  8. import { defineMessages, injectIntl } from 'react-intl';
  9. import IconButton from '../../../components/icon_button';
  10. import ImmutablePureComponent from 'react-immutable-pure-component';
  11. import ImageLoader from './image_loader';
  12. const messages = defineMessages({
  13. close: { id: 'lightbox.close', defaultMessage: 'Close' },
  14. previous: { id: 'lightbox.previous', defaultMessage: 'Previous' },
  15. next: { id: 'lightbox.next', defaultMessage: 'Next' },
  16. });
  17. export const previewState = 'previewMediaModal';
  18. export default @injectIntl
  19. class MediaModal extends ImmutablePureComponent {
  20. static propTypes = {
  21. media: ImmutablePropTypes.list.isRequired,
  22. index: PropTypes.number.isRequired,
  23. onClose: PropTypes.func.isRequired,
  24. intl: PropTypes.object.isRequired,
  25. };
  26. static contextTypes = {
  27. router: PropTypes.object,
  28. };
  29. state = {
  30. index: null,
  31. navigationHidden: false,
  32. };
  33. handleSwipe = (index) => {
  34. this.setState({ index: index % this.props.media.size });
  35. }
  36. handleNextClick = () => {
  37. this.setState({ index: (this.getIndex() + 1) % this.props.media.size });
  38. }
  39. handlePrevClick = () => {
  40. this.setState({ index: (this.props.media.size + this.getIndex() - 1) % this.props.media.size });
  41. }
  42. handleChangeIndex = (e) => {
  43. const index = Number(e.currentTarget.getAttribute('data-index'));
  44. this.setState({ index: index % this.props.media.size });
  45. }
  46. handleKeyDown = (e) => {
  47. switch(e.key) {
  48. case 'ArrowLeft':
  49. this.handlePrevClick();
  50. e.preventDefault();
  51. e.stopPropagation();
  52. break;
  53. case 'ArrowRight':
  54. this.handleNextClick();
  55. e.preventDefault();
  56. e.stopPropagation();
  57. break;
  58. }
  59. }
  60. componentDidMount () {
  61. window.addEventListener('keydown', this.handleKeyDown, false);
  62. if (this.context.router) {
  63. const history = this.context.router.history;
  64. history.push(history.location.pathname, previewState);
  65. this.unlistenHistory = history.listen(() => {
  66. this.props.onClose();
  67. });
  68. }
  69. }
  70. componentWillUnmount () {
  71. window.removeEventListener('keydown', this.handleKeyDown);
  72. if (this.context.router) {
  73. this.unlistenHistory();
  74. if (this.context.router.history.location.state === previewState) {
  75. this.context.router.history.goBack();
  76. }
  77. }
  78. }
  79. getIndex () {
  80. return this.state.index !== null ? this.state.index : this.props.index;
  81. }
  82. toggleNavigation = () => {
  83. this.setState(prevState => ({
  84. navigationHidden: !prevState.navigationHidden,
  85. }));
  86. };
  87. render () {
  88. const { media, intl, onClose } = this.props;
  89. const { navigationHidden } = this.state;
  90. const index = this.getIndex();
  91. let pagination = [];
  92. const leftNav = media.size > 1 && <button tabIndex='0' className='media-modal__nav media-modal__nav--left' onClick={this.handlePrevClick} aria-label={intl.formatMessage(messages.previous)}><i className='fa fa-fw fa-chevron-left' /></button>;
  93. const rightNav = media.size > 1 && <button tabIndex='0' className='media-modal__nav media-modal__nav--right' onClick={this.handleNextClick} aria-label={intl.formatMessage(messages.next)}><i className='fa fa-fw fa-chevron-right' /></button>;
  94. if (media.size > 1) {
  95. pagination = media.map((item, i) => {
  96. const classes = ['media-modal__button'];
  97. if (i === index) {
  98. classes.push('media-modal__button--active');
  99. }
  100. return (<li className='media-modal__page-dot' key={i}><button tabIndex='0' className={classes.join(' ')} onClick={this.handleChangeIndex} data-index={i}>{i + 1}</button></li>);
  101. });
  102. }
  103. const content = media.map((image) => {
  104. const width = image.getIn(['meta', 'original', 'width']) || null;
  105. const height = image.getIn(['meta', 'original', 'height']) || null;
  106. if (image.get('type') === 'image') {
  107. return (
  108. <ImageLoader
  109. previewSrc={image.get('preview_url')}
  110. src={image.get('url')}
  111. width={width}
  112. height={height}
  113. alt={image.get('description')}
  114. key={image.get('url')}
  115. onClick={this.toggleNavigation}
  116. />
  117. );
  118. } else if (image.get('type') === 'video') {
  119. const { time } = this.props;
  120. return (
  121. <Video
  122. preview={image.get('preview_url')}
  123. src={image.get('url')}
  124. width={image.get('width')}
  125. height={image.get('height')}
  126. startTime={time || 0}
  127. onCloseVideo={onClose}
  128. detailed
  129. alt={image.get('description')}
  130. key={image.get('url')}
  131. />
  132. );
  133. } else if (image.get('type') === 'gifv') {
  134. return (
  135. <ExtendedVideoPlayer
  136. src={image.get('url')}
  137. muted
  138. controls={false}
  139. width={width}
  140. height={height}
  141. key={image.get('preview_url')}
  142. alt={image.get('description')}
  143. onClick={this.toggleNavigation}
  144. />
  145. );
  146. }
  147. return null;
  148. }).toArray();
  149. // you can't use 100vh, because the viewport height is taller
  150. // than the visible part of the document in some mobile
  151. // browsers when it's address bar is visible.
  152. // https://developers.google.com/web/updates/2016/12/url-bar-resizing
  153. const swipeableViewsStyle = {
  154. width: '100%',
  155. height: '100%',
  156. };
  157. const containerStyle = {
  158. alignItems: 'center', // center vertically
  159. };
  160. const navigationClassName = classNames('media-modal__navigation', {
  161. 'media-modal__navigation--hidden': navigationHidden,
  162. });
  163. return (
  164. <div className='modal-root__modal media-modal'>
  165. <div
  166. className='media-modal__closer'
  167. role='presentation'
  168. onClick={onClose}
  169. >
  170. <ReactSwipeableViews
  171. style={swipeableViewsStyle}
  172. containerStyle={containerStyle}
  173. onChangeIndex={this.handleSwipe}
  174. onSwitching={this.handleSwitching}
  175. index={index}
  176. >
  177. {content}
  178. </ReactSwipeableViews>
  179. </div>
  180. <div className={navigationClassName}>
  181. <IconButton className='media-modal__close' title={intl.formatMessage(messages.close)} icon='times' onClick={onClose} size={40} />
  182. {leftNav}
  183. {rightNav}
  184. <ul className='media-modal__pagination'>
  185. {pagination}
  186. </ul>
  187. </div>
  188. </div>
  189. );
  190. }
  191. }