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.

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