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.

186 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 // eslint-disable-line jsx-a11y/anchor-has-content
  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
  92. muted
  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. @injectIntl
  106. export default class MediaGallery extends React.PureComponent {
  107. static propTypes = {
  108. sensitive: PropTypes.bool,
  109. media: ImmutablePropTypes.list.isRequired,
  110. height: PropTypes.number.isRequired,
  111. onOpenMedia: PropTypes.func.isRequired,
  112. intl: PropTypes.object.isRequired,
  113. autoPlayGif: PropTypes.bool.isRequired,
  114. };
  115. state = {
  116. visible: !this.props.sensitive,
  117. };
  118. handleOpen = () => {
  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 ${this.state.visible ? 'spoiler-button--visible' : ''}`}>
  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. }