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.

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