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.
 
 
 
 

97 lines
2.9 KiB

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