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.

228 lines
7.0 KiB

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