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.

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