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.
240 lines
6.5 KiB
240 lines
6.5 KiB
7 years ago
|
// Package imports //
|
||
8 years ago
|
import React from 'react';
|
||
8 years ago
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
||
8 years ago
|
import escapeTextContentForBrowser from 'escape-html';
|
||
8 years ago
|
import PropTypes from 'prop-types';
|
||
8 years ago
|
import { FormattedMessage } from 'react-intl';
|
||
7 years ago
|
|
||
|
// Mastodon imports //
|
||
|
import emojify from '../../../mastodon/emoji';
|
||
|
import { isRtl } from '../../../mastodon/rtl';
|
||
|
import Permalink from '../../../mastodon/components/permalink';
|
||
8 years ago
|
|
||
8 years ago
|
export default class StatusContent extends React.PureComponent {
|
||
8 years ago
|
|
||
8 years ago
|
static contextTypes = {
|
||
8 years ago
|
router: PropTypes.object,
|
||
8 years ago
|
};
|
||
|
|
||
|
static propTypes = {
|
||
|
status: ImmutablePropTypes.map.isRequired,
|
||
7 years ago
|
expanded: PropTypes.oneOf([true, false, null]),
|
||
|
setExpansion: PropTypes.func,
|
||
8 years ago
|
onHeightUpdate: PropTypes.func,
|
||
7 years ago
|
media: PropTypes.element,
|
||
7 years ago
|
mediaIcon: PropTypes.string,
|
||
7 years ago
|
parseClick: PropTypes.func,
|
||
8 years ago
|
};
|
||
|
|
||
|
state = {
|
||
8 years ago
|
hidden: true,
|
||
8 years ago
|
};
|
||
8 years ago
|
|
||
|
componentDidMount () {
|
||
8 years ago
|
const node = this.node;
|
||
8 years ago
|
const links = node.querySelectorAll('a');
|
||
|
|
||
|
for (var i = 0; i < links.length; ++i) {
|
||
|
let link = links[i];
|
||
|
let mention = this.props.status.get('mentions').find(item => link.href === item.get('url'));
|
||
|
|
||
|
if (mention) {
|
||
8 years ago
|
link.addEventListener('click', this.onMentionClick.bind(this, mention), false);
|
||
8 years ago
|
link.setAttribute('title', mention.get('acct'));
|
||
8 years ago
|
} else if (link.textContent[0] === '#' || (link.previousSibling && link.previousSibling.textContent && link.previousSibling.textContent[link.previousSibling.textContent.length - 1] === '#')) {
|
||
8 years ago
|
link.addEventListener('click', this.onHashtagClick.bind(this, link.text), false);
|
||
8 years ago
|
} else {
|
||
7 years ago
|
link.addEventListener('click', this.onLinkClick.bind(this), false);
|
||
8 years ago
|
link.setAttribute('target', '_blank');
|
||
|
link.setAttribute('rel', 'noopener');
|
||
8 years ago
|
link.setAttribute('title', link.href);
|
||
8 years ago
|
}
|
||
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
componentDidUpdate () {
|
||
|
if (this.props.onHeightUpdate) {
|
||
|
this.props.onHeightUpdate();
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
onLinkClick = (e) => {
|
||
7 years ago
|
if (this.props.expanded === false) {
|
||
|
if (this.props.parseClick) this.props.parseClick(e);
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
onMentionClick = (mention, e) => {
|
||
7 years ago
|
if (this.props.parseClick) {
|
||
|
this.props.parseClick(e, `/accounts/${mention.get('id')}`);
|
||
8 years ago
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
onHashtagClick = (hashtag, e) => {
|
||
8 years ago
|
hashtag = hashtag.replace(/^#/, '').toLowerCase();
|
||
|
|
||
7 years ago
|
if (this.props.parseClick) {
|
||
|
this.props.parseClick(e, `/timelines/tag/${hashtag}`);
|
||
8 years ago
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
handleMouseDown = (e) => {
|
||
8 years ago
|
this.startXY = [e.clientX, e.clientY];
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
handleMouseUp = (e) => {
|
||
7 years ago
|
const { parseClick } = this.props;
|
||
|
|
||
8 years ago
|
if (!this.startXY) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
8 years ago
|
const [ startX, startY ] = this.startXY;
|
||
|
const [ deltaX, deltaY ] = [Math.abs(e.clientX - startX), Math.abs(e.clientY - startY)];
|
||
|
|
||
8 years ago
|
if (e.target.localName === 'button' || e.target.localName === 'a' || (e.target.parentNode && (e.target.parentNode.localName === 'button' || e.target.parentNode.localName === 'a'))) {
|
||
8 years ago
|
return;
|
||
|
}
|
||
|
|
||
7 years ago
|
if (deltaX + deltaY < 5 && e.button === 0 && parseClick) {
|
||
|
parseClick(e);
|
||
8 years ago
|
}
|
||
|
|
||
|
this.startXY = null;
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
handleSpoilerClick = (e) => {
|
||
8 years ago
|
e.preventDefault();
|
||
8 years ago
|
|
||
7 years ago
|
if (this.props.setExpansion) {
|
||
|
this.props.setExpansion(this.props.expanded ? null : true);
|
||
8 years ago
|
} else {
|
||
|
this.setState({ hidden: !this.state.hidden });
|
||
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
setRef = (c) => {
|
||
8 years ago
|
this.node = c;
|
||
|
}
|
||
|
|
||
8 years ago
|
render () {
|
||
7 years ago
|
const { status, media, mediaIcon } = this.props;
|
||
8 years ago
|
|
||
7 years ago
|
const hidden = (
|
||
|
this.props.setExpansion ?
|
||
|
!this.props.expanded :
|
||
|
this.state.hidden
|
||
|
);
|
||
8 years ago
|
|
||
8 years ago
|
const content = { __html: emojify(status.get('content')) };
|
||
7 years ago
|
const spoilerContent = {
|
||
|
__html: emojify(escapeTextContentForBrowser(
|
||
|
status.get('spoiler_text', '')
|
||
|
)),
|
||
|
};
|
||
8 years ago
|
const directionStyle = { direction: 'ltr' };
|
||
|
|
||
8 years ago
|
if (isRtl(status.get('search_index'))) {
|
||
8 years ago
|
directionStyle.direction = 'rtl';
|
||
|
}
|
||
8 years ago
|
|
||
|
if (status.get('spoiler_text').length > 0) {
|
||
8 years ago
|
let mentionsPlaceholder = '';
|
||
|
|
||
8 years ago
|
const mentionLinks = status.get('mentions').map(item => (
|
||
7 years ago
|
<Permalink
|
||
|
to={`/accounts/${item.get('id')}`}
|
||
|
href={item.get('url')}
|
||
|
key={item.get('id')}
|
||
|
className='mention'
|
||
|
>
|
||
8 years ago
|
@<span>{item.get('username')}</span>
|
||
|
</Permalink>
|
||
8 years ago
|
)).reduce((aggregate, item) => [...aggregate, item, ' '], []);
|
||
8 years ago
|
|
||
7 years ago
|
const toggleText = hidden ? [
|
||
|
<FormattedMessage
|
||
|
id='status.show_more'
|
||
|
defaultMessage='Show more'
|
||
|
key='0'
|
||
|
/>,
|
||
|
mediaIcon ? (
|
||
|
<i
|
||
|
className={
|
||
|
`fa fa-fw fa-${mediaIcon} status__content__spoiler-icon`
|
||
|
}
|
||
|
aria-hidden='true'
|
||
|
key='1'
|
||
|
/>
|
||
|
) : null,
|
||
|
] : [
|
||
|
<FormattedMessage
|
||
|
id='status.show_less'
|
||
|
defaultMessage='Show less'
|
||
|
key='0'
|
||
|
/>,
|
||
|
];
|
||
8 years ago
|
|
||
8 years ago
|
if (hidden) {
|
||
|
mentionsPlaceholder = <div>{mentionLinks}</div>;
|
||
|
}
|
||
|
|
||
8 years ago
|
return (
|
||
7 years ago
|
<div className='status__content status__content--with-action' ref={this.setRef}>
|
||
|
<p
|
||
|
style={{ marginBottom: hidden && status.get('mentions').isEmpty() ? '0px' : null }}
|
||
|
onMouseDown={this.handleMouseDown}
|
||
|
onMouseUp={this.handleMouseUp}
|
||
|
>
|
||
8 years ago
|
<span dangerouslySetInnerHTML={spoilerContent} />
|
||
|
{' '}
|
||
7 years ago
|
<button tabIndex='0' className='status__content__spoiler-link' onClick={this.handleSpoilerClick}>
|
||
7 years ago
|
{toggleText}
|
||
7 years ago
|
</button>
|
||
8 years ago
|
</p>
|
||
|
|
||
8 years ago
|
{mentionsPlaceholder}
|
||
|
|
||
8 years ago
|
<div className={`status__content__spoiler ${!hidden ? 'status__content__spoiler--visible' : ''}`}>
|
||
7 years ago
|
<div
|
||
|
style={directionStyle}
|
||
|
onMouseDown={this.handleMouseDown}
|
||
|
onMouseUp={this.handleMouseUp}
|
||
|
dangerouslySetInnerHTML={content}
|
||
|
/>
|
||
7 years ago
|
{media}
|
||
8 years ago
|
</div>
|
||
|
|
||
8 years ago
|
</div>
|
||
|
);
|
||
7 years ago
|
} else if (this.props.parseClick) {
|
||
8 years ago
|
return (
|
||
|
<div
|
||
8 years ago
|
ref={this.setRef}
|
||
8 years ago
|
className='status__content status__content--with-action'
|
||
8 years ago
|
style={directionStyle}
|
||
8 years ago
|
>
|
||
7 years ago
|
<div
|
||
|
onMouseDown={this.handleMouseDown}
|
||
|
onMouseUp={this.handleMouseUp}
|
||
|
dangerouslySetInnerHTML={content}
|
||
|
/>
|
||
7 years ago
|
{media}
|
||
8 years ago
|
</div>
|
||
8 years ago
|
);
|
||
8 years ago
|
} else {
|
||
|
return (
|
||
|
<div
|
||
8 years ago
|
ref={this.setRef}
|
||
8 years ago
|
className='status__content'
|
||
8 years ago
|
style={directionStyle}
|
||
8 years ago
|
>
|
||
|
<div dangerouslySetInnerHTML={content} />
|
||
7 years ago
|
{media}
|
||
8 years ago
|
</div>
|
||
8 years ago
|
);
|
||
8 years ago
|
}
|
||
8 years ago
|
}
|
||
|
|
||
|
}
|