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.

368 lines
11 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. import { autoPlayGif, cropImages, displayMedia, useBlurhash } from '../initial_state';
  10. import { debounce } from 'lodash';
  11. import Blurhash from 'mastodon/components/blurhash';
  12. const messages = defineMessages({
  13. toggle_visible: { id: 'media_gallery.toggle_visible', defaultMessage: '{number, plural, one {Hide image} other {Hide images}}' },
  14. });
  15. class Item extends React.PureComponent {
  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. displayWidth: PropTypes.number,
  23. visible: PropTypes.bool.isRequired,
  24. autoplay: PropTypes.bool,
  25. };
  26. static defaultProps = {
  27. standalone: false,
  28. index: 0,
  29. size: 1,
  30. };
  31. state = {
  32. loaded: false,
  33. };
  34. handleMouseEnter = (e) => {
  35. if (this.hoverToPlay()) {
  36. e.target.play();
  37. }
  38. }
  39. handleMouseLeave = (e) => {
  40. if (this.hoverToPlay()) {
  41. e.target.pause();
  42. e.target.currentTime = 0;
  43. }
  44. }
  45. getAutoPlay() {
  46. return this.props.autoplay || autoPlayGif;
  47. }
  48. hoverToPlay () {
  49. const { attachment } = this.props;
  50. return !this.getAutoPlay() && attachment.get('type') === 'gifv';
  51. }
  52. handleClick = (e) => {
  53. const { index, onClick } = this.props;
  54. if (e.button === 0 && !(e.ctrlKey || e.metaKey)) {
  55. if (this.hoverToPlay()) {
  56. e.target.pause();
  57. e.target.currentTime = 0;
  58. }
  59. e.preventDefault();
  60. onClick(index);
  61. }
  62. e.stopPropagation();
  63. }
  64. handleImageLoad = () => {
  65. this.setState({ loaded: true });
  66. }
  67. render () {
  68. const { attachment, index, size, standalone, displayWidth, visible } = this.props;
  69. let width = 50;
  70. let height = 100;
  71. let top = 'auto';
  72. let left = 'auto';
  73. let bottom = 'auto';
  74. let right = 'auto';
  75. if (size === 1) {
  76. width = 100;
  77. }
  78. if (size === 4 || (size === 3 && index > 0)) {
  79. height = 50;
  80. }
  81. if (size === 2) {
  82. if (index === 0) {
  83. right = '2px';
  84. } else {
  85. left = '2px';
  86. }
  87. } else if (size === 3) {
  88. if (index === 0) {
  89. right = '2px';
  90. } else if (index > 0) {
  91. left = '2px';
  92. }
  93. if (index === 1) {
  94. bottom = '2px';
  95. } else if (index > 1) {
  96. top = '2px';
  97. }
  98. } else if (size === 4) {
  99. if (index === 0 || index === 2) {
  100. right = '2px';
  101. }
  102. if (index === 1 || index === 3) {
  103. left = '2px';
  104. }
  105. if (index < 2) {
  106. bottom = '2px';
  107. } else {
  108. top = '2px';
  109. }
  110. }
  111. let thumbnail = '';
  112. if (attachment.get('type') === 'unknown') {
  113. return (
  114. <div className={classNames('media-gallery__item', { standalone })} key={attachment.get('id')} style={{ left: left, top: top, right: right, bottom: bottom, width: `${width}%`, height: `${height}%` }}>
  115. <a className='media-gallery__item-thumbnail' href={attachment.get('remote_url') || attachment.get('url')} style={{ cursor: 'pointer' }} title={attachment.get('description')} target='_blank' rel='noopener noreferrer'>
  116. <Blurhash
  117. hash={attachment.get('blurhash')}
  118. className='media-gallery__preview'
  119. dummy={!useBlurhash}
  120. />
  121. </a>
  122. </div>
  123. );
  124. } else if (attachment.get('type') === 'image') {
  125. const previewUrl = attachment.get('preview_url');
  126. const previewWidth = attachment.getIn(['meta', 'small', 'width']);
  127. const originalUrl = attachment.get('url');
  128. const originalWidth = attachment.getIn(['meta', 'original', 'width']);
  129. const hasSize = typeof originalWidth === 'number' && typeof previewWidth === 'number';
  130. const srcSet = hasSize ? `${originalUrl} ${originalWidth}w, ${previewUrl} ${previewWidth}w` : null;
  131. const sizes = hasSize && (displayWidth > 0) ? `${displayWidth * (width / 100)}px` : null;
  132. const focusX = attachment.getIn(['meta', 'focus', 'x']) || 0;
  133. const focusY = attachment.getIn(['meta', 'focus', 'y']) || 0;
  134. const x = ((focusX / 2) + .5) * 100;
  135. const y = ((focusY / -2) + .5) * 100;
  136. thumbnail = (
  137. <a
  138. className='media-gallery__item-thumbnail'
  139. href={attachment.get('remote_url') || originalUrl}
  140. onClick={this.handleClick}
  141. target='_blank'
  142. rel='noopener noreferrer'
  143. >
  144. <img
  145. src={previewUrl}
  146. srcSet={srcSet}
  147. sizes={sizes}
  148. alt={attachment.get('description')}
  149. title={attachment.get('description')}
  150. style={{ objectPosition: `${x}% ${y}%` }}
  151. onLoad={this.handleImageLoad}
  152. />
  153. </a>
  154. );
  155. } else if (attachment.get('type') === 'gifv') {
  156. const autoPlay = !isIOS() && this.getAutoPlay();
  157. thumbnail = (
  158. <div className={classNames('media-gallery__gifv', { autoplay: autoPlay })}>
  159. <video
  160. className='media-gallery__item-gifv-thumbnail'
  161. aria-label={attachment.get('description')}
  162. title={attachment.get('description')}
  163. role='application'
  164. src={attachment.get('url')}
  165. onClick={this.handleClick}
  166. onMouseEnter={this.handleMouseEnter}
  167. onMouseLeave={this.handleMouseLeave}
  168. autoPlay={autoPlay}
  169. loop
  170. muted
  171. />
  172. <span className='media-gallery__gifv__label'>GIF</span>
  173. </div>
  174. );
  175. }
  176. return (
  177. <div className={classNames('media-gallery__item', { standalone })} key={attachment.get('id')} style={{ left: left, top: top, right: right, bottom: bottom, width: `${width}%`, height: `${height}%` }}>
  178. <Blurhash
  179. hash={attachment.get('blurhash')}
  180. dummy={!useBlurhash}
  181. className={classNames('media-gallery__preview', {
  182. 'media-gallery__preview--hidden': visible && this.state.loaded,
  183. })}
  184. />
  185. {visible && thumbnail}
  186. </div>
  187. );
  188. }
  189. }
  190. export default @injectIntl
  191. class MediaGallery extends React.PureComponent {
  192. static propTypes = {
  193. sensitive: PropTypes.bool,
  194. standalone: PropTypes.bool,
  195. media: ImmutablePropTypes.list.isRequired,
  196. size: PropTypes.object,
  197. height: PropTypes.number.isRequired,
  198. onOpenMedia: PropTypes.func.isRequired,
  199. intl: PropTypes.object.isRequired,
  200. defaultWidth: PropTypes.number,
  201. cacheWidth: PropTypes.func,
  202. visible: PropTypes.bool,
  203. autoplay: PropTypes.bool,
  204. onToggleVisibility: PropTypes.func,
  205. };
  206. static defaultProps = {
  207. standalone: false,
  208. };
  209. state = {
  210. visible: this.props.visible !== undefined ? this.props.visible : (displayMedia !== 'hide_all' && !this.props.sensitive || displayMedia === 'show_all'),
  211. width: this.props.defaultWidth,
  212. };
  213. componentDidMount () {
  214. window.addEventListener('resize', this.handleResize, { passive: true });
  215. }
  216. componentWillUnmount () {
  217. window.removeEventListener('resize', this.handleResize);
  218. }
  219. componentWillReceiveProps (nextProps) {
  220. if (!is(nextProps.media, this.props.media) && nextProps.visible === undefined) {
  221. this.setState({ visible: displayMedia !== 'hide_all' && !nextProps.sensitive || displayMedia === 'show_all' });
  222. } else if (!is(nextProps.visible, this.props.visible) && nextProps.visible !== undefined) {
  223. this.setState({ visible: nextProps.visible });
  224. }
  225. }
  226. handleResize = debounce(() => {
  227. if (this.node) {
  228. this._setDimensions();
  229. }
  230. }, 250, {
  231. trailing: true,
  232. });
  233. handleOpen = () => {
  234. if (this.props.onToggleVisibility) {
  235. this.props.onToggleVisibility();
  236. } else {
  237. this.setState({ visible: !this.state.visible });
  238. }
  239. }
  240. handleClick = (index) => {
  241. this.props.onOpenMedia(this.props.media, index);
  242. }
  243. handleRef = c => {
  244. this.node = c;
  245. if (this.node) {
  246. this._setDimensions();
  247. }
  248. }
  249. _setDimensions () {
  250. const width = this.node.offsetWidth;
  251. // offsetWidth triggers a layout, so only calculate when we need to
  252. if (this.props.cacheWidth) {
  253. this.props.cacheWidth(width);
  254. }
  255. this.setState({
  256. width: width,
  257. });
  258. }
  259. isFullSizeEligible() {
  260. const { media } = this.props;
  261. return media.size === 1 && media.getIn([0, 'meta', 'small', 'aspect']);
  262. }
  263. render () {
  264. const { media, intl, sensitive, height, defaultWidth, standalone, autoplay } = this.props;
  265. const { visible } = this.state;
  266. const width = this.state.width || defaultWidth;
  267. let children, spoilerButton;
  268. const style = {};
  269. if (this.isFullSizeEligible() && (standalone || !cropImages)) {
  270. if (width) {
  271. style.height = width / this.props.media.getIn([0, 'meta', 'small', 'aspect']);
  272. }
  273. } else if (width) {
  274. style.height = width / (16/9);
  275. } else {
  276. style.height = height;
  277. }
  278. const size = media.take(4).size;
  279. const uncached = media.every(attachment => attachment.get('type') === 'unknown');
  280. if (standalone && this.isFullSizeEligible()) {
  281. children = <Item standalone autoplay={autoplay} onClick={this.handleClick} attachment={media.get(0)} displayWidth={width} visible={visible} />;
  282. } else {
  283. children = media.take(4).map((attachment, i) => <Item key={attachment.get('id')} autoplay={autoplay} onClick={this.handleClick} attachment={attachment} index={i} size={size} displayWidth={width} visible={visible || uncached} />);
  284. }
  285. if (uncached) {
  286. spoilerButton = (
  287. <button type='button' disabled className='spoiler-button__overlay'>
  288. <span className='spoiler-button__overlay__label'><FormattedMessage id='status.uncached_media_warning' defaultMessage='Not available' /></span>
  289. </button>
  290. );
  291. } else if (visible) {
  292. spoilerButton = <IconButton title={intl.formatMessage(messages.toggle_visible, { number: size })} icon='eye-slash' overlay onClick={this.handleOpen} />;
  293. } else {
  294. spoilerButton = (
  295. <button type='button' onClick={this.handleOpen} className='spoiler-button__overlay'>
  296. <span className='spoiler-button__overlay__label'>{sensitive ? <FormattedMessage id='status.sensitive_warning' defaultMessage='Sensitive content' /> : <FormattedMessage id='status.media_hidden' defaultMessage='Media hidden' />}</span>
  297. </button>
  298. );
  299. }
  300. return (
  301. <div className='media-gallery' style={style} ref={this.handleRef}>
  302. <div className={classNames('spoiler-button', { 'spoiler-button--minified': visible && !uncached, 'spoiler-button--click-thru': uncached })}>
  303. {spoilerButton}
  304. </div>
  305. {children}
  306. </div>
  307. );
  308. }
  309. }