The code powering m.abunchtell.com https://m.abunchtell.com
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

231 lines
6.0 KiB

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