The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

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