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

  1. import React from 'react';
  2. import LoadingIndicator from '../../../components/loading_indicator';
  3. import ImmutablePropTypes from 'react-immutable-proptypes';
  4. import PropTypes from 'prop-types';
  5. import ExtendedVideoPlayer from '../../../components/extended_video_player';
  6. import ImageLoader from 'react-imageloader';
  7. import { defineMessages, injectIntl } from 'react-intl';
  8. import IconButton from '../../../components/icon_button';
  9. import ImmutablePureComponent from 'react-immutable-pure-component';
  10. const messages = defineMessages({
  11. close: { id: 'lightbox.close', defaultMessage: 'Close' }
  12. });
  13. class MediaModal extends ImmutablePureComponent {
  14. constructor (props, context) {
  15. super(props, context);
  16. this.state = {
  17. index: null
  18. };
  19. this.handleNextClick = this.handleNextClick.bind(this);
  20. this.handlePrevClick = this.handlePrevClick.bind(this);
  21. this.handleKeyUp = this.handleKeyUp.bind(this);
  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 src={url} imgProps={{ style: { display: 'block' } }} />;
  61. } else if (attachment.get('type') === 'gifv') {
  62. content = <ExtendedVideoPlayer src={url} muted={true} 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. {content}
  70. </div>
  71. {rightNav}
  72. </div>
  73. );
  74. }
  75. }
  76. MediaModal.propTypes = {
  77. media: ImmutablePropTypes.list.isRequired,
  78. index: PropTypes.number.isRequired,
  79. onClose: PropTypes.func.isRequired,
  80. intl: PropTypes.object.isRequired
  81. };
  82. export default injectIntl(MediaModal);