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.

99 lines
3.0 KiB

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