The code powering m.abunchtell.com https://m.abunchtell.com
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

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