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.

129 lines
3.8 KiB

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