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.

188 lines
5.8 KiB

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