闭社主体 forked from https://github.com/tootsuite/mastodon
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.

256 lines
8.3 KiB

8 years ago
8 years ago
8 years ago
8 years ago
  1. import React from 'react';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import PropTypes from 'prop-types';
  4. import { isRtl } from '../rtl';
  5. import { FormattedMessage } from 'react-intl';
  6. import Permalink from './permalink';
  7. import classnames from 'classnames';
  8. import PollContainer from 'mastodon/containers/poll_container';
  9. import Icon from 'mastodon/components/icon';
  10. import { autoPlayGif } from 'mastodon/initial_state';
  11. const MAX_HEIGHT = 642; // 20px * 32 (+ 2px padding at the top)
  12. export default class StatusContent extends React.PureComponent {
  13. static contextTypes = {
  14. router: PropTypes.object,
  15. };
  16. static propTypes = {
  17. status: ImmutablePropTypes.map.isRequired,
  18. expanded: PropTypes.bool,
  19. onExpandedToggle: PropTypes.func,
  20. onClick: PropTypes.func,
  21. collapsable: PropTypes.bool,
  22. };
  23. state = {
  24. hidden: true,
  25. collapsed: null, // `collapsed: null` indicates that an element doesn't need collapsing, while `true` or `false` indicates that it does (and is/isn't).
  26. };
  27. _updateStatusLinks () {
  28. const node = this.node;
  29. if (!node) {
  30. return;
  31. }
  32. const links = node.querySelectorAll('a');
  33. for (var i = 0; i < links.length; ++i) {
  34. let link = links[i];
  35. if (link.classList.contains('status-link')) {
  36. continue;
  37. }
  38. link.classList.add('status-link');
  39. let mention = this.props.status.get('mentions').find(item => link.href === item.get('url'));
  40. if (mention) {
  41. link.addEventListener('click', this.onMentionClick.bind(this, mention), false);
  42. link.setAttribute('title', mention.get('acct'));
  43. } else if (link.textContent[0] === '#' || (link.previousSibling && link.previousSibling.textContent && link.previousSibling.textContent[link.previousSibling.textContent.length - 1] === '#')) {
  44. link.addEventListener('click', this.onHashtagClick.bind(this, link.text), false);
  45. } else {
  46. link.setAttribute('title', link.href);
  47. link.classList.add('unhandled-link');
  48. }
  49. link.setAttribute('target', '_blank');
  50. link.setAttribute('rel', 'noopener');
  51. }
  52. if (
  53. this.props.collapsable
  54. && this.props.onClick
  55. && this.state.collapsed === null
  56. && node.clientHeight > MAX_HEIGHT
  57. && this.props.status.get('spoiler_text').length === 0
  58. ) {
  59. this.setState({ collapsed: true });
  60. }
  61. }
  62. _updateStatusEmojis () {
  63. const node = this.node;
  64. if (!node || autoPlayGif) {
  65. return;
  66. }
  67. const emojis = node.querySelectorAll('.custom-emoji');
  68. for (var i = 0; i < emojis.length; i++) {
  69. let emoji = emojis[i];
  70. if (emoji.classList.contains('status-emoji')) {
  71. continue;
  72. }
  73. emoji.classList.add('status-emoji');
  74. emoji.addEventListener('mouseenter', this.handleEmojiMouseEnter, false);
  75. emoji.addEventListener('mouseleave', this.handleEmojiMouseLeave, false);
  76. }
  77. }
  78. componentDidMount () {
  79. this._updateStatusLinks();
  80. this._updateStatusEmojis();
  81. }
  82. componentDidUpdate () {
  83. this._updateStatusLinks();
  84. this._updateStatusEmojis();
  85. }
  86. onMentionClick = (mention, e) => {
  87. if (this.context.router && e.button === 0 && !(e.ctrlKey || e.metaKey)) {
  88. e.preventDefault();
  89. this.context.router.history.push(`/accounts/${mention.get('id')}`);
  90. }
  91. }
  92. onHashtagClick = (hashtag, e) => {
  93. hashtag = hashtag.replace(/^#/, '');
  94. if (this.context.router && e.button === 0 && !(e.ctrlKey || e.metaKey)) {
  95. e.preventDefault();
  96. this.context.router.history.push(`/timelines/tag/${hashtag}`);
  97. }
  98. }
  99. handleEmojiMouseEnter = ({ target }) => {
  100. target.src = target.getAttribute('data-original');
  101. }
  102. handleEmojiMouseLeave = ({ target }) => {
  103. target.src = target.getAttribute('data-static');
  104. }
  105. handleMouseDown = (e) => {
  106. this.startXY = [e.clientX, e.clientY];
  107. }
  108. handleMouseUp = (e) => {
  109. if (!this.startXY) {
  110. return;
  111. }
  112. const [ startX, startY ] = this.startXY;
  113. const [ deltaX, deltaY ] = [Math.abs(e.clientX - startX), Math.abs(e.clientY - startY)];
  114. let element = e.target;
  115. while (element) {
  116. if (element.localName === 'button' || element.localName === 'a' || element.localName === 'label') {
  117. return;
  118. }
  119. element = element.parentNode;
  120. }
  121. if (deltaX + deltaY < 5 && e.button === 0 && this.props.onClick) {
  122. this.props.onClick();
  123. }
  124. this.startXY = null;
  125. }
  126. handleSpoilerClick = (e) => {
  127. e.preventDefault();
  128. if (this.props.onExpandedToggle) {
  129. // The parent manages the state
  130. this.props.onExpandedToggle();
  131. } else {
  132. this.setState({ hidden: !this.state.hidden });
  133. }
  134. }
  135. setRef = (c) => {
  136. this.node = c;
  137. }
  138. render () {
  139. const { status } = this.props;
  140. if (status.get('content').length === 0) {
  141. return null;
  142. }
  143. const hidden = this.props.onExpandedToggle ? !this.props.expanded : this.state.hidden;
  144. const content = { __html: status.get('contentHtml') };
  145. const spoilerContent = { __html: status.get('spoilerHtml') };
  146. const directionStyle = { direction: 'ltr' };
  147. const classNames = classnames('status__content', {
  148. 'status__content--with-action': this.props.onClick && this.context.router,
  149. 'status__content--with-spoiler': status.get('spoiler_text').length > 0,
  150. 'status__content--collapsed': this.state.collapsed === true,
  151. });
  152. if (isRtl(status.get('search_index'))) {
  153. directionStyle.direction = 'rtl';
  154. }
  155. const readMoreButton = (
  156. <button className='status__content__read-more-button' onClick={this.props.onClick} key='read-more'>
  157. <FormattedMessage id='status.read_more' defaultMessage='Read more' /><Icon id='angle-right' fixedWidth />
  158. </button>
  159. );
  160. if (status.get('spoiler_text').length > 0) {
  161. let mentionsPlaceholder = '';
  162. const mentionLinks = status.get('mentions').map(item => (
  163. <Permalink to={`/accounts/${item.get('id')}`} href={item.get('url')} key={item.get('id')} className='mention'>
  164. @<span>{item.get('username')}</span>
  165. </Permalink>
  166. )).reduce((aggregate, item) => [...aggregate, item, ' '], []);
  167. const toggleText = hidden ? <FormattedMessage id='status.show_more' defaultMessage='Show more' /> : <FormattedMessage id='status.show_less' defaultMessage='Show less' />;
  168. if (hidden) {
  169. mentionsPlaceholder = <div>{mentionLinks}</div>;
  170. }
  171. return (
  172. <div className={classNames} ref={this.setRef} tabIndex='0' style={directionStyle} onMouseDown={this.handleMouseDown} onMouseUp={this.handleMouseUp}>
  173. <p style={{ marginBottom: hidden && status.get('mentions').isEmpty() ? '0px' : null }}>
  174. <span dangerouslySetInnerHTML={spoilerContent} lang={status.get('language')} />
  175. {' '}
  176. <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>
  177. </p>
  178. {mentionsPlaceholder}
  179. <div tabIndex={!hidden ? 0 : null} className={`status__content__text ${!hidden ? 'status__content__text--visible' : ''}`} style={directionStyle} dangerouslySetInnerHTML={content} lang={status.get('language')} />
  180. {!hidden && !!status.get('poll') && <PollContainer pollId={status.get('poll')} />}
  181. </div>
  182. );
  183. } else if (this.props.onClick) {
  184. const output = [
  185. <div className={classNames} ref={this.setRef} tabIndex='0' style={directionStyle} onMouseDown={this.handleMouseDown} onMouseUp={this.handleMouseUp} key='status-content'>
  186. <div className='status__content__text status__content__text--visible' style={directionStyle} dangerouslySetInnerHTML={content} lang={status.get('language')} />
  187. {!!status.get('poll') && <PollContainer pollId={status.get('poll')} />}
  188. </div>,
  189. ];
  190. if (this.state.collapsed) {
  191. output.push(readMoreButton);
  192. }
  193. return output;
  194. } else {
  195. return (
  196. <div className={classNames} ref={this.setRef} tabIndex='0' style={directionStyle}>
  197. <div className='status__content__text status__content__text--visible' style={directionStyle} dangerouslySetInnerHTML={content} lang={status.get('language')} />
  198. {!!status.get('poll') && <PollContainer pollId={status.get('poll')} />}
  199. </div>
  200. );
  201. }
  202. }
  203. }