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.
166 lines
5.3 KiB
166 lines
5.3 KiB
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 emojify from '../emoji';
|
||
8 years ago
|
import { isRtl } from '../rtl';
|
||
8 years ago
|
import { FormattedMessage } from 'react-intl';
|
||
8 years ago
|
import Permalink from './permalink';
|
||
8 years ago
|
|
||
8 years ago
|
class StatusContent extends React.PureComponent {
|
||
8 years ago
|
|
||
8 years ago
|
constructor (props, context) {
|
||
|
super(props, context);
|
||
|
this.state = {
|
||
8 years ago
|
hidden: true
|
||
|
};
|
||
8 years ago
|
this.onMentionClick = this.onMentionClick.bind(this);
|
||
|
this.onHashtagClick = this.onHashtagClick.bind(this);
|
||
|
this.handleMouseDown = this.handleMouseDown.bind(this)
|
||
|
this.handleMouseUp = this.handleMouseUp.bind(this);
|
||
|
this.handleSpoilerClick = this.handleSpoilerClick.bind(this);
|
||
8 years ago
|
this.setRef = this.setRef.bind(this);
|
||
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'));
|
||
8 years ago
|
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')));
|
||
8 years ago
|
|
||
|
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 if (media) {
|
||
|
link.innerHTML = '<i class="fa fa-fw fa-photo"></i>';
|
||
8 years ago
|
} else {
|
||
|
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
|
onMentionClick (mention, e) {
|
||
8 years ago
|
if (e.button === 0) {
|
||
|
e.preventDefault();
|
||
|
this.context.router.push(`/accounts/${mention.get('id')}`);
|
||
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
onHashtagClick (hashtag, e) {
|
||
|
hashtag = hashtag.replace(/^#/, '').toLowerCase();
|
||
|
|
||
|
if (e.button === 0) {
|
||
|
e.preventDefault();
|
||
8 years ago
|
this.context.router.push(`/timelines/tag/${hashtag}`);
|
||
8 years ago
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
handleMouseDown (e) {
|
||
|
this.startXY = [e.clientX, e.clientY];
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
|
handleMouseUp (e) {
|
||
|
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 === 'a' || (e.target.parentNode && e.target.parentNode.localName === 'a')) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
8 years ago
|
if (deltaX + deltaY < 5 && e.button === 0) {
|
||
8 years ago
|
this.props.onClick();
|
||
|
}
|
||
|
|
||
|
this.startXY = null;
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
handleSpoilerClick (e) {
|
||
|
e.preventDefault();
|
||
8 years ago
|
this.setState({ hidden: !this.state.hidden });
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
setRef (c) {
|
||
|
this.node = c;
|
||
|
}
|
||
|
|
||
8 years ago
|
render () {
|
||
8 years ago
|
const { status } = this.props;
|
||
8 years ago
|
const { hidden } = this.state;
|
||
8 years ago
|
|
||
8 years ago
|
const content = { __html: emojify(status.get('content')) };
|
||
8 years ago
|
const spoilerContent = { __html: emojify(escapeTextContentForBrowser(status.get('spoiler_text', ''))) };
|
||
8 years ago
|
const directionStyle = { direction: 'ltr' };
|
||
|
|
||
|
if (isRtl(status.get('content'))) {
|
||
|
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 => (
|
||
|
<Permalink to={`/accounts/${item.get('id')}`} href={item.get('url')} key={item.get('id')} className='mention'>
|
||
|
@<span>{item.get('username')}</span>
|
||
|
</Permalink>
|
||
|
)).reduce((aggregate, item) => [...aggregate, item, ' '], [])
|
||
|
|
||
8 years ago
|
const toggleText = hidden ? <FormattedMessage id='status.show_more' defaultMessage='Show more' /> : <FormattedMessage id='status.show_less' defaultMessage='Show less' />;
|
||
8 years ago
|
|
||
8 years ago
|
if (hidden) {
|
||
|
mentionsPlaceholder = <div>{mentionLinks}</div>;
|
||
|
}
|
||
|
|
||
8 years ago
|
return (
|
||
8 years ago
|
<div className='status__content' onMouseDown={this.handleMouseDown} onMouseUp={this.handleMouseUp}>
|
||
8 years ago
|
<p style={{ marginBottom: hidden && status.get('mentions').size === 0 ? '0px' : '' }} >
|
||
8 years ago
|
<span dangerouslySetInnerHTML={spoilerContent} /> <a tabIndex='0' className='status__content__spoiler-link' role='button' onClick={this.handleSpoilerClick}>{toggleText}</a>
|
||
8 years ago
|
</p>
|
||
|
|
||
8 years ago
|
{mentionsPlaceholder}
|
||
|
|
||
8 years ago
|
<div ref={this.setRef} style={{ display: hidden ? 'none' : 'block', ...directionStyle }} dangerouslySetInnerHTML={content} />
|
||
8 years ago
|
</div>
|
||
|
);
|
||
8 years ago
|
} else if (this.props.onClick) {
|
||
8 years ago
|
return (
|
||
|
<div
|
||
8 years ago
|
ref={this.setRef}
|
||
8 years ago
|
className='status__content'
|
||
8 years ago
|
style={{ ...directionStyle }}
|
||
8 years ago
|
onMouseDown={this.handleMouseDown}
|
||
|
onMouseUp={this.handleMouseUp}
|
||
|
dangerouslySetInnerHTML={content}
|
||
|
/>
|
||
|
);
|
||
8 years ago
|
} else {
|
||
|
return (
|
||
|
<div
|
||
8 years ago
|
ref={this.setRef}
|
||
8 years ago
|
className='status__content status__content--no-action'
|
||
8 years ago
|
style={{ ...directionStyle }}
|
||
|
dangerouslySetInnerHTML={content}
|
||
|
/>
|
||
|
);
|
||
8 years ago
|
}
|
||
8 years ago
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
StatusContent.contextTypes = {
|
||
|
router: PropTypes.object
|
||
|
};
|
||
8 years ago
|
|
||
8 years ago
|
StatusContent.propTypes = {
|
||
|
status: ImmutablePropTypes.map.isRequired,
|
||
|
onClick: PropTypes.func
|
||
|
};
|
||
8 years ago
|
|
||
|
export default StatusContent;
|