The code powering m.abunchtell.com https://m.abunchtell.com
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

108 linhas
3.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 '../../../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. @injectIntl
  14. export default 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. handleSwipe = (index) => {
  25. this.setState({ index: (index) % this.props.media.size });
  26. }
  27. handleNextClick = () => {
  28. this.setState({ index: (this.getIndex() + 1) % this.props.media.size });
  29. }
  30. handlePrevClick = () => {
  31. this.setState({ index: (this.getIndex() - 1) % this.props.media.size });
  32. }
  33. handleKeyUp = (e) => {
  34. switch(e.key) {
  35. case 'ArrowLeft':
  36. this.handlePrevClick();
  37. break;
  38. case 'ArrowRight':
  39. this.handleNextClick();
  40. break;
  41. }
  42. }
  43. componentDidMount () {
  44. window.addEventListener('keyup', this.handleKeyUp, false);
  45. }
  46. componentWillUnmount () {
  47. window.removeEventListener('keyup', this.handleKeyUp);
  48. }
  49. getIndex () {
  50. return this.state.index !== null ? this.state.index : this.props.index;
  51. }
  52. render () {
  53. const { media, intl, onClose } = this.props;
  54. const index = this.getIndex();
  55. const attachment = media.get(index);
  56. const url = attachment.get('url');
  57. let leftNav, rightNav, content;
  58. leftNav = rightNav = content = '';
  59. if (media.size > 1) {
  60. 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>;
  61. 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>;
  62. }
  63. if (attachment.get('type') === 'image') {
  64. content = media.map((image) => {
  65. const width = image.getIn(['meta', 'original', 'width']) || null;
  66. const height = image.getIn(['meta', 'original', 'height']) || null;
  67. return <ImageLoader previewSrc={image.get('preview_url')} src={image.get('url')} width={width} height={height} key={image.get('preview_url')} />;
  68. }).toArray();
  69. } else if (attachment.get('type') === 'gifv') {
  70. content = <ExtendedVideoPlayer src={url} muted controls={false} />;
  71. }
  72. return (
  73. <div className='modal-root__modal media-modal'>
  74. {leftNav}
  75. <div className='media-modal__content'>
  76. <IconButton className='media-modal__close' title={intl.formatMessage(messages.close)} icon='times' onClick={onClose} size={16} />
  77. <ReactSwipeableViews onChangeIndex={this.handleSwipe} index={index}>
  78. {content}
  79. </ReactSwipeableViews>
  80. </div>
  81. {rightNav}
  82. </div>
  83. );
  84. }
  85. }