闭社主体 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.

160 lines
5.1 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. if (!this.startXY) {
  57. return;
  58. }
  59. const [ startX, startY ] = this.startXY;
  60. const [ deltaX, deltaY ] = [Math.abs(e.clientX - startX), Math.abs(e.clientY - startY)];
  61. if (e.target.localName === 'button' || e.target.localName === 'span' || e.target.localName === 'a' || (e.target.parentNode && e.target.parentNode.localName === 'a')) {
  62. return;
  63. }
  64. if (deltaX + deltaY < 5 && e.button === 0 && this.props.onClick) {
  65. this.props.onClick();
  66. }
  67. this.startXY = null;
  68. }
  69. handleSpoilerClick = (e) => {
  70. e.preventDefault();
  71. this.setState({ hidden: !this.state.hidden });
  72. }
  73. setRef = (c) => {
  74. this.node = c;
  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. const directionStyle = { direction: 'ltr' };
  82. if (isRtl(status.get('search_index'))) {
  83. directionStyle.direction = 'rtl';
  84. }
  85. if (status.get('spoiler_text').length > 0) {
  86. let mentionsPlaceholder = '';
  87. const mentionLinks = status.get('mentions').map(item => (
  88. <Permalink to={`/accounts/${item.get('id')}`} href={item.get('url')} key={item.get('id')} className='mention'>
  89. @<span>{item.get('username')}</span>
  90. </Permalink>
  91. )).reduce((aggregate, item) => [...aggregate, item, ' '], []);
  92. const toggleText = hidden ? <FormattedMessage id='status.show_more' defaultMessage='Show more' /> : <FormattedMessage id='status.show_less' defaultMessage='Show less' />;
  93. if (hidden) {
  94. mentionsPlaceholder = <div>{mentionLinks}</div>;
  95. }
  96. return (
  97. <div className='status__content' ref={this.setRef} onMouseDown={this.handleMouseDown} onMouseUp={this.handleMouseUp}>
  98. <p style={{ marginBottom: hidden && status.get('mentions').isEmpty() ? '0px' : null }}>
  99. <span dangerouslySetInnerHTML={spoilerContent} />
  100. {' '}
  101. <button tabIndex='0' className='status__content__spoiler-link' onClick={this.handleSpoilerClick}>{toggleText}</button>
  102. </p>
  103. {mentionsPlaceholder}
  104. <div className={`status__content__text ${!hidden ? 'status__content__text--visible' : ''}`} style={directionStyle} dangerouslySetInnerHTML={content} />
  105. </div>
  106. );
  107. } else if (this.props.onClick) {
  108. return (
  109. <div
  110. ref={this.setRef}
  111. className='status__content'
  112. style={directionStyle}
  113. onMouseDown={this.handleMouseDown}
  114. onMouseUp={this.handleMouseUp}
  115. dangerouslySetInnerHTML={content}
  116. />
  117. );
  118. } else {
  119. return (
  120. <div
  121. ref={this.setRef}
  122. className='status__content status__content--no-action'
  123. style={directionStyle}
  124. dangerouslySetInnerHTML={content}
  125. />
  126. );
  127. }
  128. }
  129. }
  130. export default StatusContent;