The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

176 lines
5.3 KiB

  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. expanded: PropTypes.bool,
  16. onExpandedToggle: PropTypes.func,
  17. onHeightUpdate: PropTypes.func,
  18. onClick: PropTypes.func,
  19. };
  20. state = {
  21. hidden: true,
  22. };
  23. componentDidMount () {
  24. const node = this.node;
  25. const links = node.querySelectorAll('a');
  26. for (var i = 0; i < links.length; ++i) {
  27. let link = links[i];
  28. let mention = this.props.status.get('mentions').find(item => link.href === item.get('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. componentDidUpdate () {
  42. if (this.props.onHeightUpdate) {
  43. this.props.onHeightUpdate();
  44. }
  45. }
  46. onMentionClick = (mention, e) => {
  47. if (e.button === 0) {
  48. e.preventDefault();
  49. this.context.router.history.push(`/accounts/${mention.get('id')}`);
  50. }
  51. }
  52. onHashtagClick = (hashtag, e) => {
  53. hashtag = hashtag.replace(/^#/, '').toLowerCase();
  54. if (e.button === 0) {
  55. e.preventDefault();
  56. this.context.router.history.push(`/timelines/tag/${hashtag}`);
  57. }
  58. }
  59. handleMouseDown = (e) => {
  60. this.startXY = [e.clientX, e.clientY];
  61. }
  62. handleMouseUp = (e) => {
  63. if (!this.startXY) {
  64. return;
  65. }
  66. const [ startX, startY ] = this.startXY;
  67. const [ deltaX, deltaY ] = [Math.abs(e.clientX - startX), Math.abs(e.clientY - startY)];
  68. if (e.target.localName === 'button' || e.target.localName === 'a' || (e.target.parentNode && (e.target.parentNode.localName === 'button' || e.target.parentNode.localName === 'a'))) {
  69. return;
  70. }
  71. if (deltaX + deltaY < 5 && e.button === 0 && this.props.onClick) {
  72. this.props.onClick();
  73. }
  74. this.startXY = null;
  75. }
  76. handleSpoilerClick = (e) => {
  77. e.preventDefault();
  78. if (this.props.onExpandedToggle) {
  79. // The parent manages the state
  80. this.props.onExpandedToggle();
  81. } else {
  82. this.setState({ hidden: !this.state.hidden });
  83. }
  84. }
  85. setRef = (c) => {
  86. this.node = c;
  87. }
  88. render () {
  89. const { status } = this.props;
  90. const hidden = this.props.onExpandedToggle ? !this.props.expanded : this.state.hidden;
  91. const content = { __html: emojify(status.get('content')) };
  92. const spoilerContent = { __html: emojify(escapeTextContentForBrowser(status.get('spoiler_text', ''))) };
  93. const directionStyle = { direction: 'ltr' };
  94. if (isRtl(status.get('search_index'))) {
  95. directionStyle.direction = 'rtl';
  96. }
  97. if (status.get('spoiler_text').length > 0) {
  98. let mentionsPlaceholder = '';
  99. const mentionLinks = status.get('mentions').map(item => (
  100. <Permalink to={`/accounts/${item.get('id')}`} href={item.get('url')} key={item.get('id')} className='mention'>
  101. @<span>{item.get('username')}</span>
  102. </Permalink>
  103. )).reduce((aggregate, item) => [...aggregate, item, ' '], []);
  104. const toggleText = hidden ? <FormattedMessage id='status.show_more' defaultMessage='Show more' /> : <FormattedMessage id='status.show_less' defaultMessage='Show less' />;
  105. if (hidden) {
  106. mentionsPlaceholder = <div>{mentionLinks}</div>;
  107. }
  108. return (
  109. <div className='status__content status__content--with_action' ref={this.setRef} onMouseDown={this.handleMouseDown} onMouseUp={this.handleMouseUp}>
  110. <p style={{ marginBottom: hidden && status.get('mentions').isEmpty() ? '0px' : null }}>
  111. <span dangerouslySetInnerHTML={spoilerContent} />
  112. {' '}
  113. <button tabIndex='0' className='status__content__spoiler-link' onClick={this.handleSpoilerClick}>{toggleText}</button>
  114. </p>
  115. {mentionsPlaceholder}
  116. <div className={`status__content__text ${!hidden ? 'status__content__text--visible' : ''}`} style={directionStyle} dangerouslySetInnerHTML={content} />
  117. </div>
  118. );
  119. } else if (this.props.onClick) {
  120. return (
  121. <div
  122. ref={this.setRef}
  123. className='status__content status__content--with-action'
  124. style={directionStyle}
  125. onMouseDown={this.handleMouseDown}
  126. onMouseUp={this.handleMouseUp}
  127. dangerouslySetInnerHTML={content}
  128. />
  129. );
  130. } else {
  131. return (
  132. <div
  133. ref={this.setRef}
  134. className='status__content'
  135. style={directionStyle}
  136. dangerouslySetInnerHTML={content}
  137. />
  138. );
  139. }
  140. }
  141. }
  142. export default StatusContent;