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.

176 lines
5.5 KiB

7 years ago
7 years ago
7 years ago
  1. import React from 'react';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import escapeTextContentForBrowser from 'escape-html';
  4. import PropTypes from 'prop-types';
  5. import emojify from '../emoji';
  6. import { isRtl } from '../rtl';
  7. import { FormattedMessage } from 'react-intl';
  8. import Permalink from './permalink';
  9. 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. onHeightUpdate: PropTypes.func,
  18. onClick: PropTypes.func,
  19. };
  20. state = {
  21. hidden: true,
  22. };
  23. componentDidMount () {
  24. const node = this.node;
  25. const links = node.querySelectorAll('a');
  26. for (var i = 0; i < links.length; ++i) {
  27. let link = links[i];
  28. let mention = this.props.status.get('mentions').find(item => link.href === item.get('url'));
  29. let media = this.props.status.get('media_attachments').find(item => link.href === item.get('text_url') || (item.get('remote_url').length > 0 && link.href === item.get('remote_url')));
  30. if (mention) {
  31. link.addEventListener('click', this.onMentionClick.bind(this, mention), false);
  32. link.setAttribute('title', mention.get('acct'));
  33. } else if (link.textContent[0] === '#' || (link.previousSibling && link.previousSibling.textContent && link.previousSibling.textContent[link.previousSibling.textContent.length - 1] === '#')) {
  34. link.addEventListener('click', this.onHashtagClick.bind(this, link.text), false);
  35. } else {
  36. link.setAttribute('target', '_blank');
  37. link.setAttribute('rel', 'noopener');
  38. link.setAttribute('title', link.href);
  39. }
  40. }
  41. }
  42. componentDidUpdate () {
  43. if (this.props.onHeightUpdate) {
  44. this.props.onHeightUpdate();
  45. }
  46. }
  47. onMentionClick = (mention, e) => {
  48. if (e.button === 0) {
  49. e.preventDefault();
  50. this.context.router.history.push(`/accounts/${mention.get('id')}`);
  51. }
  52. }
  53. onHashtagClick = (hashtag, e) => {
  54. hashtag = hashtag.replace(/^#/, '').toLowerCase();
  55. if (e.button === 0) {
  56. e.preventDefault();
  57. this.context.router.history.push(`/timelines/tag/${hashtag}`);
  58. }
  59. }
  60. handleMouseDown = (e) => {
  61. this.startXY = [e.clientX, e.clientY];
  62. }
  63. handleMouseUp = (e) => {
  64. if (!this.startXY) {
  65. return;
  66. }
  67. const [ startX, startY ] = this.startXY;
  68. const [ deltaX, deltaY ] = [Math.abs(e.clientX - startX), Math.abs(e.clientY - startY)];
  69. if (e.target.localName === 'button' || e.target.localName === 'a' || (e.target.parentNode && (e.target.parentNode.localName === 'button' || e.target.parentNode.localName === 'a'))) {
  70. return;
  71. }
  72. if (deltaX + deltaY < 5 && e.button === 0 && this.props.onClick) {
  73. this.props.onClick();
  74. }
  75. this.startXY = null;
  76. }
  77. handleSpoilerClick = (e) => {
  78. e.preventDefault();
  79. if (this.props.onExpandedToggle) {
  80. // The parent manages the state
  81. this.props.onExpandedToggle();
  82. } else {
  83. this.setState({ hidden: !this.state.hidden });
  84. }
  85. }
  86. setRef = (c) => {
  87. this.node = c;
  88. }
  89. render () {
  90. const { status } = this.props;
  91. const hidden = this.props.onExpandedToggle ? !this.props.expanded : this.state.hidden;
  92. const content = { __html: emojify(status.get('content')) };
  93. const spoilerContent = { __html: emojify(escapeTextContentForBrowser(status.get('spoiler_text', ''))) };
  94. const directionStyle = { direction: 'ltr' };
  95. if (isRtl(status.get('search_index'))) {
  96. directionStyle.direction = 'rtl';
  97. }
  98. if (status.get('spoiler_text').length > 0) {
  99. let mentionsPlaceholder = '';
  100. const mentionLinks = status.get('mentions').map(item => (
  101. <Permalink to={`/accounts/${item.get('id')}`} href={item.get('url')} key={item.get('id')} className='mention'>
  102. @<span>{item.get('username')}</span>
  103. </Permalink>
  104. )).reduce((aggregate, item) => [...aggregate, item, ' '], []);
  105. const toggleText = hidden ? <FormattedMessage id='status.show_more' defaultMessage='Show more' /> : <FormattedMessage id='status.show_less' defaultMessage='Show less' />;
  106. if (hidden) {
  107. mentionsPlaceholder = <div>{mentionLinks}</div>;
  108. }
  109. return (
  110. <div className='status__content status__content--with_action' ref={this.setRef} onMouseDown={this.handleMouseDown} onMouseUp={this.handleMouseUp}>
  111. <p style={{ marginBottom: hidden && status.get('mentions').isEmpty() ? '0px' : null }}>
  112. <span dangerouslySetInnerHTML={spoilerContent} />
  113. {' '}
  114. <button tabIndex='0' className='status__content__spoiler-link' onClick={this.handleSpoilerClick}>{toggleText}</button>
  115. </p>
  116. {mentionsPlaceholder}
  117. <div className={`status__content__text ${!hidden ? 'status__content__text--visible' : ''}`} style={directionStyle} dangerouslySetInnerHTML={content} />
  118. </div>
  119. );
  120. } else if (this.props.onClick) {
  121. return (
  122. <div
  123. ref={this.setRef}
  124. className='status__content status__content--with-action'
  125. style={directionStyle}
  126. onMouseDown={this.handleMouseDown}
  127. onMouseUp={this.handleMouseUp}
  128. dangerouslySetInnerHTML={content}
  129. />
  130. );
  131. } else {
  132. return (
  133. <div
  134. ref={this.setRef}
  135. className='status__content'
  136. style={directionStyle}
  137. dangerouslySetInnerHTML={content}
  138. />
  139. );
  140. }
  141. }
  142. }
  143. export default StatusContent;