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.

159 lines
4.9 KiB

7 years ago
7 years ago
7 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 === '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(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
  95. ref={this.setRef}
  96. className='status__content'
  97. onMouseDown={this.handleMouseDown}
  98. onMouseUp={this.handleMouseUp}
  99. >
  100. <p style={{ marginBottom: hidden && status.get('mentions').size === 0 ? '0px' : '' }} >
  101. <span dangerouslySetInnerHTML={spoilerContent} /> <button tabIndex='0' className='status__content__spoiler-link' onClick={this.handleSpoilerClick}>{toggleText}</button>
  102. </p>
  103. {mentionsPlaceholder}
  104. <div style={{ display: hidden ? 'none' : 'block', ...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;