2017-05-03 09:04:16 +09:00
|
|
|
import React from 'react';
|
2017-09-14 10:39:10 +09:00
|
|
|
import PropTypes from 'prop-types';
|
2017-11-25 23:41:45 +09:00
|
|
|
import Immutable from 'immutable';
|
2017-01-20 09:00:14 +09:00
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
2020-06-07 00:41:56 +09:00
|
|
|
import { FormattedMessage } from 'react-intl';
|
2019-08-06 19:09:28 +09:00
|
|
|
import punycode from 'punycode';
|
2017-09-01 23:20:16 +09:00
|
|
|
import classnames from 'classnames';
|
2019-02-01 08:14:05 +09:00
|
|
|
import Icon from 'mastodon/components/icon';
|
2020-06-07 00:41:56 +09:00
|
|
|
import { useBlurhash } from 'mastodon/initial_state';
|
2020-07-09 20:01:30 +09:00
|
|
|
import Blurhash from 'mastodon/components/blurhash';
|
2020-06-24 17:25:32 +09:00
|
|
|
import { debounce } from 'lodash';
|
2019-08-06 19:09:28 +09:00
|
|
|
|
|
|
|
const IDNA_PREFIX = 'xn--';
|
|
|
|
|
|
|
|
const decodeIDNA = domain => {
|
|
|
|
return domain
|
|
|
|
.split('.')
|
|
|
|
.map(part => part.indexOf(IDNA_PREFIX) === 0 ? punycode.decode(part.slice(IDNA_PREFIX.length)) : part)
|
|
|
|
.join('.');
|
|
|
|
};
|
2017-01-20 09:00:14 +09:00
|
|
|
|
|
|
|
const getHostname = url => {
|
|
|
|
const parser = document.createElement('a');
|
|
|
|
parser.href = url;
|
|
|
|
return parser.hostname;
|
|
|
|
};
|
|
|
|
|
2018-02-15 15:04:28 +09:00
|
|
|
const trim = (text, len) => {
|
|
|
|
const cut = text.indexOf(' ', len);
|
|
|
|
|
|
|
|
if (cut === -1) {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2022-04-05 01:19:45 +09:00
|
|
|
return text.slice(0, cut) + (text.length > len ? '…' : '');
|
2018-02-15 15:04:28 +09:00
|
|
|
};
|
|
|
|
|
2018-02-16 07:05:12 +09:00
|
|
|
const domParser = new DOMParser();
|
|
|
|
|
|
|
|
const addAutoPlay = html => {
|
|
|
|
const document = domParser.parseFromString(html, 'text/html').documentElement;
|
|
|
|
const iframe = document.querySelector('iframe');
|
|
|
|
|
|
|
|
if (iframe) {
|
|
|
|
if (iframe.src.indexOf('?') !== -1) {
|
|
|
|
iframe.src += '&';
|
|
|
|
} else {
|
|
|
|
iframe.src += '?';
|
|
|
|
}
|
|
|
|
|
|
|
|
iframe.src += 'autoplay=1&auto_play=1';
|
|
|
|
|
|
|
|
// DOM parser creates html/body elements around original HTML fragment,
|
|
|
|
// so we need to get innerHTML out of the body and not the entire document
|
|
|
|
return document.querySelector('body').innerHTML;
|
|
|
|
}
|
|
|
|
|
|
|
|
return html;
|
|
|
|
};
|
|
|
|
|
2017-06-24 02:36:54 +09:00
|
|
|
export default class Card extends React.PureComponent {
|
2017-01-20 09:00:14 +09:00
|
|
|
|
2017-05-12 21:44:10 +09:00
|
|
|
static propTypes = {
|
2017-05-21 00:31:47 +09:00
|
|
|
card: ImmutablePropTypes.map,
|
2017-09-14 10:39:10 +09:00
|
|
|
maxDescription: PropTypes.number,
|
2017-11-25 23:41:45 +09:00
|
|
|
onOpenMedia: PropTypes.func.isRequired,
|
2018-10-29 12:42:07 +09:00
|
|
|
compact: PropTypes.bool,
|
2019-02-11 21:19:59 +09:00
|
|
|
defaultWidth: PropTypes.number,
|
|
|
|
cacheWidth: PropTypes.func,
|
2020-06-07 00:41:56 +09:00
|
|
|
sensitive: PropTypes.bool,
|
2017-09-14 10:39:10 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
maxDescription: 50,
|
2018-10-28 14:35:03 +09:00
|
|
|
compact: false,
|
2017-05-12 21:44:10 +09:00
|
|
|
};
|
|
|
|
|
2017-10-08 09:34:49 +09:00
|
|
|
state = {
|
2019-02-11 21:19:59 +09:00
|
|
|
width: this.props.defaultWidth || 280,
|
2020-06-07 00:41:56 +09:00
|
|
|
previewLoaded: false,
|
2018-02-15 15:04:28 +09:00
|
|
|
embedded: false,
|
2020-06-07 00:41:56 +09:00
|
|
|
revealed: !this.props.sensitive,
|
2017-10-08 09:34:49 +09:00
|
|
|
};
|
|
|
|
|
2018-02-15 15:04:28 +09:00
|
|
|
componentWillReceiveProps (nextProps) {
|
2018-11-13 06:07:31 +09:00
|
|
|
if (!Immutable.is(this.props.card, nextProps.card)) {
|
2020-06-07 00:41:56 +09:00
|
|
|
this.setState({ embedded: false, previewLoaded: false });
|
|
|
|
}
|
|
|
|
if (this.props.sensitive !== nextProps.sensitive) {
|
|
|
|
this.setState({ revealed: !nextProps.sensitive });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount () {
|
2020-06-24 17:25:32 +09:00
|
|
|
window.addEventListener('resize', this.handleResize, { passive: true });
|
2020-06-07 00:41:56 +09:00
|
|
|
}
|
|
|
|
|
2020-06-24 17:25:32 +09:00
|
|
|
componentWillUnmount () {
|
|
|
|
window.removeEventListener('resize', this.handleResize);
|
|
|
|
}
|
|
|
|
|
|
|
|
_setDimensions () {
|
|
|
|
const width = this.node.offsetWidth;
|
|
|
|
|
|
|
|
if (this.props.cacheWidth) {
|
|
|
|
this.props.cacheWidth(width);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({ width });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleResize = debounce(() => {
|
|
|
|
if (this.node) {
|
|
|
|
this._setDimensions();
|
|
|
|
}
|
|
|
|
}, 250, {
|
|
|
|
trailing: true,
|
|
|
|
});
|
|
|
|
|
2017-11-25 23:41:45 +09:00
|
|
|
handlePhotoClick = () => {
|
|
|
|
const { card, onOpenMedia } = this.props;
|
|
|
|
|
|
|
|
onOpenMedia(
|
|
|
|
Immutable.fromJS([
|
|
|
|
{
|
|
|
|
type: 'image',
|
2017-12-15 07:31:14 +09:00
|
|
|
url: card.get('embed_url'),
|
2017-11-25 23:41:45 +09:00
|
|
|
description: card.get('title'),
|
|
|
|
meta: {
|
|
|
|
original: {
|
|
|
|
width: card.get('width'),
|
|
|
|
height: card.get('height'),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]),
|
2020-03-09 00:02:36 +09:00
|
|
|
0,
|
2017-11-25 23:41:45 +09:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2018-02-15 15:04:28 +09:00
|
|
|
handleEmbedClick = () => {
|
2017-04-27 21:42:22 +09:00
|
|
|
const { card } = this.props;
|
|
|
|
|
2018-02-15 15:04:28 +09:00
|
|
|
if (card.get('type') === 'photo') {
|
|
|
|
this.handlePhotoClick();
|
|
|
|
} else {
|
|
|
|
this.setState({ embedded: true });
|
|
|
|
}
|
2023-01-30 09:45:35 +09:00
|
|
|
};
|
2017-04-27 21:42:22 +09:00
|
|
|
|
2017-10-08 09:34:49 +09:00
|
|
|
setRef = c => {
|
2020-06-24 17:25:32 +09:00
|
|
|
this.node = c;
|
|
|
|
|
|
|
|
if (this.node) {
|
|
|
|
this._setDimensions();
|
2017-10-08 09:34:49 +09:00
|
|
|
}
|
2023-01-30 09:45:35 +09:00
|
|
|
};
|
2017-10-08 09:34:49 +09:00
|
|
|
|
2020-06-07 00:41:56 +09:00
|
|
|
handleImageLoad = () => {
|
|
|
|
this.setState({ previewLoaded: true });
|
2023-01-30 09:45:35 +09:00
|
|
|
};
|
2020-06-07 00:41:56 +09:00
|
|
|
|
2020-06-26 05:42:01 +09:00
|
|
|
handleReveal = e => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2020-06-07 00:41:56 +09:00
|
|
|
this.setState({ revealed: true });
|
2023-01-30 09:45:35 +09:00
|
|
|
};
|
2020-06-07 00:41:56 +09:00
|
|
|
|
2017-04-27 21:42:22 +09:00
|
|
|
renderVideo () {
|
2017-10-08 09:34:49 +09:00
|
|
|
const { card } = this.props;
|
2018-02-16 07:05:12 +09:00
|
|
|
const content = { __html: addAutoPlay(card.get('html')) };
|
2017-10-08 09:34:49 +09:00
|
|
|
const { width } = this.state;
|
|
|
|
const ratio = card.get('width') / card.get('height');
|
2018-10-29 12:42:07 +09:00
|
|
|
const height = width / ratio;
|
2017-04-27 21:42:22 +09:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
2017-10-08 09:34:49 +09:00
|
|
|
ref={this.setRef}
|
2018-02-15 15:04:28 +09:00
|
|
|
className='status-card__image status-card-video'
|
2017-04-27 21:42:22 +09:00
|
|
|
dangerouslySetInnerHTML={content}
|
2017-10-08 09:34:49 +09:00
|
|
|
style={{ height }}
|
2017-04-27 21:42:22 +09:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2018-10-28 14:35:03 +09:00
|
|
|
const { card, maxDescription, compact } = this.props;
|
2020-06-07 00:41:56 +09:00
|
|
|
const { width, embedded, revealed } = this.state;
|
2017-04-27 21:42:22 +09:00
|
|
|
|
|
|
|
if (card === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-02-15 15:04:28 +09:00
|
|
|
const provider = card.get('provider_name').length === 0 ? decodeIDNA(getHostname(card.get('url'))) : card.get('provider_name');
|
2018-10-28 14:35:03 +09:00
|
|
|
const horizontal = (!compact && card.get('width') > card.get('height') && (card.get('width') + 100 >= width)) || card.get('type') !== 'link' || embedded;
|
2018-02-15 15:04:28 +09:00
|
|
|
const interactive = card.get('type') !== 'link';
|
2018-10-28 14:35:03 +09:00
|
|
|
const className = classnames('status-card', { horizontal, compact, interactive });
|
2019-10-25 05:44:42 +09:00
|
|
|
const title = interactive ? <a className='status-card__title' href={card.get('url')} title={card.get('title')} rel='noopener noreferrer' target='_blank'><strong>{card.get('title')}</strong></a> : <strong className='status-card__title' title={card.get('title')}>{card.get('title')}</strong>;
|
2023-02-25 04:04:38 +09:00
|
|
|
const language = card.get('language') || '';
|
2018-10-29 12:42:07 +09:00
|
|
|
const ratio = card.get('width') / card.get('height');
|
|
|
|
const height = (compact && !embedded) ? (width / (16 / 9)) : (width / ratio);
|
2018-02-15 15:04:28 +09:00
|
|
|
|
|
|
|
const description = (
|
2023-02-25 04:04:38 +09:00
|
|
|
<div className='status-card__content' lang={language}>
|
2018-02-15 15:04:28 +09:00
|
|
|
{title}
|
2018-10-28 14:35:03 +09:00
|
|
|
{!(horizontal || compact) && <p className='status-card__description'>{trim(card.get('description') || '', maxDescription)}</p>}
|
2018-02-15 15:04:28 +09:00
|
|
|
<span className='status-card__host'>{provider}</span>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
let embed = '';
|
2020-07-09 20:01:30 +09:00
|
|
|
let canvas = (
|
|
|
|
<Blurhash
|
|
|
|
className={classnames('status-card__image-preview', {
|
|
|
|
'status-card__image-preview--hidden': revealed && this.state.previewLoaded,
|
|
|
|
})}
|
|
|
|
hash={card.get('blurhash')}
|
|
|
|
dummy={!useBlurhash}
|
|
|
|
/>
|
|
|
|
);
|
2020-06-07 00:41:56 +09:00
|
|
|
let thumbnail = <img src={card.get('image')} alt='' style={{ width: horizontal ? width : null, height: horizontal ? height : null, visibility: revealed ? null : 'hidden' }} onLoad={this.handleImageLoad} className='status-card__image-image' />;
|
|
|
|
let spoilerButton = (
|
|
|
|
<button type='button' onClick={this.handleReveal} className='spoiler-button__overlay'>
|
|
|
|
<span className='spoiler-button__overlay__label'><FormattedMessage id='status.sensitive_warning' defaultMessage='Sensitive content' /></span>
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
spoilerButton = (
|
2020-06-26 21:31:13 +09:00
|
|
|
<div className={classnames('spoiler-button', { 'spoiler-button--minified': revealed })}>
|
2020-06-07 00:41:56 +09:00
|
|
|
{spoilerButton}
|
|
|
|
</div>
|
|
|
|
);
|
2018-02-15 15:04:28 +09:00
|
|
|
|
|
|
|
if (interactive) {
|
|
|
|
if (embedded) {
|
|
|
|
embed = this.renderVideo();
|
|
|
|
} else {
|
|
|
|
let iconVariant = 'play';
|
|
|
|
|
|
|
|
if (card.get('type') === 'photo') {
|
|
|
|
iconVariant = 'search-plus';
|
|
|
|
}
|
|
|
|
|
|
|
|
embed = (
|
|
|
|
<div className='status-card__image'>
|
2020-06-07 00:41:56 +09:00
|
|
|
{canvas}
|
2018-02-15 15:04:28 +09:00
|
|
|
{thumbnail}
|
|
|
|
|
2020-06-07 00:41:56 +09:00
|
|
|
{revealed && (
|
|
|
|
<div className='status-card__actions'>
|
|
|
|
<div>
|
2022-11-05 21:43:37 +09:00
|
|
|
<button type='button' onClick={this.handleEmbedClick}><Icon id={iconVariant} /></button>
|
2020-06-07 00:41:56 +09:00
|
|
|
{horizontal && <a href={card.get('url')} target='_blank' rel='noopener noreferrer'><Icon id='external-link' /></a>}
|
|
|
|
</div>
|
2018-02-15 15:04:28 +09:00
|
|
|
</div>
|
2020-06-07 00:41:56 +09:00
|
|
|
)}
|
|
|
|
{!revealed && spoilerButton}
|
2018-02-15 15:04:28 +09:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2020-06-26 05:42:01 +09:00
|
|
|
<div className={className} ref={this.setRef} onClick={revealed ? null : this.handleReveal} role={revealed ? 'button' : null}>
|
2018-02-15 15:04:28 +09:00
|
|
|
{embed}
|
2018-10-28 14:35:03 +09:00
|
|
|
{!compact && description}
|
2018-02-15 15:04:28 +09:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
} else if (card.get('image')) {
|
|
|
|
embed = (
|
|
|
|
<div className='status-card__image'>
|
2020-06-07 00:41:56 +09:00
|
|
|
{canvas}
|
2018-02-15 15:04:28 +09:00
|
|
|
{thumbnail}
|
|
|
|
</div>
|
|
|
|
);
|
2019-01-04 20:44:46 +09:00
|
|
|
} else {
|
|
|
|
embed = (
|
|
|
|
<div className='status-card__image'>
|
2019-02-01 08:14:05 +09:00
|
|
|
<Icon id='file-text' />
|
2019-01-04 20:44:46 +09:00
|
|
|
</div>
|
|
|
|
);
|
2017-04-27 21:42:22 +09:00
|
|
|
}
|
2018-02-15 15:04:28 +09:00
|
|
|
|
|
|
|
return (
|
2019-10-25 05:44:42 +09:00
|
|
|
<a href={card.get('url')} className={className} target='_blank' rel='noopener noreferrer' ref={this.setRef}>
|
2018-02-15 15:04:28 +09:00
|
|
|
{embed}
|
|
|
|
{description}
|
|
|
|
</a>
|
|
|
|
);
|
2017-04-27 21:42:22 +09:00
|
|
|
}
|
2017-05-21 00:31:47 +09:00
|
|
|
|
2017-04-22 03:05:35 +09:00
|
|
|
}
|