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.

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