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.

255 lines
7.0 KiB

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