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.

281 lines
7.6 KiB

  1. import React from 'react';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import PropTypes from 'prop-types';
  4. import { is } from 'immutable';
  5. import IconButton from './icon_button';
  6. import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
  7. import { isIOS } from '../is_mobile';
  8. import classNames from 'classnames';
  9. const messages = defineMessages({
  10. toggle_visible: { id: 'media_gallery.toggle_visible', defaultMessage: 'Toggle visibility' },
  11. });
  12. class Item extends React.PureComponent {
  13. static contextTypes = {
  14. router: PropTypes.object,
  15. };
  16. static propTypes = {
  17. attachment: ImmutablePropTypes.map.isRequired,
  18. standalone: PropTypes.bool,
  19. index: PropTypes.number.isRequired,
  20. size: PropTypes.number.isRequired,
  21. onClick: PropTypes.func.isRequired,
  22. autoPlayGif: PropTypes.bool,
  23. };
  24. static defaultProps = {
  25. autoPlayGif: false,
  26. standalone: false,
  27. index: 0,
  28. size: 1,
  29. };
  30. handleMouseEnter = (e) => {
  31. if (this.hoverToPlay()) {
  32. e.target.play();
  33. }
  34. }
  35. handleMouseLeave = (e) => {
  36. if (this.hoverToPlay()) {
  37. e.target.pause();
  38. e.target.currentTime = 0;
  39. }
  40. }
  41. hoverToPlay () {
  42. const { attachment, autoPlayGif } = this.props;
  43. return !autoPlayGif && attachment.get('type') === 'gifv';
  44. }
  45. handleClick = (e) => {
  46. const { index, onClick } = this.props;
  47. if (this.context.router && e.button === 0) {
  48. e.preventDefault();
  49. onClick(index);
  50. }
  51. e.stopPropagation();
  52. }
  53. render () {
  54. const { attachment, index, size, standalone } = this.props;
  55. let width = 50;
  56. let height = 100;
  57. let top = 'auto';
  58. let left = 'auto';
  59. let bottom = 'auto';
  60. let right = 'auto';
  61. if (size === 1) {
  62. width = 100;
  63. }
  64. if (size === 4 || (size === 3 && index > 0)) {
  65. height = 50;
  66. }
  67. if (size === 2) {
  68. if (index === 0) {
  69. right = '2px';
  70. } else {
  71. left = '2px';
  72. }
  73. } else if (size === 3) {
  74. if (index === 0) {
  75. right = '2px';
  76. } else if (index > 0) {
  77. left = '2px';
  78. }
  79. if (index === 1) {
  80. bottom = '2px';
  81. } else if (index > 1) {
  82. top = '2px';
  83. }
  84. } else if (size === 4) {
  85. if (index === 0 || index === 2) {
  86. right = '2px';
  87. }
  88. if (index === 1 || index === 3) {
  89. left = '2px';
  90. }
  91. if (index < 2) {
  92. bottom = '2px';
  93. } else {
  94. top = '2px';
  95. }
  96. }
  97. let thumbnail = '';
  98. if (attachment.get('type') === 'image') {
  99. const previewUrl = attachment.get('preview_url');
  100. const previewWidth = attachment.getIn(['meta', 'small', 'width']);
  101. const originalUrl = attachment.get('url');
  102. const originalWidth = attachment.getIn(['meta', 'original', 'width']);
  103. const hasSize = typeof originalWidth === 'number' && typeof previewWidth === 'number';
  104. const srcSet = hasSize ? `${originalUrl} ${originalWidth}w, ${previewUrl} ${previewWidth}w` : null;
  105. const sizes = hasSize ? `(min-width: 1025px) ${320 * (width / 100)}px, ${width}vw` : null;
  106. thumbnail = (
  107. <a
  108. className='media-gallery__item-thumbnail'
  109. href={attachment.get('remote_url') || originalUrl}
  110. onClick={this.handleClick}
  111. target='_blank'
  112. >
  113. <img src={previewUrl} srcSet={srcSet} sizes={sizes} alt={attachment.get('description')} title={attachment.get('description')} />
  114. </a>
  115. );
  116. } else if (attachment.get('type') === 'gifv') {
  117. const autoPlay = !isIOS() && this.props.autoPlayGif;
  118. thumbnail = (
  119. <div className={classNames('media-gallery__gifv', { autoplay: autoPlay })}>
  120. <video
  121. className='media-gallery__item-gifv-thumbnail'
  122. aria-label={attachment.get('description')}
  123. role='application'
  124. src={attachment.get('url')}
  125. onClick={this.handleClick}
  126. onMouseEnter={this.handleMouseEnter}
  127. onMouseLeave={this.handleMouseLeave}
  128. autoPlay={autoPlay}
  129. loop
  130. muted
  131. />
  132. <span className='media-gallery__gifv__label'>GIF</span>
  133. </div>
  134. );
  135. }
  136. return (
  137. <div className={classNames('media-gallery__item', { standalone })} key={attachment.get('id')} style={{ left: left, top: top, right: right, bottom: bottom, width: `${width}%`, height: `${height}%` }}>
  138. {thumbnail}
  139. </div>
  140. );
  141. }
  142. }
  143. @injectIntl
  144. export default class MediaGallery extends React.PureComponent {
  145. static propTypes = {
  146. sensitive: PropTypes.bool,
  147. standalone: PropTypes.bool,
  148. media: ImmutablePropTypes.list.isRequired,
  149. size: PropTypes.object,
  150. height: PropTypes.number.isRequired,
  151. onOpenMedia: PropTypes.func.isRequired,
  152. intl: PropTypes.object.isRequired,
  153. autoPlayGif: PropTypes.bool,
  154. };
  155. static defaultProps = {
  156. autoPlayGif: false,
  157. standalone: false,
  158. };
  159. state = {
  160. visible: !this.props.sensitive,
  161. };
  162. componentWillReceiveProps (nextProps) {
  163. if (!is(nextProps.media, this.props.media)) {
  164. this.setState({ visible: !nextProps.sensitive });
  165. }
  166. }
  167. handleOpen = () => {
  168. this.setState({ visible: !this.state.visible });
  169. }
  170. handleClick = (index) => {
  171. this.props.onOpenMedia(this.props.media, index);
  172. }
  173. handleRef = (node) => {
  174. if (node && this.isStandaloneEligible()) {
  175. // offsetWidth triggers a layout, so only calculate when we need to
  176. this.setState({
  177. width: node.offsetWidth,
  178. });
  179. }
  180. }
  181. isStandaloneEligible() {
  182. const { media, standalone } = this.props;
  183. return standalone && media.size === 1 && media.getIn([0, 'meta', 'small', 'aspect']);
  184. }
  185. render () {
  186. const { media, intl, sensitive, height } = this.props;
  187. const { width, visible } = this.state;
  188. let children;
  189. const style = {};
  190. if (this.isStandaloneEligible()) {
  191. if (!visible && width) {
  192. // only need to forcibly set the height in "sensitive" mode
  193. style.height = width / this.props.media.getIn([0, 'meta', 'small', 'aspect']);
  194. } else {
  195. // layout automatically, using image's natural aspect ratio
  196. style.height = '';
  197. }
  198. } else {
  199. // crop the image
  200. style.height = height;
  201. }
  202. if (!visible) {
  203. let warning;
  204. if (sensitive) {
  205. warning = <FormattedMessage id='status.sensitive_warning' defaultMessage='Sensitive content' />;
  206. } else {
  207. warning = <FormattedMessage id='status.media_hidden' defaultMessage='Media hidden' />;
  208. }
  209. children = (
  210. <button className='media-spoiler' onClick={this.handleOpen} style={style} ref={this.handleRef}>
  211. <span className='media-spoiler__warning'>{warning}</span>
  212. <span className='media-spoiler__trigger'><FormattedMessage id='status.sensitive_toggle' defaultMessage='Click to view' /></span>
  213. </button>
  214. );
  215. } else {
  216. const size = media.take(4).size;
  217. if (this.isStandaloneEligible()) {
  218. children = <Item standalone onClick={this.handleClick} attachment={media.get(0)} autoPlayGif={this.props.autoPlayGif} />;
  219. } else {
  220. 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} />);
  221. }
  222. }
  223. return (
  224. <div className='media-gallery' style={style}>
  225. <div className={classNames('spoiler-button', { 'spoiler-button--visible': visible })}>
  226. <IconButton title={intl.formatMessage(messages.toggle_visible)} icon={visible ? 'eye' : 'eye-slash'} overlay onClick={this.handleOpen} />
  227. </div>
  228. {children}
  229. </div>
  230. );
  231. }
  232. }