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.

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