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.

212 lines
6.5 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. const previewState = 'previewMediaModal';
  18. @injectIntl
  19. export default 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. handleKeyUp = (e) => {
  47. switch(e.key) {
  48. case 'ArrowLeft':
  49. this.handlePrevClick();
  50. break;
  51. case 'ArrowRight':
  52. this.handleNextClick();
  53. break;
  54. }
  55. }
  56. componentDidMount () {
  57. window.addEventListener('keyup', this.handleKeyUp, false);
  58. const history = this.context.router.history;
  59. history.push(history.location.pathname, previewState);
  60. this.unlistenHistory = history.listen(() => {
  61. this.props.onClose();
  62. });
  63. }
  64. componentWillUnmount () {
  65. window.removeEventListener('keyup', this.handleKeyUp);
  66. this.unlistenHistory();
  67. if (this.context.router.history.location.state === previewState) {
  68. this.context.router.history.goBack();
  69. }
  70. }
  71. getIndex () {
  72. return this.state.index !== null ? this.state.index : this.props.index;
  73. }
  74. toggleNavigation = () => {
  75. this.setState(prevState => ({
  76. navigationHidden: !prevState.navigationHidden,
  77. }));
  78. };
  79. render () {
  80. const { media, intl, onClose } = this.props;
  81. const { navigationHidden } = this.state;
  82. const index = this.getIndex();
  83. let pagination = [];
  84. 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>;
  85. 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>;
  86. if (media.size > 1) {
  87. pagination = media.map((item, i) => {
  88. const classes = ['media-modal__button'];
  89. if (i === index) {
  90. classes.push('media-modal__button--active');
  91. }
  92. 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>);
  93. });
  94. }
  95. const content = media.map((image) => {
  96. const width = image.getIn(['meta', 'original', 'width']) || null;
  97. const height = image.getIn(['meta', 'original', 'height']) || null;
  98. if (image.get('type') === 'image') {
  99. return (
  100. <ImageLoader
  101. previewSrc={image.get('preview_url')}
  102. src={image.get('url')}
  103. width={width}
  104. height={height}
  105. alt={image.get('description')}
  106. key={image.get('url')}
  107. onClick={this.toggleNavigation}
  108. />
  109. );
  110. } else if (image.get('type') === 'video') {
  111. const { time } = this.props;
  112. return (
  113. <Video
  114. preview={image.get('preview_url')}
  115. src={image.get('url')}
  116. width={image.get('width')}
  117. height={image.get('height')}
  118. startTime={time || 0}
  119. onCloseVideo={onClose}
  120. detailed
  121. description={image.get('description')}
  122. key={image.get('url')}
  123. />
  124. );
  125. } else if (image.get('type') === 'gifv') {
  126. return (
  127. <ExtendedVideoPlayer
  128. src={image.get('url')}
  129. muted
  130. controls={false}
  131. width={width}
  132. height={height}
  133. key={image.get('preview_url')}
  134. alt={image.get('description')}
  135. onClick={this.toggleNavigation}
  136. />
  137. );
  138. }
  139. return null;
  140. }).toArray();
  141. // you can't use 100vh, because the viewport height is taller
  142. // than the visible part of the document in some mobile
  143. // browsers when it's address bar is visible.
  144. // https://developers.google.com/web/updates/2016/12/url-bar-resizing
  145. const swipeableViewsStyle = {
  146. width: '100%',
  147. height: '100%',
  148. };
  149. const containerStyle = {
  150. alignItems: 'center', // center vertically
  151. };
  152. const navigationClassName = classNames('media-modal__navigation', {
  153. 'media-modal__navigation--hidden': navigationHidden,
  154. });
  155. return (
  156. <div className='modal-root__modal media-modal'>
  157. <div
  158. className='media-modal__closer'
  159. role='presentation'
  160. onClick={onClose}
  161. >
  162. <ReactSwipeableViews
  163. style={swipeableViewsStyle}
  164. containerStyle={containerStyle}
  165. onChangeIndex={this.handleSwipe}
  166. onSwitching={this.handleSwitching}
  167. index={index}
  168. >
  169. {content}
  170. </ReactSwipeableViews>
  171. </div>
  172. <div className={navigationClassName}>
  173. <IconButton className='media-modal__close' title={intl.formatMessage(messages.close)} icon='times' onClick={onClose} size={40} />
  174. {leftNav}
  175. {rightNav}
  176. <ul className='media-modal__pagination'>
  177. {pagination}
  178. </ul>
  179. </div>
  180. </div>
  181. );
  182. }
  183. }