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.
 
 
 
 

338 lines
9.6 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. import { decode } from 'blurhash';
  11. const messages = defineMessages({
  12. toggle_visible: { id: 'media_gallery.toggle_visible', defaultMessage: 'Toggle visibility' },
  13. });
  14. class Item extends React.PureComponent {
  15. static propTypes = {
  16. attachment: ImmutablePropTypes.map.isRequired,
  17. standalone: PropTypes.bool,
  18. index: PropTypes.number.isRequired,
  19. size: PropTypes.number.isRequired,
  20. onClick: PropTypes.func.isRequired,
  21. displayWidth: PropTypes.number,
  22. visible: PropTypes.bool.isRequired,
  23. };
  24. static defaultProps = {
  25. standalone: false,
  26. index: 0,
  27. size: 1,
  28. };
  29. state = {
  30. loaded: false,
  31. };
  32. handleMouseEnter = (e) => {
  33. if (this.hoverToPlay()) {
  34. e.target.play();
  35. }
  36. }
  37. handleMouseLeave = (e) => {
  38. if (this.hoverToPlay()) {
  39. e.target.pause();
  40. e.target.currentTime = 0;
  41. }
  42. }
  43. hoverToPlay () {
  44. const { attachment } = this.props;
  45. return !autoPlayGif && attachment.get('type') === 'gifv';
  46. }
  47. handleClick = (e) => {
  48. const { index, onClick } = this.props;
  49. if (e.button === 0 && !(e.ctrlKey || e.metaKey)) {
  50. if (this.hoverToPlay()) {
  51. e.target.pause();
  52. e.target.currentTime = 0;
  53. }
  54. e.preventDefault();
  55. onClick(index);
  56. }
  57. e.stopPropagation();
  58. }
  59. componentDidMount () {
  60. if (this.props.attachment.get('blurhash')) {
  61. this._decode();
  62. }
  63. }
  64. componentDidUpdate (prevProps) {
  65. if (prevProps.attachment.get('blurhash') !== this.props.attachment.get('blurhash') && this.props.attachment.get('blurhash')) {
  66. this._decode();
  67. }
  68. }
  69. _decode () {
  70. const hash = this.props.attachment.get('blurhash');
  71. const pixels = decode(hash, 32, 32);
  72. if (pixels) {
  73. const ctx = this.canvas.getContext('2d');
  74. const imageData = new ImageData(pixels, 32, 32);
  75. ctx.putImageData(imageData, 0, 0);
  76. }
  77. }
  78. setCanvasRef = c => {
  79. this.canvas = c;
  80. }
  81. handleImageLoad = () => {
  82. this.setState({ loaded: true });
  83. }
  84. render () {
  85. const { attachment, index, size, standalone, displayWidth, visible } = this.props;
  86. let width = 50;
  87. let height = 100;
  88. let top = 'auto';
  89. let left = 'auto';
  90. let bottom = 'auto';
  91. let right = 'auto';
  92. if (size === 1) {
  93. width = 100;
  94. }
  95. if (size === 4 || (size === 3 && index > 0)) {
  96. height = 50;
  97. }
  98. if (size === 2) {
  99. if (index === 0) {
  100. right = '2px';
  101. } else {
  102. left = '2px';
  103. }
  104. } else if (size === 3) {
  105. if (index === 0) {
  106. right = '2px';
  107. } else if (index > 0) {
  108. left = '2px';
  109. }
  110. if (index === 1) {
  111. bottom = '2px';
  112. } else if (index > 1) {
  113. top = '2px';
  114. }
  115. } else if (size === 4) {
  116. if (index === 0 || index === 2) {
  117. right = '2px';
  118. }
  119. if (index === 1 || index === 3) {
  120. left = '2px';
  121. }
  122. if (index < 2) {
  123. bottom = '2px';
  124. } else {
  125. top = '2px';
  126. }
  127. }
  128. let thumbnail = '';
  129. if (attachment.get('type') === 'unknown') {
  130. return (
  131. <div className={classNames('media-gallery__item', { standalone })} key={attachment.get('id')} style={{ left: left, top: top, right: right, bottom: bottom, width: `${width}%`, height: `${height}%` }}>
  132. <a className='media-gallery__item-thumbnail' href={attachment.get('remote_url')} target='_blank' style={{ cursor: 'pointer' }} >
  133. <canvas width={32} height={32} ref={this.setCanvasRef} className='media-gallery__preview' />
  134. </a>
  135. </div>
  136. );
  137. } else if (attachment.get('type') === 'image') {
  138. const previewUrl = attachment.get('preview_url');
  139. const previewWidth = attachment.getIn(['meta', 'small', 'width']);
  140. const originalUrl = attachment.get('url');
  141. const originalWidth = attachment.getIn(['meta', 'original', 'width']);
  142. const hasSize = typeof originalWidth === 'number' && typeof previewWidth === 'number';
  143. const srcSet = hasSize ? `${originalUrl} ${originalWidth}w, ${previewUrl} ${previewWidth}w` : null;
  144. const sizes = hasSize && (displayWidth > 0) ? `${displayWidth * (width / 100)}px` : null;
  145. const focusX = attachment.getIn(['meta', 'focus', 'x']) || 0;
  146. const focusY = attachment.getIn(['meta', 'focus', 'y']) || 0;
  147. const x = ((focusX / 2) + .5) * 100;
  148. const y = ((focusY / -2) + .5) * 100;
  149. thumbnail = (
  150. <a
  151. className='media-gallery__item-thumbnail'
  152. href={attachment.get('remote_url') || originalUrl}
  153. onClick={this.handleClick}
  154. target='_blank'
  155. >
  156. <img
  157. src={previewUrl}
  158. srcSet={srcSet}
  159. sizes={sizes}
  160. alt={attachment.get('description')}
  161. title={attachment.get('description')}
  162. style={{ objectPosition: `${x}% ${y}%` }}
  163. onLoad={this.handleImageLoad}
  164. />
  165. </a>
  166. );
  167. } else if (attachment.get('type') === 'gifv') {
  168. const autoPlay = !isIOS() && autoPlayGif;
  169. thumbnail = (
  170. <div className={classNames('media-gallery__gifv', { autoplay: autoPlay })}>
  171. <video
  172. className='media-gallery__item-gifv-thumbnail'
  173. aria-label={attachment.get('description')}
  174. title={attachment.get('description')}
  175. role='application'
  176. src={attachment.get('url')}
  177. onClick={this.handleClick}
  178. onMouseEnter={this.handleMouseEnter}
  179. onMouseLeave={this.handleMouseLeave}
  180. autoPlay={autoPlay}
  181. loop
  182. muted
  183. />
  184. <span className='media-gallery__gifv__label'>GIF</span>
  185. </div>
  186. );
  187. }
  188. return (
  189. <div className={classNames('media-gallery__item', { standalone })} key={attachment.get('id')} style={{ left: left, top: top, right: right, bottom: bottom, width: `${width}%`, height: `${height}%` }}>
  190. <canvas width={32} height={32} ref={this.setCanvasRef} className={classNames('media-gallery__preview', { 'media-gallery__preview--hidden': visible && this.state.loaded })} />
  191. {visible && thumbnail}
  192. </div>
  193. );
  194. }
  195. }
  196. export default @injectIntl
  197. class MediaGallery extends React.PureComponent {
  198. static propTypes = {
  199. sensitive: PropTypes.bool,
  200. standalone: PropTypes.bool,
  201. media: ImmutablePropTypes.list.isRequired,
  202. size: PropTypes.object,
  203. height: PropTypes.number.isRequired,
  204. onOpenMedia: PropTypes.func.isRequired,
  205. intl: PropTypes.object.isRequired,
  206. defaultWidth: PropTypes.number,
  207. cacheWidth: PropTypes.func,
  208. };
  209. static defaultProps = {
  210. standalone: false,
  211. };
  212. state = {
  213. visible: displayMedia !== 'hide_all' && !this.props.sensitive || displayMedia === 'show_all',
  214. width: this.props.defaultWidth,
  215. };
  216. componentWillReceiveProps (nextProps) {
  217. if (!is(nextProps.media, this.props.media)) {
  218. this.setState({ visible: !nextProps.sensitive });
  219. }
  220. }
  221. handleOpen = () => {
  222. this.setState({ visible: !this.state.visible });
  223. }
  224. handleClick = (index) => {
  225. this.props.onOpenMedia(this.props.media, index);
  226. }
  227. handleRef = (node) => {
  228. if (node /*&& this.isStandaloneEligible()*/) {
  229. // offsetWidth triggers a layout, so only calculate when we need to
  230. if (this.props.cacheWidth) this.props.cacheWidth(node.offsetWidth);
  231. this.setState({
  232. width: node.offsetWidth,
  233. });
  234. }
  235. }
  236. isStandaloneEligible() {
  237. const { media, standalone } = this.props;
  238. return standalone && media.size === 1 && media.getIn([0, 'meta', 'small', 'aspect']);
  239. }
  240. render () {
  241. const { media, intl, sensitive, height, defaultWidth } = this.props;
  242. const { visible } = this.state;
  243. const width = this.state.width || defaultWidth;
  244. let children, spoilerButton;
  245. const style = {};
  246. if (this.isStandaloneEligible()) {
  247. if (width) {
  248. style.height = width / this.props.media.getIn([0, 'meta', 'small', 'aspect']);
  249. }
  250. } else if (width) {
  251. style.height = width / (16/9);
  252. } else {
  253. style.height = height;
  254. }
  255. const size = media.take(4).size;
  256. if (this.isStandaloneEligible()) {
  257. children = <Item standalone onClick={this.handleClick} attachment={media.get(0)} displayWidth={width} visible={visible} />;
  258. } else {
  259. children = media.take(4).map((attachment, i) => <Item key={attachment.get('id')} onClick={this.handleClick} attachment={attachment} index={i} size={size} displayWidth={width} visible={visible} />);
  260. }
  261. if (visible) {
  262. spoilerButton = <IconButton title={intl.formatMessage(messages.toggle_visible)} icon={visible ? 'eye' : 'eye-slash'} overlay onClick={this.handleOpen} />;
  263. } else {
  264. spoilerButton = (
  265. <button type='button' onClick={this.handleOpen} className='spoiler-button__overlay'>
  266. <span className='spoiler-button__overlay__label'>{sensitive ? <FormattedMessage id='status.sensitive_warning' defaultMessage='Sensitive content' /> : <FormattedMessage id='status.media_hidden' defaultMessage='Media hidden' />}</span>
  267. </button>
  268. );
  269. }
  270. return (
  271. <div className='media-gallery' style={style} ref={this.handleRef}>
  272. <div className={classNames('spoiler-button', { 'spoiler-button--minified': visible })}>
  273. {spoilerButton}
  274. </div>
  275. {children}
  276. </div>
  277. );
  278. }
  279. }