闭社主体 forked from https://github.com/tootsuite/mastodon
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.

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