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.

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