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.

100 lines
3.1 KiB

  1. import React from 'react';
  2. import ReactSwipeable from 'react-swipeable';
  3. import LoadingIndicator from '../../../components/loading_indicator';
  4. import ImmutablePropTypes from 'react-immutable-proptypes';
  5. import PropTypes from 'prop-types';
  6. import ExtendedVideoPlayer from '../../../components/extended_video_player';
  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. });
  14. class MediaModal extends ImmutablePureComponent {
  15. static propTypes = {
  16. media: ImmutablePropTypes.list.isRequired,
  17. index: PropTypes.number.isRequired,
  18. onClose: PropTypes.func.isRequired,
  19. intl: PropTypes.object.isRequired,
  20. };
  21. state = {
  22. index: null,
  23. };
  24. handleNextClick = () => {
  25. this.setState({ index: (this.getIndex() + 1) % this.props.media.size });
  26. }
  27. handlePrevClick = () => {
  28. this.setState({ index: (this.getIndex() - 1) % this.props.media.size });
  29. }
  30. handleKeyUp = (e) => {
  31. switch(e.key) {
  32. case 'ArrowLeft':
  33. this.handlePrevClick();
  34. break;
  35. case 'ArrowRight':
  36. this.handleNextClick();
  37. break;
  38. }
  39. }
  40. componentDidMount () {
  41. window.addEventListener('keyup', this.handleKeyUp, false);
  42. }
  43. componentWillUnmount () {
  44. window.removeEventListener('keyup', this.handleKeyUp);
  45. }
  46. getIndex () {
  47. return this.state.index !== null ? this.state.index : this.props.index;
  48. }
  49. render () {
  50. const { media, intl, onClose } = this.props;
  51. const index = this.getIndex();
  52. const attachment = media.get(index);
  53. const url = attachment.get('url');
  54. let leftNav, rightNav, content;
  55. leftNav = rightNav = content = '';
  56. if (media.size > 1) {
  57. leftNav = <div role='button' tabIndex='0' className='modal-container__nav modal-container__nav--left' onClick={this.handlePrevClick}><i className='fa fa-fw fa-chevron-left' /></div>;
  58. rightNav = <div role='button' tabIndex='0' className='modal-container__nav modal-container__nav--right' onClick={this.handleNextClick}><i className='fa fa-fw fa-chevron-right' /></div>;
  59. }
  60. if (attachment.get('type') === 'image') {
  61. content = <ImageLoader previewSrc={attachment.get('preview_url')} src={url} width={attachment.getIn(['meta', 'original', 'width'])} height={attachment.getIn(['meta', 'original', 'height'])} />;
  62. } else if (attachment.get('type') === 'gifv') {
  63. content = <ExtendedVideoPlayer src={url} muted controls={false} />;
  64. }
  65. return (
  66. <div className='modal-root__modal media-modal'>
  67. {leftNav}
  68. <div className='media-modal__content'>
  69. <IconButton className='media-modal__close' title={intl.formatMessage(messages.close)} icon='times' onClick={onClose} size={16} />
  70. <ReactSwipeable onSwipedRight={this.handlePrevClick} onSwipedLeft={this.handleNextClick}>
  71. {content}
  72. </ReactSwipeable>
  73. </div>
  74. {rightNav}
  75. </div>
  76. );
  77. }
  78. }
  79. export default injectIntl(MediaModal);