status_content.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. import React from 'react';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import PropTypes from 'prop-types';
  4. import { FormattedMessage, injectIntl } from 'react-intl';
  5. import Permalink from './permalink';
  6. import classnames from 'classnames';
  7. import PollContainer from 'mastodon/containers/poll_container';
  8. import Icon from 'mastodon/components/icon';
  9. import { autoPlayGif, languages as preloadedLanguages, translationEnabled } from 'mastodon/initial_state';
  10. const MAX_HEIGHT = 642; // 20px * 32 (+ 2px padding at the top)
  11. class TranslateButton extends React.PureComponent {
  12. static propTypes = {
  13. translation: ImmutablePropTypes.map,
  14. onClick: PropTypes.func,
  15. };
  16. render () {
  17. const { translation, onClick } = this.props;
  18. if (translation) {
  19. const language = preloadedLanguages.find(lang => lang[0] === translation.get('detected_source_language'));
  20. const languageName = language ? language[2] : translation.get('detected_source_language');
  21. const provider = translation.get('provider');
  22. return (
  23. <div className='translate-button'>
  24. <div className='translate-button__meta'>
  25. <FormattedMessage id='status.translated_from_with' defaultMessage='Translated from {lang} using {provider}' values={{ lang: languageName, provider }} />
  26. </div>
  27. <button className='link-button' onClick={onClick}>
  28. <FormattedMessage id='status.show_original' defaultMessage='Show original' />
  29. </button>
  30. </div>
  31. );
  32. }
  33. return (
  34. <button className='status__content__read-more-button' onClick={onClick}>
  35. <FormattedMessage id='status.translate' defaultMessage='Translate' />
  36. </button>
  37. );
  38. }
  39. }
  40. export default @injectIntl
  41. class StatusContent extends React.PureComponent {
  42. static contextTypes = {
  43. router: PropTypes.object,
  44. identity: PropTypes.object,
  45. };
  46. static propTypes = {
  47. status: ImmutablePropTypes.map.isRequired,
  48. expanded: PropTypes.bool,
  49. onExpandedToggle: PropTypes.func,
  50. onTranslate: PropTypes.func,
  51. onClick: PropTypes.func,
  52. collapsable: PropTypes.bool,
  53. onCollapsedToggle: PropTypes.func,
  54. intl: PropTypes.object,
  55. };
  56. state = {
  57. hidden: true,
  58. };
  59. _updateStatusLinks () {
  60. const node = this.node;
  61. if (!node) {
  62. return;
  63. }
  64. const links = node.querySelectorAll('a');
  65. for (var i = 0; i < links.length; ++i) {
  66. let link = links[i];
  67. if (link.classList.contains('status-link')) {
  68. continue;
  69. }
  70. link.classList.add('status-link');
  71. let mention = this.props.status.get('mentions').find(item => link.href === item.get('url'));
  72. if (mention) {
  73. link.addEventListener('click', this.onMentionClick.bind(this, mention), false);
  74. link.setAttribute('title', mention.get('acct'));
  75. } else if (link.textContent[0] === '#' || (link.previousSibling && link.previousSibling.textContent && link.previousSibling.textContent[link.previousSibling.textContent.length - 1] === '#')) {
  76. link.addEventListener('click', this.onHashtagClick.bind(this, link.text), false);
  77. } else {
  78. link.setAttribute('title', link.href);
  79. link.classList.add('unhandled-link');
  80. }
  81. }
  82. if (this.props.status.get('collapsed', null) === null) {
  83. let collapsed =
  84. this.props.collapsable
  85. && this.props.onClick
  86. && node.clientHeight > MAX_HEIGHT
  87. && this.props.status.get('spoiler_text').length === 0;
  88. if(this.props.onCollapsedToggle) this.props.onCollapsedToggle(collapsed);
  89. this.props.status.set('collapsed', collapsed);
  90. }
  91. }
  92. handleMouseEnter = ({ currentTarget }) => {
  93. if (autoPlayGif) {
  94. return;
  95. }
  96. const emojis = currentTarget.querySelectorAll('.custom-emoji');
  97. for (var i = 0; i < emojis.length; i++) {
  98. let emoji = emojis[i];
  99. emoji.src = emoji.getAttribute('data-original');
  100. }
  101. }
  102. handleMouseLeave = ({ currentTarget }) => {
  103. if (autoPlayGif) {
  104. return;
  105. }
  106. const emojis = currentTarget.querySelectorAll('.custom-emoji');
  107. for (var i = 0; i < emojis.length; i++) {
  108. let emoji = emojis[i];
  109. emoji.src = emoji.getAttribute('data-static');
  110. }
  111. }
  112. componentDidMount () {
  113. this._updateStatusLinks();
  114. }
  115. componentDidUpdate () {
  116. this._updateStatusLinks();
  117. }
  118. onMentionClick = (mention, e) => {
  119. if (this.context.router && e.button === 0 && !(e.ctrlKey || e.metaKey)) {
  120. e.preventDefault();
  121. this.context.router.history.push(`/@${mention.get('acct')}`);
  122. }
  123. }
  124. onHashtagClick = (hashtag, e) => {
  125. hashtag = hashtag.replace(/^#/, '');
  126. if (this.context.router && e.button === 0 && !(e.ctrlKey || e.metaKey)) {
  127. e.preventDefault();
  128. this.context.router.history.push(`/tags/${hashtag}`);
  129. }
  130. }
  131. handleMouseDown = (e) => {
  132. this.startXY = [e.clientX, e.clientY];
  133. }
  134. handleMouseUp = (e) => {
  135. if (!this.startXY) {
  136. return;
  137. }
  138. const [ startX, startY ] = this.startXY;
  139. const [ deltaX, deltaY ] = [Math.abs(e.clientX - startX), Math.abs(e.clientY - startY)];
  140. let element = e.target;
  141. while (element) {
  142. if (element.localName === 'button' || element.localName === 'a' || element.localName === 'label') {
  143. return;
  144. }
  145. element = element.parentNode;
  146. }
  147. if (deltaX + deltaY < 5 && e.button === 0 && this.props.onClick) {
  148. this.props.onClick();
  149. }
  150. this.startXY = null;
  151. }
  152. handleSpoilerClick = (e) => {
  153. e.preventDefault();
  154. if (this.props.onExpandedToggle) {
  155. // The parent manages the state
  156. this.props.onExpandedToggle();
  157. } else {
  158. this.setState({ hidden: !this.state.hidden });
  159. }
  160. }
  161. handleTranslate = () => {
  162. this.props.onTranslate();
  163. }
  164. setRef = (c) => {
  165. this.node = c;
  166. }
  167. render () {
  168. const { status, intl } = this.props;
  169. const hidden = this.props.onExpandedToggle ? !this.props.expanded : this.state.hidden;
  170. const renderReadMore = this.props.onClick && status.get('collapsed');
  171. const renderTranslate = translationEnabled && this.context.identity.signedIn && this.props.onTranslate && ['public', 'unlisted'].includes(status.get('visibility')) && status.get('contentHtml').length > 0 && status.get('language') !== null && intl.locale !== status.get('language');
  172. const content = { __html: status.get('translation') ? status.getIn(['translation', 'content']) : status.get('contentHtml') };
  173. const spoilerContent = { __html: status.get('spoilerHtml') };
  174. const lang = status.get('translation') ? intl.locale : status.get('language');
  175. const classNames = classnames('status__content', {
  176. 'status__content--with-action': this.props.onClick && this.context.router,
  177. 'status__content--with-spoiler': status.get('spoiler_text').length > 0,
  178. 'status__content--collapsed': renderReadMore,
  179. });
  180. const readMoreButton = renderReadMore && (
  181. <button className='status__content__read-more-button' onClick={this.props.onClick} key='read-more'>
  182. <FormattedMessage id='status.read_more' defaultMessage='Read more' /><Icon id='angle-right' fixedWidth />
  183. </button>
  184. );
  185. const translateButton = renderTranslate && (
  186. <TranslateButton onClick={this.handleTranslate} translation={status.get('translation')} />
  187. );
  188. const poll = !!status.get('poll') && (
  189. <PollContainer pollId={status.get('poll')} />
  190. );
  191. if (status.get('spoiler_text').length > 0) {
  192. let mentionsPlaceholder = '';
  193. const mentionLinks = status.get('mentions').map(item => (
  194. <Permalink to={`/@${item.get('acct')}`} href={item.get('url')} key={item.get('id')} className='mention'>
  195. @<span>{item.get('username')}</span>
  196. </Permalink>
  197. )).reduce((aggregate, item) => [...aggregate, item, ' '], []);
  198. const toggleText = hidden ? <FormattedMessage id='status.show_more' defaultMessage='Show more' /> : <FormattedMessage id='status.show_less' defaultMessage='Show less' />;
  199. if (hidden) {
  200. mentionsPlaceholder = <div>{mentionLinks}</div>;
  201. }
  202. return (
  203. <div className={classNames} ref={this.setRef} tabIndex='0' onMouseDown={this.handleMouseDown} onMouseUp={this.handleMouseUp} onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave}>
  204. <p style={{ marginBottom: hidden && status.get('mentions').isEmpty() ? '0px' : null }}>
  205. <span dangerouslySetInnerHTML={spoilerContent} className='translate' lang={lang} />
  206. {' '}
  207. <button tabIndex='0' className={`status__content__spoiler-link ${hidden ? 'status__content__spoiler-link--show-more' : 'status__content__spoiler-link--show-less'}`} onClick={this.handleSpoilerClick}>{toggleText}</button>
  208. </p>
  209. {mentionsPlaceholder}
  210. <div tabIndex={!hidden ? 0 : null} className={`status__content__text ${!hidden ? 'status__content__text--visible' : ''} translate`} lang={lang} dangerouslySetInnerHTML={content} />
  211. {!hidden && poll}
  212. {!hidden && translateButton}
  213. </div>
  214. );
  215. } else if (this.props.onClick) {
  216. return (
  217. <>
  218. <div className={classNames} ref={this.setRef} tabIndex='0' onMouseDown={this.handleMouseDown} onMouseUp={this.handleMouseUp} key='status-content' onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave}>
  219. <div className='status__content__text status__content__text--visible translate' lang={lang} dangerouslySetInnerHTML={content} />
  220. {poll}
  221. {translateButton}
  222. </div>
  223. {readMoreButton}
  224. </>
  225. );
  226. } else {
  227. return (
  228. <div className={classNames} ref={this.setRef} tabIndex='0' onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave}>
  229. <div className='status__content__text status__content__text--visible translate' lang={lang} dangerouslySetInnerHTML={content} />
  230. {poll}
  231. {translateButton}
  232. </div>
  233. );
  234. }
  235. }
  236. }