logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

status_content.js (5959B)


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