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.

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