The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

263 lines
7.1 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 sizeMe from 'react-sizeme';
  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. onClick: PropTypes.func.isRequired,
  23. autoPlayGif: PropTypes.bool,
  24. };
  25. static defaultProps = {
  26. autoPlayGif: false,
  27. standalone: false,
  28. index: 0,
  29. size: 1,
  30. };
  31. handleMouseEnter = (e) => {
  32. if (this.hoverToPlay()) {
  33. e.target.play();
  34. }
  35. }
  36. handleMouseLeave = (e) => {
  37. if (this.hoverToPlay()) {
  38. e.target.pause();
  39. e.target.currentTime = 0;
  40. }
  41. }
  42. hoverToPlay () {
  43. const { attachment, autoPlayGif } = this.props;
  44. return !autoPlayGif && attachment.get('type') === 'gifv';
  45. }
  46. handleClick = (e) => {
  47. const { index, onClick } = this.props;
  48. if (this.context.router && e.button === 0) {
  49. e.preventDefault();
  50. onClick(index);
  51. }
  52. e.stopPropagation();
  53. }
  54. render () {
  55. const { attachment, index, size, standalone } = 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 ? `(min-width: 1025px) ${320 * (width / 100)}px, ${width}vw` : null;
  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 src={previewUrl} srcSet={srcSet} sizes={sizes} alt={attachment.get('description')} />
  115. </a>
  116. );
  117. } else if (attachment.get('type') === 'gifv') {
  118. const autoPlay = !isIOS() && this.props.autoPlayGif;
  119. thumbnail = (
  120. <div className={classNames('media-gallery__gifv', { autoplay: autoPlay })}>
  121. <video
  122. className='media-gallery__item-gifv-thumbnail'
  123. aria-label={attachment.get('description')}
  124. role='application'
  125. src={attachment.get('url')}
  126. onClick={this.handleClick}
  127. onMouseEnter={this.handleMouseEnter}
  128. onMouseLeave={this.handleMouseLeave}
  129. autoPlay={autoPlay}
  130. loop
  131. muted
  132. />
  133. <span className='media-gallery__gifv__label'>GIF</span>
  134. </div>
  135. );
  136. }
  137. return (
  138. <div className={classNames('media-gallery__item', { standalone })} key={attachment.get('id')} style={{ left: left, top: top, right: right, bottom: bottom, width: `${width}%`, height: `${height}%` }}>
  139. {thumbnail}
  140. </div>
  141. );
  142. }
  143. }
  144. @injectIntl
  145. @sizeMe({})
  146. export default class MediaGallery extends React.PureComponent {
  147. static propTypes = {
  148. sensitive: PropTypes.bool,
  149. standalone: PropTypes.bool,
  150. media: ImmutablePropTypes.list.isRequired,
  151. size: PropTypes.object,
  152. height: PropTypes.number.isRequired,
  153. onOpenMedia: PropTypes.func.isRequired,
  154. intl: PropTypes.object.isRequired,
  155. autoPlayGif: PropTypes.bool,
  156. };
  157. static defaultProps = {
  158. autoPlayGif: false,
  159. standalone: false,
  160. };
  161. state = {
  162. visible: !this.props.sensitive,
  163. };
  164. componentWillReceiveProps (nextProps) {
  165. if (!is(nextProps.media, this.props.media)) {
  166. this.setState({ visible: !nextProps.sensitive });
  167. }
  168. }
  169. handleOpen = () => {
  170. this.setState({ visible: !this.state.visible });
  171. }
  172. handleClick = (index) => {
  173. this.props.onOpenMedia(this.props.media, index);
  174. }
  175. render () {
  176. const { media, intl, sensitive, height, standalone, size } = this.props;
  177. let children;
  178. const standaloneEligible = standalone && size.width && media.size === 1 && media.getIn([0, 'meta', 'small', 'aspect']);
  179. const style = {};
  180. if (standaloneEligible) {
  181. style.height = size.width / media.getIn([0, 'meta', 'small', 'aspect']);
  182. } else {
  183. style.height = height;
  184. }
  185. if (!this.state.visible) {
  186. let warning;
  187. if (sensitive) {
  188. warning = <FormattedMessage id='status.sensitive_warning' defaultMessage='Sensitive content' />;
  189. } else {
  190. warning = <FormattedMessage id='status.media_hidden' defaultMessage='Media hidden' />;
  191. }
  192. children = (
  193. <button className='media-spoiler' onClick={this.handleOpen} style={style}>
  194. <span className='media-spoiler__warning'>{warning}</span>
  195. <span className='media-spoiler__trigger'><FormattedMessage id='status.sensitive_toggle' defaultMessage='Click to view' /></span>
  196. </button>
  197. );
  198. } else {
  199. const size = media.take(4).size;
  200. if (standaloneEligible) {
  201. children = <Item standalone onClick={this.handleClick} attachment={media.get(0)} autoPlayGif={this.props.autoPlayGif} />;
  202. } else {
  203. children = media.take(4).map((attachment, i) => <Item key={attachment.get('id')} onClick={this.handleClick} attachment={attachment} autoPlayGif={this.props.autoPlayGif} index={i} size={size} />);
  204. }
  205. }
  206. return (
  207. <div className='media-gallery' style={style}>
  208. <div className={classNames('spoiler-button', { 'spoiler-button--visible': this.state.visible })}>
  209. <IconButton title={intl.formatMessage(messages.toggle_visible)} icon={this.state.visible ? 'eye' : 'eye-slash'} overlay onClick={this.handleOpen} />
  210. </div>
  211. {children}
  212. </div>
  213. );
  214. }
  215. }