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.

157 lines
5.2 KiB

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