The code powering m.abunchtell.com https://m.abunchtell.com
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

136 lines
3.5 KiB

  1. import LoadingIndicator from '../../../components/loading_indicator';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import PropTypes from 'prop-types';
  4. import ExtendedVideoPlayer from '../../../components/extended_video_player';
  5. import ImageLoader from 'react-imageloader';
  6. import { defineMessages, injectIntl } from 'react-intl';
  7. import IconButton from '../../../components/icon_button';
  8. const messages = defineMessages({
  9. close: { id: 'lightbox.close', defaultMessage: 'Close' }
  10. });
  11. const leftNavStyle = {
  12. position: 'absolute',
  13. background: 'rgba(0, 0, 0, 0.5)',
  14. padding: '30px 15px',
  15. cursor: 'pointer',
  16. fontSize: '24px',
  17. top: '0',
  18. left: '-61px',
  19. boxSizing: 'border-box',
  20. height: '100%',
  21. display: 'flex',
  22. alignItems: 'center'
  23. };
  24. const rightNavStyle = {
  25. position: 'absolute',
  26. background: 'rgba(0, 0, 0, 0.5)',
  27. padding: '30px 15px',
  28. cursor: 'pointer',
  29. fontSize: '24px',
  30. top: '0',
  31. right: '-61px',
  32. boxSizing: 'border-box',
  33. height: '100%',
  34. display: 'flex',
  35. alignItems: 'center'
  36. };
  37. const closeStyle = {
  38. position: 'absolute',
  39. top: '4px',
  40. right: '4px'
  41. };
  42. class MediaModal extends React.PureComponent {
  43. constructor (props, context) {
  44. super(props, context);
  45. this.state = {
  46. index: null
  47. };
  48. this.handleNextClick = this.handleNextClick.bind(this);
  49. this.handlePrevClick = this.handlePrevClick.bind(this);
  50. this.handleKeyUp = this.handleKeyUp.bind(this);
  51. }
  52. handleNextClick () {
  53. this.setState({ index: (this.getIndex() + 1) % this.props.media.size});
  54. }
  55. handlePrevClick () {
  56. this.setState({ index: (this.getIndex() - 1) % this.props.media.size});
  57. }
  58. handleKeyUp (e) {
  59. switch(e.key) {
  60. case 'ArrowLeft':
  61. this.handlePrevClick();
  62. break;
  63. case 'ArrowRight':
  64. this.handleNextClick();
  65. break;
  66. }
  67. }
  68. componentDidMount () {
  69. window.addEventListener('keyup', this.handleKeyUp, false);
  70. }
  71. componentWillUnmount () {
  72. window.removeEventListener('keyup', this.handleKeyUp);
  73. }
  74. getIndex () {
  75. return this.state.index !== null ? this.state.index : this.props.index;
  76. }
  77. render () {
  78. const { media, intl, onClose } = this.props;
  79. const index = this.getIndex();
  80. const attachment = media.get(index);
  81. const url = attachment.get('url');
  82. let leftNav, rightNav, content;
  83. leftNav = rightNav = content = '';
  84. if (media.size > 1) {
  85. leftNav = <div role='button' tabIndex='0' style={leftNavStyle} className='modal-container__nav' onClick={this.handlePrevClick}><i className='fa fa-fw fa-chevron-left' /></div>;
  86. rightNav = <div role='button' tabIndex='0' style={rightNavStyle} className='modal-container__nav' onClick={this.handleNextClick}><i className='fa fa-fw fa-chevron-right' /></div>;
  87. }
  88. if (attachment.get('type') === 'image') {
  89. content = <ImageLoader src={url} imgProps={{ style: { display: 'block' } }} />;
  90. } else if (attachment.get('type') === 'gifv') {
  91. content = <ExtendedVideoPlayer src={url} muted={true} controls={false} />;
  92. }
  93. return (
  94. <div className='modal-root__modal media-modal'>
  95. {leftNav}
  96. <div>
  97. <IconButton title={intl.formatMessage(messages.close)} icon='times' onClick={onClose} size={16} style={closeStyle} />
  98. {content}
  99. </div>
  100. {rightNav}
  101. </div>
  102. );
  103. }
  104. }
  105. MediaModal.propTypes = {
  106. media: ImmutablePropTypes.list.isRequired,
  107. index: PropTypes.number.isRequired,
  108. onClose: PropTypes.func.isRequired,
  109. intl: PropTypes.object.isRequired
  110. };
  111. export default injectIntl(MediaModal);