The code powering m.abunchtell.com https://m.abunchtell.com
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

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