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

187 lines
4.9 KiB

  1. import React from 'react';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import PropTypes from 'prop-types';
  4. import IconButton from './icon_button';
  5. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  6. import { isIOS } from '../is_mobile';
  7. const messages = defineMessages({
  8. toggle_visible: { id: 'media_gallery.toggle_visible', defaultMessage: 'Toggle visibility' }
  9. });
  10. class Item extends React.PureComponent {
  11. static propTypes = {
  12. attachment: ImmutablePropTypes.map.isRequired,
  13. index: PropTypes.number.isRequired,
  14. size: PropTypes.number.isRequired,
  15. onClick: PropTypes.func.isRequired,
  16. autoPlayGif: PropTypes.bool.isRequired
  17. };
  18. handleClick = (e) => {
  19. const { index, onClick } = this.props;
  20. if (e.button === 0) {
  21. e.preventDefault();
  22. onClick(index);
  23. }
  24. e.stopPropagation();
  25. }
  26. render () {
  27. const { attachment, index, size } = this.props;
  28. let width = 50;
  29. let height = 100;
  30. let top = 'auto';
  31. let left = 'auto';
  32. let bottom = 'auto';
  33. let right = 'auto';
  34. if (size === 1) {
  35. width = 100;
  36. }
  37. if (size === 4 || (size === 3 && index > 0)) {
  38. height = 50;
  39. }
  40. if (size === 2) {
  41. if (index === 0) {
  42. right = '2px';
  43. } else {
  44. left = '2px';
  45. }
  46. } else if (size === 3) {
  47. if (index === 0) {
  48. right = '2px';
  49. } else if (index > 0) {
  50. left = '2px';
  51. }
  52. if (index === 1) {
  53. bottom = '2px';
  54. } else if (index > 1) {
  55. top = '2px';
  56. }
  57. } else if (size === 4) {
  58. if (index === 0 || index === 2) {
  59. right = '2px';
  60. }
  61. if (index === 1 || index === 3) {
  62. left = '2px';
  63. }
  64. if (index < 2) {
  65. bottom = '2px';
  66. } else {
  67. top = '2px';
  68. }
  69. }
  70. let thumbnail = '';
  71. if (attachment.get('type') === 'image') {
  72. thumbnail = (
  73. <a
  74. className='media-gallery__item-thumbnail'
  75. href={attachment.get('remote_url') || attachment.get('url')}
  76. onClick={this.handleClick}
  77. target='_blank'
  78. style={{ backgroundImage: `url(${attachment.get('preview_url')})` }}
  79. />
  80. );
  81. } else if (attachment.get('type') === 'gifv') {
  82. const autoPlay = !isIOS() && this.props.autoPlayGif;
  83. thumbnail = (
  84. <div className={`media-gallery__gifv ${autoPlay ? 'autoplay' : ''}`}>
  85. <video
  86. className='media-gallery__item-gifv-thumbnail'
  87. role='application'
  88. src={attachment.get('url')}
  89. onClick={this.handleClick}
  90. autoPlay={autoPlay}
  91. loop={true}
  92. muted={true}
  93. />
  94. <span className='media-gallery__gifv__label'>GIF</span>
  95. </div>
  96. );
  97. }
  98. return (
  99. <div className='media-gallery__item' key={attachment.get('id')} style={{ left: left, top: top, right: right, bottom: bottom, width: `${width}%`, height: `${height}%` }}>
  100. {thumbnail}
  101. </div>
  102. );
  103. }
  104. }
  105. class MediaGallery extends React.PureComponent {
  106. static propTypes = {
  107. sensitive: PropTypes.bool,
  108. media: ImmutablePropTypes.list.isRequired,
  109. height: PropTypes.number.isRequired,
  110. onOpenMedia: PropTypes.func.isRequired,
  111. intl: PropTypes.object.isRequired,
  112. autoPlayGif: PropTypes.bool.isRequired
  113. };
  114. state = {
  115. visible: !this.props.sensitive
  116. };
  117. handleOpen = (e) => {
  118. this.setState({ visible: !this.state.visible });
  119. }
  120. handleClick = (index) => {
  121. this.props.onOpenMedia(this.props.media, index);
  122. }
  123. render () {
  124. const { media, intl, sensitive } = this.props;
  125. let children;
  126. if (!this.state.visible) {
  127. let warning;
  128. if (sensitive) {
  129. warning = <FormattedMessage id='status.sensitive_warning' defaultMessage='Sensitive content' />;
  130. } else {
  131. warning = <FormattedMessage id='status.media_hidden' defaultMessage='Media hidden' />;
  132. }
  133. children = (
  134. <div role='button' tabIndex='0' className='media-spoiler' onClick={this.handleOpen}>
  135. <span className='media-spoiler__warning'>{warning}</span>
  136. <span className='media-spoiler__trigger'><FormattedMessage id='status.sensitive_toggle' defaultMessage='Click to view' /></span>
  137. </div>
  138. );
  139. } else {
  140. const size = media.take(4).size;
  141. children = media.take(4).map((attachment, i) => <Item key={attachment.get('id')} onClick={this.handleClick} attachment={attachment} autoPlayGif={this.props.autoPlayGif} index={i} size={size} />);
  142. }
  143. return (
  144. <div className='media-gallery' style={{ height: `${this.props.height}px` }}>
  145. <div className={`spoiler-button ${this.state.visible ? 'spoiler-button--visible' : ''}`}>
  146. <IconButton title={intl.formatMessage(messages.toggle_visible)} icon={this.state.visible ? 'eye' : 'eye-slash'} overlay onClick={this.handleOpen} />
  147. </div>
  148. {children}
  149. </div>
  150. );
  151. }
  152. }
  153. export default injectIntl(MediaGallery);