The code powering m.abunchtell.com https://m.abunchtell.com
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

153 satır
4.9 KiB

  1. import ImmutablePropTypes from 'react-immutable-proptypes';
  2. import PureRenderMixin from 'react-addons-pure-render-mixin';
  3. import escapeTextContentForBrowser from 'escape-html';
  4. import emojify from '../emoji';
  5. import { isRtl } from '../rtl';
  6. import { FormattedMessage } from 'react-intl';
  7. import Permalink from './permalink';
  8. const StatusContent = React.createClass({
  9. contextTypes: {
  10. router: React.PropTypes.object
  11. },
  12. propTypes: {
  13. status: ImmutablePropTypes.map.isRequired,
  14. onClick: React.PropTypes.func
  15. },
  16. getInitialState () {
  17. return {
  18. hidden: true
  19. };
  20. },
  21. mixins: [PureRenderMixin],
  22. componentDidMount () {
  23. const node = ReactDOM.findDOMNode(this);
  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. }
  40. }
  41. },
  42. onMentionClick (mention, e) {
  43. if (e.button === 0) {
  44. e.preventDefault();
  45. this.context.router.push(`/accounts/${mention.get('id')}`);
  46. }
  47. },
  48. onHashtagClick (hashtag, e) {
  49. hashtag = hashtag.replace(/^#/, '').toLowerCase();
  50. if (e.button === 0) {
  51. e.preventDefault();
  52. this.context.router.push(`/timelines/tag/${hashtag}`);
  53. }
  54. },
  55. handleMouseDown (e) {
  56. this.startXY = [e.clientX, e.clientY];
  57. },
  58. handleMouseUp (e) {
  59. const [ startX, startY ] = this.startXY;
  60. const [ deltaX, deltaY ] = [Math.abs(e.clientX - startX), Math.abs(e.clientY - startY)];
  61. if (e.target.localName === 'a' || (e.target.parentNode && e.target.parentNode.localName === 'a')) {
  62. return;
  63. }
  64. if (deltaX + deltaY < 5 && e.button === 0) {
  65. this.props.onClick();
  66. }
  67. this.startXY = null;
  68. },
  69. handleSpoilerClick (e) {
  70. e.preventDefault();
  71. this.setState({ hidden: !this.state.hidden });
  72. },
  73. render () {
  74. const { status } = this.props;
  75. const { hidden } = this.state;
  76. const content = { __html: emojify(status.get('content')).replace(/\n/g, '') };
  77. const spoilerContent = { __html: emojify(escapeTextContentForBrowser(status.get('spoiler_text', ''))) };
  78. const directionStyle = { direction: 'ltr' };
  79. if (isRtl(status.get('content'))) {
  80. directionStyle.direction = 'rtl';
  81. }
  82. if (status.get('spoiler_text').length > 0) {
  83. let mentionsPlaceholder = '';
  84. const mentionLinks = status.get('mentions').map(item => (
  85. <Permalink to={`/accounts/${item.get('id')}`} href={item.get('url')} key={item.get('id')} className='mention'>
  86. @<span>{item.get('username')}</span>
  87. </Permalink>
  88. )).reduce((aggregate, item) => [...aggregate, item, ' '], [])
  89. const toggleText = hidden ? <FormattedMessage id='status.show_more' defaultMessage='Show more' /> : <FormattedMessage id='status.show_less' defaultMessage='Show less' />;
  90. if (hidden) {
  91. mentionsPlaceholder = <div>{mentionLinks}</div>;
  92. }
  93. return (
  94. <div className='status__content' style={{ cursor: 'pointer' }} onMouseDown={this.handleMouseDown} onMouseUp={this.handleMouseUp}>
  95. <p style={{ marginBottom: hidden && status.get('mentions').size === 0 ? '0px' : '' }} >
  96. <span dangerouslySetInnerHTML={spoilerContent} /> <a className='status__content__spoiler-link' onClick={this.handleSpoilerClick}>{toggleText}</a>
  97. </p>
  98. {mentionsPlaceholder}
  99. <div style={{ display: hidden ? 'none' : 'block', ...directionStyle }} dangerouslySetInnerHTML={content} />
  100. </div>
  101. );
  102. } else if (this.props.onClick) {
  103. return (
  104. <div
  105. className='status__content'
  106. style={{ cursor: 'pointer', ...directionStyle }}
  107. onMouseDown={this.handleMouseDown}
  108. onMouseUp={this.handleMouseUp}
  109. dangerouslySetInnerHTML={content}
  110. />
  111. );
  112. } else {
  113. return (
  114. <div
  115. className='status__content'
  116. style={{ ...directionStyle }}
  117. dangerouslySetInnerHTML={content}
  118. />
  119. );
  120. }
  121. },
  122. });
  123. export default StatusContent;