logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

media_gallery.js (8554B)


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