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.

307 lines
8.4 KiB

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