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.

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