gab-social/app/javascript/gabsocial/components/status_content.js

343 lines
9.2 KiB
JavaScript
Raw Normal View History

2020-03-26 03:11:32 +00:00
import ImmutablePropTypes from 'react-immutable-proptypes'
import ImmutablePureComponent from 'react-immutable-pure-component'
2020-04-22 06:00:11 +01:00
import { injectIntl, defineMessages } from 'react-intl'
2020-02-14 00:40:04 +00:00
import classNames from 'classnames/bind'
2020-04-11 23:29:19 +01:00
import { isRtl } from '../utils/rtl'
import Button from './button'
import Icon from './icon'
import Text from './text'
2020-03-26 03:11:32 +00:00
const MAX_HEIGHT = 200
2020-02-08 06:12:01 +00:00
const messages = defineMessages({
2020-05-02 07:25:55 +01:00
show: { id: 'status.show_more', defaultMessage: 'Show' },
hide: { id: 'status.show_less', defaultMessage: 'Hide' },
2020-02-14 00:40:04 +00:00
readMore: { id: 'status.read_more', defaultMessage: 'Read more' },
2020-02-08 06:12:01 +00:00
})
2020-02-19 23:57:07 +00:00
const cx = classNames.bind(_s)
2020-04-11 23:29:19 +01:00
// .emojione {
// margin: -3px 0 0;
// @include size(20px);
// }
2020-02-08 06:12:01 +00:00
export default
@injectIntl
class StatusContent extends ImmutablePureComponent {
static contextTypes = {
router: PropTypes.object,
2020-03-26 03:11:32 +00:00
}
static propTypes = {
status: ImmutablePropTypes.map.isRequired,
expanded: PropTypes.bool,
onExpandedToggle: PropTypes.func,
onClick: PropTypes.func,
collapsable: PropTypes.bool,
2020-02-08 06:12:01 +00:00
intl: PropTypes.object.isRequired,
2020-03-08 23:02:28 +00:00
isComment: PropTypes.bool,
2020-03-26 03:11:32 +00:00
}
state = {
hidden: true,
2020-03-26 03:11:32 +00:00
collapsed: null, // `collapsed: null` indicates that an element doesn't need collapsing, while `true` or `false` indicates that it does (and is/isn't).
}
2020-04-22 06:00:11 +01:00
updateOnProps = [
'status',
'expanded',
'collapsable',
'isComment',
]
2020-03-26 03:11:32 +00:00
_updateStatusLinks() {
const node = this.node
2020-03-26 03:11:32 +00:00
if (!node) return
2020-03-26 03:11:32 +00:00
const links = node.querySelectorAll('a')
2020-03-31 17:04:50 +01:00
for (let i = 0; i < links.length; ++i) {
const link = links[i]
if (link.classList.contains('linked')) {
2020-03-26 03:11:32 +00:00
continue
}
2020-03-31 17:04:50 +01:00
link.classList.add('linked')
link.classList.add(_s.text, _s.colorBrand, _s.cursorPointer, _s.inherit)
2020-03-31 17:04:50 +01:00
const mention = this.props.status.get('mentions').find(item => link.href === `${item.get('url')}`)
if (mention) {
2020-03-26 03:11:32 +00:00
link.addEventListener('click', this.onMentionClick.bind(this, mention), false)
link.setAttribute('title', mention.get('acct'))
} else if (link.textContent[0] === '#' || (link.previousSibling && link.previousSibling.textContent && link.previousSibling.textContent[link.previousSibling.textContent.length - 1] === '#')) {
2020-03-26 03:11:32 +00:00
link.addEventListener('click', this.onHashtagClick.bind(this, link.text), false)
} else {
2020-03-26 03:11:32 +00:00
link.setAttribute('title', link.href)
}
}
if (
this.props.collapsable
&& this.props.onClick
&& this.state.collapsed === null
&& node.clientHeight > MAX_HEIGHT
&& this.props.status.get('spoiler_text').length === 0
) {
2020-03-26 03:11:32 +00:00
this.setState({ collapsed: true })
}
}
2020-03-26 03:11:32 +00:00
componentDidMount() {
this._updateStatusLinks()
}
2020-03-26 03:11:32 +00:00
componentDidUpdate() {
this._updateStatusLinks()
}
onMentionClick = (mention, e) => {
if (this.context.router && e.button === 0 && !(e.ctrlKey || e.metaKey)) {
2020-03-31 17:04:50 +01:00
// e.preventDefault()
// this.context.router.history.push(`/${mention.get('acct')}`)
}
}
onHashtagClick = (hashtag, e) => {
2020-03-26 03:11:32 +00:00
hashtag = hashtag.replace(/^#/, '').toLowerCase()
if (this.context.router && e.button === 0 && !(e.ctrlKey || e.metaKey)) {
2020-03-26 03:11:32 +00:00
e.preventDefault()
this.context.router.history.push(`/tags/${hashtag}`)
}
}
handleMouseDown = (e) => {
2020-03-26 03:11:32 +00:00
this.startXY = [e.clientX, e.clientY]
}
handleMouseUp = (e) => {
2020-03-26 03:11:32 +00:00
if (!this.startXY) return
2020-03-26 03:11:32 +00:00
const [startX, startY] = this.startXY
const [deltaX, deltaY] = [Math.abs(e.clientX - startX), Math.abs(e.clientY - startY)]
if (e.target.localName === 'button' ||
2020-03-26 03:11:32 +00:00
e.target.localName === 'a' ||
(e.target.parentNode && (e.target.parentNode.localName === 'button' || e.target.parentNode.localName === 'a'))) {
return
}
if (deltaX + deltaY < 5 && e.button === 0 && this.props.onClick) {
2020-03-26 03:11:32 +00:00
this.props.onClick()
}
2020-03-26 03:11:32 +00:00
this.startXY = null
}
handleSpoilerClick = (e) => {
2020-03-26 03:11:32 +00:00
e.preventDefault()
if (this.props.onExpandedToggle) {
// The parent manages the state
2020-03-24 04:39:12 +00:00
this.props.onExpandedToggle()
} else {
2020-03-24 04:39:12 +00:00
this.setState({ hidden: !this.state.hidden })
}
}
2020-03-24 04:39:12 +00:00
handleReadMore = (e) => {
2020-03-26 03:11:32 +00:00
e.preventDefault()
this.setState({ collapsed: false })
}
setRef = (c) => {
2020-03-26 03:11:32 +00:00
this.node = c
}
getHtmlContent = () => {
2020-03-26 03:11:32 +00:00
const { status, reblogContent } = this.props
2020-03-26 03:11:32 +00:00
const properContent = status.get('contentHtml')
return reblogContent
2020-04-11 23:29:19 +01:00
? `${reblogContent} <div className='status__quote'>${properContent}</div>`
2020-03-26 03:11:32 +00:00
: properContent
}
2020-03-26 03:11:32 +00:00
render() {
2020-03-24 04:39:12 +00:00
const { status, intl, isComment } = this.props
const { collapsed } = this.state
2020-03-26 03:11:32 +00:00
if (status.get('content').length === 0) return null
2020-03-26 03:11:32 +00:00
const hidden = this.props.onExpandedToggle ? !this.props.expanded : this.state.hidden
2020-03-26 03:11:32 +00:00
const content = { __html: this.getHtmlContent() }
const spoilerContent = { __html: status.get('spoilerHtml') }
const directionStyle = {
direction: isRtl(status.get('search_index')) ? 'rtl' : 'ltr',
}
if (status.get('spoiler_text').length > 0) {
2020-03-26 03:11:32 +00:00
let mentionsPlaceholder = null
const mentionLinks = status.get('mentions').map(item => (
2020-03-26 03:11:32 +00:00
<Button
2020-04-23 07:13:29 +01:00
isText
2020-03-26 03:11:32 +00:00
backgroundColor='none'
to={`/${item.get('acct')}`}
href={`/${item.get('acct')}`}
key={item.get('id')}
2020-04-04 00:18:26 +01:00
className={['mention', _s.mr5, _s.pb5].join(' ')}
2020-03-26 03:11:32 +00:00
>
@{item.get('username')}
2020-02-24 21:56:07 +00:00
</Button>
2020-03-26 03:11:32 +00:00
)).reduce((aggregate, item) => [...aggregate, item, ' '], [])
if (hidden) {
2020-03-26 03:11:32 +00:00
mentionsPlaceholder = (
<div className={[_s.statusContent, _s.default, _s.alignItemsStart, _s.flexRow, _s.flexWrap].join(' ')}>
{mentionLinks}
</div>
)
}
2020-05-02 07:25:55 +01:00
const toggleText = intl.formatMessage(hidden ? messages.show : messages.hide)
2020-03-26 03:11:32 +00:00
const spoilerContainerClasses = cx({
default: 1,
py10: 1,
borderBottom1PX: !hidden,
borderColorSecondary: !hidden,
mb10: !hidden,
})
const statusContentClasses = cx({
statusContent: 1,
displayNone: hidden,
})
2020-04-08 02:06:59 +01:00
const containerClasses = cx({
statusContent: 1,
px15: !isComment,
2020-04-24 04:17:27 +01:00
outlineNone: 1,
2020-04-08 02:06:59 +01:00
})
return (
2020-03-26 03:11:32 +00:00
<div
className={[].join(' ')}
ref={this.setRef}
tabIndex='0'
2020-04-08 02:06:59 +01:00
className={containerClasses}
2020-03-26 03:11:32 +00:00
style={directionStyle}
onMouseDown={this.handleMouseDown}
onMouseUp={this.handleMouseUp}
>
<div className={spoilerContainerClasses}>
<div className={[_s.default, _s.flexRow, _s.mr5].join(' ')}>
2020-04-29 23:32:49 +01:00
<Icon id='warning' size='14px' className={[_s.fillPrimary, _s.mt2, _s.mr5].join(' ')}/>
2020-03-26 03:11:32 +00:00
<div
className={_s.statusContent}
dangerouslySetInnerHTML={spoilerContent}
lang={status.get('language')}
/>
</div>
<div className={[_s.default, _s.mt10, _s.alignItemsStart].join(' ')}>
<Button
2020-04-23 07:13:29 +01:00
isNarrow
2020-03-26 03:11:32 +00:00
radiusSmall
backgroundColor='tertiary'
color='primary'
tabIndex='0'
onClick={this.handleSpoilerClick}
>
<Text size='small' color='inherit'>
{toggleText}
</Text>
</Button>
</div>
</div>
{mentionsPlaceholder}
2020-03-26 03:11:32 +00:00
<div
tabIndex={!hidden ? 0 : null}
className={statusContentClasses}
style={directionStyle}
dangerouslySetInnerHTML={content}
lang={status.get('language')}
/>
</div>
2020-03-26 03:11:32 +00:00
)
} else if (this.props.onClick) {
2020-02-14 00:40:04 +00:00
const hasMarginBottom = !!status.get('card') || !!status.get('poll') || status.get('media_attachments').size > 0
const containerClasses = cx({
2020-03-11 23:56:18 +00:00
px15: !isComment,
mb15: hasMarginBottom,
2020-02-14 00:40:04 +00:00
})
2020-03-24 04:39:12 +00:00
const statusContentClasses = cx({
statusContent: 1,
2020-05-04 19:44:37 +01:00
height215PX: collapsed,
2020-03-24 04:39:12 +00:00
overflowHidden: collapsed,
})
2020-02-08 06:12:01 +00:00
return (
2020-02-14 00:40:04 +00:00
<div className={containerClasses}>
2020-02-08 06:12:01 +00:00
<div
ref={this.setRef}
tabIndex='0'
2020-03-24 04:39:12 +00:00
className={statusContentClasses}
2020-02-08 06:12:01 +00:00
style={directionStyle}
dangerouslySetInnerHTML={content}
lang={status.get('language')}
onMouseDown={this.handleMouseDown}
onMouseUp={this.handleMouseUp}
/>
{
this.state.collapsed &&
2020-03-27 15:29:52 +00:00
<Button
2020-04-23 07:13:29 +01:00
isText
2020-03-27 15:29:52 +00:00
underlineOnHover
color='primary'
backgroundColor='none'
className={[_s.py2].join(' ')}
2020-03-24 04:39:12 +00:00
onClick={this.handleReadMore}
2020-02-08 06:12:01 +00:00
>
2020-03-27 15:29:52 +00:00
<Text size='medium' color='inherit' weight='bold'>
{intl.formatMessage(messages.readMore)}
</Text>
</Button>
2020-02-08 06:12:01 +00:00
}
2020-02-14 00:40:04 +00:00
</div>
2020-02-08 06:12:01 +00:00
)
}
2020-04-08 02:06:59 +01:00
const containerClasses = cx({
statusContent: 1,
px15: !isComment,
mb15: !isComment,
mt5: isComment,
})
return (
<div
tabIndex='0'
ref={this.setRef}
2020-04-08 02:06:59 +01:00
className={containerClasses}
style={directionStyle}
dangerouslySetInnerHTML={content}
lang={status.get('language')}
/>
2020-03-26 03:11:32 +00:00
)
}
}