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.

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