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.

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