The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

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