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.

64 lines
1.7 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. const StatusContent = React.createClass({
  4. contextTypes: {
  5. router: React.PropTypes.object
  6. },
  7. propTypes: {
  8. status: ImmutablePropTypes.map.isRequired
  9. },
  10. mixins: [PureRenderMixin],
  11. componentDidMount () {
  12. const node = ReactDOM.findDOMNode(this);
  13. const links = node.querySelectorAll('a');
  14. for (var i = 0; i < links.length; ++i) {
  15. let link = links[i];
  16. let mention = this.props.status.get('mentions').find(item => link.href === item.get('url'));
  17. if (mention) {
  18. link.addEventListener('click', this.onMentionClick.bind(this, mention), false);
  19. } else if (link.text[0] === '#' || (link.previousSibling && link.previousSibling.text === '#')) {
  20. link.addEventListener('click', this.onHashtagClick.bind(this, link.text), false);
  21. } else {
  22. link.setAttribute('target', '_blank');
  23. link.setAttribute('rel', 'noopener');
  24. }
  25. link.addEventListener('click', this.onNormalClick, false);
  26. }
  27. },
  28. onMentionClick (mention, e) {
  29. if (e.button === 0) {
  30. e.preventDefault();
  31. this.context.router.push(`/accounts/${mention.get('id')}`);
  32. }
  33. },
  34. onHashtagClick (hashtag, e) {
  35. hashtag = hashtag.replace(/^#/, '').toLowerCase();
  36. if (e.button === 0) {
  37. e.preventDefault();
  38. this.context.router.push(`/statuses/tag/${hashtag}`);
  39. }
  40. },
  41. onNormalClick (e) {
  42. e.stopPropagation();
  43. },
  44. render () {
  45. const content = { __html: this.props.status.get('content') };
  46. return <div className='status__content' dangerouslySetInnerHTML={content} />;
  47. },
  48. });
  49. export default StatusContent;