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.

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