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.

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