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.

151 lines
4.8 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. } else if (link.textContent[0] === '#' || (link.previousSibling && link.previousSibling.textContent && link.previousSibling.textContent[link.previousSibling.textContent.length - 1] === '#')) {
  32. link.addEventListener('click', this.onHashtagClick.bind(this, link.text), false);
  33. } else if (media) {
  34. link.innerHTML = '<i class="fa fa-fw fa-photo"></i>';
  35. } else {
  36. link.setAttribute('target', '_blank');
  37. link.setAttribute('rel', 'noopener');
  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')).replace(/\n/g, '') };
  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' style={{ cursor: 'pointer' }} onMouseDown={this.handleMouseDown} onMouseUp={this.handleMouseUp}>
  94. <p style={{ marginBottom: hidden && status.get('mentions').size === 0 ? '0px' : '' }} >
  95. <span dangerouslySetInnerHTML={spoilerContent} /> <a className='status__content__spoiler-link' 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={{ cursor: 'pointer', ...directionStyle }}
  106. onMouseDown={this.handleMouseDown}
  107. onMouseUp={this.handleMouseUp}
  108. dangerouslySetInnerHTML={content}
  109. />
  110. );
  111. } else {
  112. return (
  113. <div
  114. className='status__content'
  115. style={{ ...directionStyle }}
  116. dangerouslySetInnerHTML={content}
  117. />
  118. );
  119. }
  120. },
  121. });
  122. export default StatusContent;