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.

153 lines
4.9 KiB

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