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.

250 lines
6.7 KiB

8 years ago
8 years ago
8 years ago
  1. import React from 'react';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import PropTypes from 'prop-types';
  4. import { isRtl } from 'flavours/glitch/util/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 propTypes = {
  10. status: ImmutablePropTypes.map.isRequired,
  11. expanded: PropTypes.bool,
  12. setExpansion: PropTypes.func,
  13. media: PropTypes.element,
  14. mediaIcon: PropTypes.string,
  15. parseClick: PropTypes.func,
  16. disabled: PropTypes.bool,
  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.addEventListener('click', this.onLinkClick.bind(this), false);
  41. link.setAttribute('title', link.href);
  42. }
  43. link.setAttribute('target', '_blank');
  44. link.setAttribute('rel', 'noopener');
  45. }
  46. }
  47. componentDidMount () {
  48. this._updateStatusLinks();
  49. }
  50. componentDidUpdate () {
  51. this._updateStatusLinks();
  52. }
  53. onLinkClick = (e) => {
  54. if (this.props.expanded === false) {
  55. if (this.props.parseClick) this.props.parseClick(e);
  56. }
  57. }
  58. onMentionClick = (mention, e) => {
  59. if (this.props.parseClick) {
  60. this.props.parseClick(e, `/accounts/${mention.get('id')}`);
  61. }
  62. }
  63. onHashtagClick = (hashtag, e) => {
  64. hashtag = hashtag.replace(/^#/, '').toLowerCase();
  65. if (this.props.parseClick) {
  66. this.props.parseClick(e, `/timelines/tag/${hashtag}`);
  67. }
  68. }
  69. handleMouseDown = (e) => {
  70. this.startXY = [e.clientX, e.clientY];
  71. }
  72. handleMouseUp = (e) => {
  73. const { parseClick } = this.props;
  74. if (!this.startXY) {
  75. return;
  76. }
  77. const [ startX, startY ] = this.startXY;
  78. const [ deltaX, deltaY ] = [Math.abs(e.clientX - startX), Math.abs(e.clientY - startY)];
  79. if (e.target.localName === 'button' || e.target.localName === 'a' || (e.target.parentNode && (e.target.parentNode.localName === 'button' || e.target.parentNode.localName === 'a'))) {
  80. return;
  81. }
  82. if (deltaX + deltaY < 5 && e.button === 0 && parseClick) {
  83. parseClick(e);
  84. }
  85. this.startXY = null;
  86. }
  87. handleSpoilerClick = (e) => {
  88. e.preventDefault();
  89. if (this.props.setExpansion) {
  90. this.props.setExpansion(this.props.expanded ? null : true);
  91. } else {
  92. this.setState({ hidden: !this.state.hidden });
  93. }
  94. }
  95. setRef = (c) => {
  96. this.node = c;
  97. }
  98. render () {
  99. const {
  100. status,
  101. media,
  102. mediaIcon,
  103. parseClick,
  104. disabled,
  105. } = this.props;
  106. const hidden = this.props.setExpansion ? !this.props.expanded : this.state.hidden;
  107. const content = { __html: status.get('contentHtml') };
  108. const spoilerContent = { __html: status.get('spoilerHtml') };
  109. const directionStyle = { direction: 'ltr' };
  110. const classNames = classnames('status__content', {
  111. 'status__content--with-action': parseClick && !disabled,
  112. 'status__content--with-spoiler': status.get('spoiler_text').length > 0,
  113. });
  114. if (isRtl(status.get('search_index'))) {
  115. directionStyle.direction = 'rtl';
  116. }
  117. if (status.get('spoiler_text').length > 0) {
  118. let mentionsPlaceholder = '';
  119. const mentionLinks = status.get('mentions').map(item => (
  120. <Permalink
  121. to={`/accounts/${item.get('id')}`}
  122. href={item.get('url')}
  123. key={item.get('id')}
  124. className='mention'
  125. >
  126. @<span>{item.get('username')}</span>
  127. </Permalink>
  128. )).reduce((aggregate, item) => [...aggregate, item, ' '], []);
  129. const toggleText = hidden ? [
  130. <FormattedMessage
  131. id='status.show_more'
  132. defaultMessage='Show more'
  133. key='0'
  134. />,
  135. mediaIcon ? (
  136. <i
  137. className={
  138. `fa fa-fw fa-${mediaIcon} status__content__spoiler-icon`
  139. }
  140. aria-hidden='true'
  141. key='1'
  142. />
  143. ) : null,
  144. ] : [
  145. <FormattedMessage
  146. id='status.show_less'
  147. defaultMessage='Show less'
  148. key='0'
  149. />,
  150. ];
  151. if (hidden) {
  152. mentionsPlaceholder = <div>{mentionLinks}</div>;
  153. }
  154. return (
  155. <div className={classNames} tabIndex='0'>
  156. <p
  157. style={{ marginBottom: hidden && status.get('mentions').isEmpty() ? '0px' : null }}
  158. onMouseDown={this.handleMouseDown}
  159. onMouseUp={this.handleMouseUp}
  160. >
  161. <span dangerouslySetInnerHTML={spoilerContent} />
  162. {' '}
  163. <button tabIndex='0' className='status__content__spoiler-link' onClick={this.handleSpoilerClick}>
  164. {toggleText}
  165. </button>
  166. </p>
  167. {mentionsPlaceholder}
  168. <div className={`status__content__spoiler ${!hidden ? 'status__content__spoiler--visible' : ''}`}>
  169. <div
  170. ref={this.setRef}
  171. style={directionStyle}
  172. tabIndex={!hidden ? 0 : null}
  173. onMouseDown={this.handleMouseDown}
  174. onMouseUp={this.handleMouseUp}
  175. dangerouslySetInnerHTML={content}
  176. />
  177. {media}
  178. </div>
  179. </div>
  180. );
  181. } else if (parseClick) {
  182. return (
  183. <div
  184. className={classNames}
  185. style={directionStyle}
  186. tabIndex='0'
  187. >
  188. <div
  189. ref={this.setRef}
  190. onMouseDown={this.handleMouseDown}
  191. onMouseUp={this.handleMouseUp}
  192. dangerouslySetInnerHTML={content}
  193. tabIndex='0'
  194. />
  195. {media}
  196. </div>
  197. );
  198. } else {
  199. return (
  200. <div
  201. className='status__content'
  202. style={directionStyle}
  203. tabIndex='0'
  204. >
  205. <div ref={this.setRef} dangerouslySetInnerHTML={content} tabIndex='0' />
  206. {media}
  207. </div>
  208. );
  209. }
  210. }
  211. }