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.

126 lines
4.3 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 ExtendedVideoPlayer from 'flavours/glitch/components/extended_video_player';
  6. import { defineMessages, injectIntl } from 'react-intl';
  7. import IconButton from 'flavours/glitch/components/icon_button';
  8. import ImmutablePureComponent from 'react-immutable-pure-component';
  9. import ImageLoader from './image_loader';
  10. const messages = defineMessages({
  11. close: { id: 'lightbox.close', defaultMessage: 'Close' },
  12. previous: { id: 'lightbox.previous', defaultMessage: 'Previous' },
  13. next: { id: 'lightbox.next', defaultMessage: 'Next' },
  14. });
  15. @injectIntl
  16. export default class MediaModal extends ImmutablePureComponent {
  17. static propTypes = {
  18. media: ImmutablePropTypes.list.isRequired,
  19. index: PropTypes.number.isRequired,
  20. onClose: PropTypes.func.isRequired,
  21. intl: PropTypes.object.isRequired,
  22. };
  23. state = {
  24. index: null,
  25. };
  26. handleSwipe = (index) => {
  27. this.setState({ index: index % this.props.media.size });
  28. }
  29. handleNextClick = () => {
  30. this.setState({ index: (this.getIndex() + 1) % this.props.media.size });
  31. }
  32. handlePrevClick = () => {
  33. this.setState({ index: (this.props.media.size + this.getIndex() - 1) % this.props.media.size });
  34. }
  35. handleChangeIndex = (e) => {
  36. const index = Number(e.currentTarget.getAttribute('data-index'));
  37. this.setState({ index: index % this.props.media.size });
  38. }
  39. handleKeyUp = (e) => {
  40. switch(e.key) {
  41. case 'ArrowLeft':
  42. this.handlePrevClick();
  43. break;
  44. case 'ArrowRight':
  45. this.handleNextClick();
  46. break;
  47. }
  48. }
  49. componentDidMount () {
  50. window.addEventListener('keyup', this.handleKeyUp, false);
  51. }
  52. componentWillUnmount () {
  53. window.removeEventListener('keyup', this.handleKeyUp);
  54. }
  55. getIndex () {
  56. return this.state.index !== null ? this.state.index : this.props.index;
  57. }
  58. render () {
  59. const { media, intl, onClose } = this.props;
  60. const index = this.getIndex();
  61. let pagination = [];
  62. const leftNav = media.size > 1 && <button tabIndex='0' className='modal-container__nav modal-container__nav--left' onClick={this.handlePrevClick} aria-label={intl.formatMessage(messages.previous)}><i className='fa fa-fw fa-chevron-left' /></button>;
  63. const rightNav = media.size > 1 && <button tabIndex='0' className='modal-container__nav modal-container__nav--right' onClick={this.handleNextClick} aria-label={intl.formatMessage(messages.next)}><i className='fa fa-fw fa-chevron-right' /></button>;
  64. if (media.size > 1) {
  65. pagination = media.map((item, i) => {
  66. const classes = ['media-modal__button'];
  67. if (i === index) {
  68. classes.push('media-modal__button--active');
  69. }
  70. 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>);
  71. });
  72. }
  73. const content = media.map((image) => {
  74. const width = image.getIn(['meta', 'original', 'width']) || null;
  75. const height = image.getIn(['meta', 'original', 'height']) || null;
  76. if (image.get('type') === 'image') {
  77. return <ImageLoader previewSrc={image.get('preview_url')} src={image.get('url')} width={width} height={height} alt={image.get('description')} key={image.get('preview_url')} />;
  78. } else if (image.get('type') === 'gifv') {
  79. return <ExtendedVideoPlayer src={image.get('url')} muted controls={false} width={width} height={height} key={image.get('preview_url')} alt={image.get('description')} />;
  80. }
  81. return null;
  82. }).toArray();
  83. const containerStyle = {
  84. alignItems: 'center', // center vertically
  85. };
  86. return (
  87. <div className='modal-root__modal media-modal'>
  88. {leftNav}
  89. <div className='media-modal__content'>
  90. <IconButton className='media-modal__close' title={intl.formatMessage(messages.close)} icon='times' onClick={onClose} size={16} />
  91. <ReactSwipeableViews containerStyle={containerStyle} onChangeIndex={this.handleSwipe} index={index}>
  92. {content}
  93. </ReactSwipeableViews>
  94. </div>
  95. <ul className='media-modal__pagination'>
  96. {pagination}
  97. </ul>
  98. {rightNav}
  99. </div>
  100. );
  101. }
  102. }