闭社主体 forked from https://github.com/tootsuite/mastodon
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.

156 lines
5.0 KiB

8 years ago
8 years ago
8 years ago
  1. import React from 'react';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import escapeTextContentForBrowser from 'escape-html';
  4. import PropTypes from 'prop-types';
  5. import emojify from '../emoji';
  6. import { isRtl } from '../rtl';
  7. import { FormattedMessage } from 'react-intl';
  8. import Permalink from './permalink';
  9. class StatusContent extends React.PureComponent {
  10. static contextTypes = {
  11. router: PropTypes.object,
  12. };
  13. static propTypes = {
  14. status: ImmutablePropTypes.map.isRequired,
  15. onClick: PropTypes.func,
  16. };
  17. state = {
  18. hidden: true,
  19. };
  20. componentDidMount () {
  21. const node = this.node;
  22. const links = node.querySelectorAll('a');
  23. for (var i = 0; i < links.length; ++i) {
  24. let link = links[i];
  25. let mention = this.props.status.get('mentions').find(item => link.href === item.get('url'));
  26. 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')));
  27. if (mention) {
  28. link.addEventListener('click', this.onMentionClick.bind(this, mention), false);
  29. link.setAttribute('title', mention.get('acct'));
  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 {
  33. link.setAttribute('target', '_blank');
  34. link.setAttribute('rel', 'noopener');
  35. link.setAttribute('title', link.href);
  36. }
  37. }
  38. }
  39. onMentionClick = (mention, e) => {
  40. if (e.button === 0) {
  41. e.preventDefault();
  42. this.context.router.push(`/accounts/${mention.get('id')}`);
  43. }
  44. }
  45. onHashtagClick = (hashtag, e) => {
  46. hashtag = hashtag.replace(/^#/, '').toLowerCase();
  47. if (e.button === 0) {
  48. e.preventDefault();
  49. this.context.router.push(`/timelines/tag/${hashtag}`);
  50. }
  51. }
  52. handleMouseDown = (e) => {
  53. this.startXY = [e.clientX, e.clientY];
  54. }
  55. handleMouseUp = (e) => {
  56. const [ startX, startY ] = this.startXY;
  57. const [ deltaX, deltaY ] = [Math.abs(e.clientX - startX), Math.abs(e.clientY - startY)];
  58. if (e.target.localName === 'button' || e.target.localName === 'span' || e.target.localName === 'a' || (e.target.parentNode && e.target.parentNode.localName === 'a')) {
  59. return;
  60. }
  61. if (deltaX + deltaY < 5 && e.button === 0 && this.props.onClick) {
  62. this.props.onClick();
  63. }
  64. this.startXY = null;
  65. }
  66. handleSpoilerClick = (e) => {
  67. e.preventDefault();
  68. this.setState({ hidden: !this.state.hidden });
  69. }
  70. setRef = (c) => {
  71. this.node = c;
  72. }
  73. render () {
  74. const { status } = this.props;
  75. const { hidden } = this.state;
  76. const content = { __html: emojify(status.get('content')) };
  77. const spoilerContent = { __html: emojify(escapeTextContentForBrowser(status.get('spoiler_text', ''))) };
  78. const directionStyle = { direction: 'ltr' };
  79. if (isRtl(status.get('content'))) {
  80. directionStyle.direction = 'rtl';
  81. }
  82. if (status.get('spoiler_text').length > 0) {
  83. let mentionsPlaceholder = '';
  84. const mentionLinks = status.get('mentions').map(item => (
  85. <Permalink to={`/accounts/${item.get('id')}`} href={item.get('url')} key={item.get('id')} className='mention'>
  86. @<span>{item.get('username')}</span>
  87. </Permalink>
  88. )).reduce((aggregate, item) => [...aggregate, item, ' '], []);
  89. const toggleText = hidden ? <FormattedMessage id='status.show_more' defaultMessage='Show more' /> : <FormattedMessage id='status.show_less' defaultMessage='Show less' />;
  90. if (hidden) {
  91. mentionsPlaceholder = <div>{mentionLinks}</div>;
  92. }
  93. return (
  94. <div className='status__content' ref={this.setRef} onMouseDown={this.handleMouseDown} onMouseUp={this.handleMouseUp}>
  95. <p style={{ marginBottom: hidden && status.get('mentions').isEmpty() ? '0px' : null }}>
  96. <span dangerouslySetInnerHTML={spoilerContent} />
  97. {' '}
  98. <button tabIndex='0' className='status__content__spoiler-link' onClick={this.handleSpoilerClick}>{toggleText}</button>
  99. </p>
  100. {mentionsPlaceholder}
  101. <div className={`status__content__text ${!hidden ? 'status__content__text--visible' : ''}`} style={directionStyle} dangerouslySetInnerHTML={content} />
  102. </div>
  103. );
  104. } else if (this.props.onClick) {
  105. return (
  106. <div
  107. ref={this.setRef}
  108. className='status__content'
  109. style={directionStyle}
  110. onMouseDown={this.handleMouseDown}
  111. onMouseUp={this.handleMouseUp}
  112. dangerouslySetInnerHTML={content}
  113. />
  114. );
  115. } else {
  116. return (
  117. <div
  118. ref={this.setRef}
  119. className='status__content status__content--no-action'
  120. style={directionStyle}
  121. dangerouslySetInnerHTML={content}
  122. />
  123. );
  124. }
  125. }
  126. }
  127. export default StatusContent;