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.

338 lines
9.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. import { autoPlayGif, displaySensitiveMedia } from '../initial_state';
  10. const messages = defineMessages({
  11. toggle_visible: { id: 'media_gallery.toggle_visible', defaultMessage: 'Toggle visibility' },
  12. });
  13. const shiftToPoint = (containerToImageRatio, containerSize, imageSize, focusSize, toMinus) => {
  14. const containerCenter = Math.floor(containerSize / 2);
  15. const focusFactor = (focusSize + 1) / 2;
  16. const scaledImage = Math.floor(imageSize / containerToImageRatio);
  17. let focus = Math.floor(focusFactor * scaledImage);
  18. if (toMinus) focus = scaledImage - focus;
  19. let focusOffset = focus - containerCenter;
  20. const remainder = scaledImage - focus;
  21. const containerRemainder = containerSize - containerCenter;
  22. if (remainder < containerRemainder) focusOffset -= containerRemainder - remainder;
  23. if (focusOffset < 0) focusOffset = 0;
  24. return (focusOffset * -100 / containerSize) + '%';
  25. };
  26. class Item extends React.PureComponent {
  27. static contextTypes = {
  28. router: PropTypes.object,
  29. };
  30. static propTypes = {
  31. attachment: ImmutablePropTypes.map.isRequired,
  32. standalone: PropTypes.bool,
  33. index: PropTypes.number.isRequired,
  34. size: PropTypes.number.isRequired,
  35. onClick: PropTypes.func.isRequired,
  36. containerWidth: PropTypes.number,
  37. containerHeight: PropTypes.number,
  38. };
  39. static defaultProps = {
  40. standalone: false,
  41. index: 0,
  42. size: 1,
  43. };
  44. handleMouseEnter = (e) => {
  45. if (this.hoverToPlay()) {
  46. e.target.play();
  47. }
  48. }
  49. handleMouseLeave = (e) => {
  50. if (this.hoverToPlay()) {
  51. e.target.pause();
  52. e.target.currentTime = 0;
  53. }
  54. }
  55. hoverToPlay () {
  56. const { attachment } = this.props;
  57. return !autoPlayGif && attachment.get('type') === 'gifv';
  58. }
  59. handleClick = (e) => {
  60. const { index, onClick } = this.props;
  61. if (this.context.router && e.button === 0) {
  62. e.preventDefault();
  63. onClick(index);
  64. }
  65. e.stopPropagation();
  66. }
  67. render () {
  68. const { attachment, index, size, standalone, containerWidth, containerHeight } = 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') === 'image') {
  113. const previewUrl = attachment.get('preview_url');
  114. const previewWidth = attachment.getIn(['meta', 'small', 'width']);
  115. const originalUrl = attachment.get('url');
  116. const originalWidth = attachment.getIn(['meta', 'original', 'width']);
  117. const originalHeight = attachment.getIn(['meta', 'original', 'height']);
  118. const hasSize = typeof originalWidth === 'number' && typeof previewWidth === 'number';
  119. const srcSet = hasSize ? `${originalUrl} ${originalWidth}w, ${previewUrl} ${previewWidth}w` : null;
  120. const sizes = hasSize ? `(min-width: 1025px) ${320 * (width / 100)}px, ${width}vw` : null;
  121. const focusX = attachment.getIn(['meta', 'focus', 'x']);
  122. const focusY = attachment.getIn(['meta', 'focus', 'y']);
  123. const imageStyle = {};
  124. if (originalWidth && originalHeight && containerWidth && containerHeight && focusX && focusY) {
  125. const widthRatio = originalWidth / (containerWidth * (width / 100));
  126. const heightRatio = originalHeight / (containerHeight * (height / 100));
  127. let hShift = 0;
  128. let vShift = 0;
  129. if (widthRatio > heightRatio) {
  130. hShift = shiftToPoint(heightRatio, (containerWidth * (width / 100)), originalWidth, focusX);
  131. } else if(widthRatio < heightRatio) {
  132. vShift = shiftToPoint(widthRatio, (containerHeight * (height / 100)), originalHeight, focusY, true);
  133. }
  134. if (originalWidth > originalHeight) {
  135. imageStyle.height = '100%';
  136. imageStyle.width = 'auto';
  137. imageStyle.minWidth = '100%';
  138. } else {
  139. imageStyle.height = 'auto';
  140. imageStyle.width = '100%';
  141. imageStyle.minHeight = '100%';
  142. }
  143. imageStyle.top = vShift;
  144. imageStyle.left = hShift;
  145. } else {
  146. imageStyle.height = '100%';
  147. }
  148. thumbnail = (
  149. <a
  150. className='media-gallery__item-thumbnail'
  151. href={attachment.get('remote_url') || originalUrl}
  152. onClick={this.handleClick}
  153. target='_blank'
  154. >
  155. <img
  156. src={previewUrl}
  157. srcSet={srcSet}
  158. sizes={sizes}
  159. alt={attachment.get('description')}
  160. title={attachment.get('description')}
  161. style={imageStyle}
  162. />
  163. </a>
  164. );
  165. } else if (attachment.get('type') === 'gifv') {
  166. const autoPlay = !isIOS() && autoPlayGif;
  167. thumbnail = (
  168. <div className={classNames('media-gallery__gifv', { autoplay: autoPlay })}>
  169. <video
  170. className='media-gallery__item-gifv-thumbnail'
  171. aria-label={attachment.get('description')}
  172. role='application'
  173. src={attachment.get('url')}
  174. onClick={this.handleClick}
  175. onMouseEnter={this.handleMouseEnter}
  176. onMouseLeave={this.handleMouseLeave}
  177. autoPlay={autoPlay}
  178. loop
  179. muted
  180. />
  181. <span className='media-gallery__gifv__label'>GIF</span>
  182. </div>
  183. );
  184. }
  185. return (
  186. <div className={classNames('media-gallery__item', { standalone })} key={attachment.get('id')} style={{ left: left, top: top, right: right, bottom: bottom, width: `${width}%`, height: `${height}%` }}>
  187. {thumbnail}
  188. </div>
  189. );
  190. }
  191. }
  192. @injectIntl
  193. export default class MediaGallery extends React.PureComponent {
  194. static propTypes = {
  195. sensitive: PropTypes.bool,
  196. standalone: PropTypes.bool,
  197. media: ImmutablePropTypes.list.isRequired,
  198. size: PropTypes.object,
  199. height: PropTypes.number.isRequired,
  200. onOpenMedia: PropTypes.func.isRequired,
  201. intl: PropTypes.object.isRequired,
  202. };
  203. static defaultProps = {
  204. standalone: false,
  205. };
  206. state = {
  207. visible: !this.props.sensitive || displaySensitiveMedia,
  208. };
  209. componentWillReceiveProps (nextProps) {
  210. if (!is(nextProps.media, this.props.media)) {
  211. this.setState({ visible: !nextProps.sensitive });
  212. }
  213. }
  214. handleOpen = () => {
  215. this.setState({ visible: !this.state.visible });
  216. }
  217. handleClick = (index) => {
  218. this.props.onOpenMedia(this.props.media, index);
  219. }
  220. handleRef = (node) => {
  221. if (node /*&& this.isStandaloneEligible()*/) {
  222. // offsetWidth triggers a layout, so only calculate when we need to
  223. this.setState({
  224. width: node.offsetWidth,
  225. });
  226. }
  227. }
  228. isStandaloneEligible() {
  229. const { media, standalone } = this.props;
  230. return standalone && media.size === 1 && media.getIn([0, 'meta', 'small', 'aspect']);
  231. }
  232. render () {
  233. const { media, intl, sensitive, height } = this.props;
  234. const { width, visible } = this.state;
  235. let children;
  236. const style = {};
  237. if (this.isStandaloneEligible()) {
  238. if (width) {
  239. style.height = width / this.props.media.getIn([0, 'meta', 'small', 'aspect']);
  240. }
  241. } else if (width) {
  242. style.height = width / (16/9);
  243. } else {
  244. style.height = height;
  245. }
  246. if (!visible) {
  247. let warning;
  248. if (sensitive) {
  249. warning = <FormattedMessage id='status.sensitive_warning' defaultMessage='Sensitive content' />;
  250. } else {
  251. warning = <FormattedMessage id='status.media_hidden' defaultMessage='Media hidden' />;
  252. }
  253. children = (
  254. <button type='button' className='media-spoiler' onClick={this.handleOpen} style={style} ref={this.handleRef}>
  255. <span className='media-spoiler__warning'>{warning}</span>
  256. <span className='media-spoiler__trigger'><FormattedMessage id='status.sensitive_toggle' defaultMessage='Click to view' /></span>
  257. </button>
  258. );
  259. } else {
  260. const size = media.take(4).size;
  261. if (this.isStandaloneEligible()) {
  262. children = <Item standalone onClick={this.handleClick} attachment={media.get(0)} />;
  263. } else {
  264. children = media.take(4).map((attachment, i) => <Item key={attachment.get('id')} onClick={this.handleClick} attachment={attachment} index={i} size={size} containerWidth={width} containerHeight={style.height} />);
  265. }
  266. }
  267. return (
  268. <div className='media-gallery' style={style} ref={this.handleRef}>
  269. <div className={classNames('spoiler-button', { 'spoiler-button--visible': visible })}>
  270. <IconButton title={intl.formatMessage(messages.toggle_visible)} icon={visible ? 'eye' : 'eye-slash'} overlay onClick={this.handleOpen} />
  271. </div>
  272. {children}
  273. </div>
  274. );
  275. }
  276. }