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.

328 lines
9.1 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, displayMedia } 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 && !(e.ctrlKey || e.metaKey)) {
  65. e.preventDefault();
  66. onClick(index);
  67. }
  68. e.stopPropagation();
  69. }
  70. handleMouseDown = (e) => {
  71. e.preventDefault();
  72. e.stopPropagation();
  73. }
  74. render () {
  75. const { attachment, index, size, standalone, letterbox, displayWidth } = this.props;
  76. let width = 50;
  77. let height = 100;
  78. let top = 'auto';
  79. let left = 'auto';
  80. let bottom = 'auto';
  81. let right = 'auto';
  82. if (size === 1) {
  83. width = 100;
  84. }
  85. if (size === 4 || (size === 3 && index > 0)) {
  86. height = 50;
  87. }
  88. if (size === 2) {
  89. if (index === 0) {
  90. right = '2px';
  91. } else {
  92. left = '2px';
  93. }
  94. } else if (size === 3) {
  95. if (index === 0) {
  96. right = '2px';
  97. } else if (index > 0) {
  98. left = '2px';
  99. }
  100. if (index === 1) {
  101. bottom = '2px';
  102. } else if (index > 1) {
  103. top = '2px';
  104. }
  105. } else if (size === 4) {
  106. if (index === 0 || index === 2) {
  107. right = '2px';
  108. }
  109. if (index === 1 || index === 3) {
  110. left = '2px';
  111. }
  112. if (index < 2) {
  113. bottom = '2px';
  114. } else {
  115. top = '2px';
  116. }
  117. }
  118. let thumbnail = '';
  119. if (attachment.get('type') === 'image') {
  120. const previewUrl = attachment.get('preview_url');
  121. const previewWidth = attachment.getIn(['meta', 'small', 'width']);
  122. const originalUrl = attachment.get('url');
  123. const originalWidth = attachment.getIn(['meta', 'original', 'width']);
  124. const hasSize = typeof originalWidth === 'number' && typeof previewWidth === 'number';
  125. const srcSet = hasSize ? `${originalUrl} ${originalWidth}w, ${previewUrl} ${previewWidth}w` : null;
  126. const sizes = hasSize && (displayWidth > 0) ? `${displayWidth * (width / 100)}px` : null;
  127. const focusX = attachment.getIn(['meta', 'focus', 'x']) || 0;
  128. const focusY = attachment.getIn(['meta', 'focus', 'y']) || 0;
  129. const x = ((focusX / 2) + .5) * 100;
  130. const y = ((focusY / -2) + .5) * 100;
  131. thumbnail = (
  132. <a
  133. className='media-gallery__item-thumbnail'
  134. href={attachment.get('remote_url') || originalUrl}
  135. onClick={this.handleClick}
  136. target='_blank'
  137. >
  138. <img
  139. className={letterbox ? 'letterbox' : null}
  140. src={previewUrl}
  141. srcSet={srcSet}
  142. sizes={sizes}
  143. alt={attachment.get('description')}
  144. title={attachment.get('description')}
  145. style={{ objectPosition: letterbox ? null : `${x}% ${y}%` }}
  146. />
  147. </a>
  148. );
  149. } else if (attachment.get('type') === 'gifv') {
  150. const autoPlay = !isIOS() && autoPlayGif;
  151. thumbnail = (
  152. <div className={classNames('media-gallery__gifv', { autoplay: autoPlay })}>
  153. <video
  154. className={`media-gallery__item-gifv-thumbnail${letterbox ? ' letterbox' : ''}`}
  155. aria-label={attachment.get('description')}
  156. title={attachment.get('description')}
  157. role='application'
  158. src={attachment.get('url')}
  159. onClick={this.handleClick}
  160. onMouseEnter={this.handleMouseEnter}
  161. onMouseLeave={this.handleMouseLeave}
  162. onMouseDown={this.handleMouseDown}
  163. autoPlay={autoPlay}
  164. loop
  165. muted
  166. />
  167. <span className='media-gallery__gifv__label'>GIF</span>
  168. </div>
  169. );
  170. }
  171. return (
  172. <div className={classNames('media-gallery__item', { standalone, letterbox })} key={attachment.get('id')} style={{ left: left, top: top, right: right, bottom: bottom, width: `${width}%`, height: `${height}%` }}>
  173. {thumbnail}
  174. </div>
  175. );
  176. }
  177. }
  178. @injectIntl
  179. export default class MediaGallery extends React.PureComponent {
  180. static propTypes = {
  181. sensitive: PropTypes.bool,
  182. revealed: PropTypes.bool,
  183. standalone: PropTypes.bool,
  184. letterbox: PropTypes.bool,
  185. fullwidth: PropTypes.bool,
  186. hidden: PropTypes.bool,
  187. media: ImmutablePropTypes.list.isRequired,
  188. size: PropTypes.object,
  189. onOpenMedia: PropTypes.func.isRequired,
  190. intl: PropTypes.object.isRequired,
  191. };
  192. static defaultProps = {
  193. standalone: false,
  194. };
  195. state = {
  196. visible: this.props.revealed === undefined ? (displayMedia !== 'hide_all' && !this.props.sensitive || displayMedia === 'show_all') : this.props.revealed,
  197. };
  198. componentWillReceiveProps (nextProps) {
  199. if (!is(nextProps.media, this.props.media)) {
  200. this.setState({ visible: nextProps.revealed === undefined ? (displayMedia !== 'hide_all' && !nextProps.sensitive || displayMedia === 'show_all') : nextProps.revealed });
  201. }
  202. }
  203. componentDidUpdate (prevProps) {
  204. if (this.node && this.node.offsetWidth && this.node.offsetWidth != this.state.width) {
  205. this.setState({
  206. width: this.node.offsetWidth,
  207. });
  208. }
  209. }
  210. handleOpen = () => {
  211. this.setState({ visible: !this.state.visible });
  212. }
  213. handleClick = (index) => {
  214. this.props.onOpenMedia(this.props.media, index);
  215. }
  216. handleRef = (node) => {
  217. this.node = node;
  218. if (node && node.offsetWidth && node.offsetWidth != this.state.width) {
  219. this.setState({
  220. width: node.offsetWidth,
  221. });
  222. }
  223. }
  224. isStandaloneEligible() {
  225. const { media, standalone } = this.props;
  226. return standalone && media.size === 1 && media.getIn([0, 'meta', 'small', 'aspect']);
  227. }
  228. render () {
  229. const { media, intl, sensitive, letterbox, fullwidth } = this.props;
  230. const { width, visible } = this.state;
  231. const size = media.take(4).size;
  232. let children;
  233. const style = {};
  234. const computedClass = classNames('media-gallery', { 'full-width': fullwidth });
  235. if (this.isStandaloneEligible() && width) {
  236. style.height = width / this.props.media.getIn([0, 'meta', 'small', 'aspect']);
  237. } else if (width) {
  238. style.height = width / (16/9);
  239. } else {
  240. return (<div className={computedClass} ref={this.handleRef}></div>);
  241. }
  242. if (!visible) {
  243. let warning = <FormattedMessage {...(sensitive ? messages.warning : messages.hidden)} />;
  244. children = (
  245. <button className='media-spoiler' type='button' onClick={this.handleOpen}>
  246. <span className='media-spoiler__warning'>{warning}</span>
  247. <span className='media-spoiler__trigger'><FormattedMessage {...messages.toggle} /></span>
  248. </button>
  249. );
  250. } else {
  251. if (this.isStandaloneEligible()) {
  252. children = <Item standalone attachment={media.get(0)} onClick={this.handleClick} displayWidth={width} />;
  253. } else {
  254. 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} />);
  255. }
  256. }
  257. return (
  258. <div className={computedClass} style={style} ref={this.handleRef}>
  259. {visible ? (
  260. <div className='sensitive-info'>
  261. <IconButton
  262. icon='eye'
  263. onClick={this.handleOpen}
  264. overlay
  265. title={intl.formatMessage(messages.toggle_visible)}
  266. />
  267. {sensitive ? (
  268. <span className='sensitive-marker'>
  269. <FormattedMessage {...messages.sensitive} />
  270. </span>
  271. ) : null}
  272. </div>
  273. ) : null}
  274. {children}
  275. </div>
  276. );
  277. }
  278. }