logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

media_modal.js (5686B)


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