闭社主体 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.

107 lines
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.props.media.size + 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. let leftNav, rightNav, content;
  56. leftNav = rightNav = content = '';
  57. if (media.size > 1) {
  58. 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>;
  59. 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>;
  60. }
  61. content = media.map((image) => {
  62. const width = image.getIn(['meta', 'original', 'width']) || null;
  63. const height = image.getIn(['meta', 'original', 'height']) || null;
  64. if (image.get('type') === 'image') {
  65. return <ImageLoader previewSrc={image.get('preview_url')} src={image.get('url')} width={width} height={height} key={image.get('preview_url')} />;
  66. } else if (image.get('type') === 'gifv') {
  67. return <ExtendedVideoPlayer src={image.get('url')} muted controls={false} width={width} height={height} key={image.get('preview_url')} />;
  68. }
  69. return null;
  70. }).toArray();
  71. return (
  72. <div className='modal-root__modal media-modal'>
  73. {leftNav}
  74. <div className='media-modal__content'>
  75. <IconButton className='media-modal__close' title={intl.formatMessage(messages.close)} icon='times' onClick={onClose} size={16} />
  76. <ReactSwipeableViews onChangeIndex={this.handleSwipe} index={index} animateHeight>
  77. {content}
  78. </ReactSwipeableViews>
  79. </div>
  80. {rightNav}
  81. </div>
  82. );
  83. }
  84. }