2017-05-03 02:04:16 +02:00
|
|
|
import React from 'react';
|
2017-09-14 03:39:10 +02:00
|
|
|
import PropTypes from 'prop-types';
|
2018-04-08 23:06:33 +02:00
|
|
|
import Immutable from 'immutable';
|
2017-01-20 01:00:14 +01:00
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
2017-05-20 17:31:47 +02:00
|
|
|
import punycode from 'punycode';
|
2017-09-01 16:20:16 +02:00
|
|
|
import classnames from 'classnames';
|
2019-08-01 14:35:17 +02:00
|
|
|
import { decode as decodeIDNA } from 'flavours/glitch/util/idna';
|
2017-01-20 01:00:14 +01:00
|
|
|
|
|
|
|
const getHostname = url => {
|
|
|
|
const parser = document.createElement('a');
|
|
|
|
parser.href = url;
|
|
|
|
return parser.hostname;
|
|
|
|
};
|
|
|
|
|
2018-09-10 17:53:28 +02:00
|
|
|
const trim = (text, len) => {
|
|
|
|
const cut = text.indexOf(' ', len);
|
|
|
|
|
|
|
|
if (cut === -1) {
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
return text.substring(0, cut) + (text.length > len ? '…' : '');
|
|
|
|
};
|
|
|
|
|
2018-09-10 17:58:21 +02: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-23 19:36:54 +02:00
|
|
|
export default class Card extends React.PureComponent {
|
2017-01-20 01:00:14 +01:00
|
|
|
|
2017-05-12 14:44:10 +02:00
|
|
|
static propTypes = {
|
2017-05-20 17:31:47 +02:00
|
|
|
card: ImmutablePropTypes.map,
|
2017-09-14 03:39:10 +02:00
|
|
|
maxDescription: PropTypes.number,
|
2018-04-08 23:06:33 +02:00
|
|
|
onOpenMedia: PropTypes.func.isRequired,
|
2018-10-29 14:56:53 +01:00
|
|
|
compact: PropTypes.bool,
|
2019-02-09 20:54:11 +01:00
|
|
|
defaultWidth: PropTypes.number,
|
|
|
|
cacheWidth: PropTypes.func,
|
2017-09-14 03:39:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
maxDescription: 50,
|
2018-10-29 14:44:04 +01:00
|
|
|
compact: false,
|
2017-05-12 14:44:10 +02:00
|
|
|
};
|
|
|
|
|
2017-10-08 02:34:49 +02:00
|
|
|
state = {
|
2019-02-09 20:54:11 +01:00
|
|
|
width: this.props.defaultWidth || 280,
|
2018-09-10 17:53:28 +02:00
|
|
|
embedded: false,
|
2017-10-08 02:34:49 +02:00
|
|
|
};
|
|
|
|
|
2018-09-10 17:53:28 +02:00
|
|
|
componentWillReceiveProps (nextProps) {
|
2019-03-05 22:51:23 +01:00
|
|
|
if (!Immutable.is(this.props.card, nextProps.card)) {
|
2018-09-10 17:53:28 +02:00
|
|
|
this.setState({ embedded: false });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-08 23:06:33 +02:00
|
|
|
handlePhotoClick = () => {
|
|
|
|
const { card, onOpenMedia } = this.props;
|
|
|
|
|
|
|
|
onOpenMedia(
|
|
|
|
Immutable.fromJS([
|
|
|
|
{
|
|
|
|
type: 'image',
|
2018-09-10 17:52:40 +02:00
|
|
|
url: card.get('embed_url'),
|
2018-04-08 23:06:33 +02:00
|
|
|
description: card.get('title'),
|
|
|
|
meta: {
|
|
|
|
original: {
|
|
|
|
width: card.get('width'),
|
|
|
|
height: card.get('height'),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]),
|
|
|
|
0
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2018-09-10 17:53:28 +02:00
|
|
|
handleEmbedClick = () => {
|
2017-04-27 14:42:22 +02:00
|
|
|
const { card } = this.props;
|
|
|
|
|
2018-09-10 17:53:28 +02:00
|
|
|
if (card.get('type') === 'photo') {
|
|
|
|
this.handlePhotoClick();
|
|
|
|
} else {
|
|
|
|
this.setState({ embedded: true });
|
|
|
|
}
|
2017-04-27 14:42:22 +02:00
|
|
|
}
|
|
|
|
|
2017-10-08 02:34:49 +02:00
|
|
|
setRef = c => {
|
|
|
|
if (c) {
|
2019-02-09 20:54:11 +01:00
|
|
|
if (this.props.cacheWidth) this.props.cacheWidth(c.offsetWidth);
|
2017-10-08 02:34:49 +02:00
|
|
|
this.setState({ width: c.offsetWidth });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-27 14:42:22 +02:00
|
|
|
renderVideo () {
|
2017-10-08 02:34:49 +02:00
|
|
|
const { card } = this.props;
|
2018-09-10 17:58:21 +02:00
|
|
|
const content = { __html: addAutoPlay(card.get('html')) };
|
2017-10-08 02:34:49 +02:00
|
|
|
const { width } = this.state;
|
|
|
|
const ratio = card.get('width') / card.get('height');
|
2018-10-29 14:56:53 +01:00
|
|
|
const height = width / ratio;
|
2017-04-27 14:42:22 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
2017-10-08 02:34:49 +02:00
|
|
|
ref={this.setRef}
|
2018-09-10 17:53:28 +02:00
|
|
|
className='status-card__image status-card-video'
|
2017-04-27 14:42:22 +02:00
|
|
|
dangerouslySetInnerHTML={content}
|
2017-10-08 02:34:49 +02:00
|
|
|
style={{ height }}
|
2017-04-27 14:42:22 +02:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
2019-02-09 20:54:11 +01:00
|
|
|
const { card, maxDescription, compact, defaultWidth } = this.props;
|
2018-10-29 14:44:04 +01:00
|
|
|
const { width, embedded } = this.state;
|
2017-04-27 14:42:22 +02:00
|
|
|
|
|
|
|
if (card === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-09-10 17:53:28 +02:00
|
|
|
const provider = card.get('provider_name').length === 0 ? decodeIDNA(getHostname(card.get('url'))) : card.get('provider_name');
|
2018-10-29 14:44:04 +01:00
|
|
|
const horizontal = (!compact && card.get('width') > card.get('height') && (card.get('width') + 100 >= width)) || card.get('type') !== 'link' || embedded;
|
2018-09-10 17:53:28 +02:00
|
|
|
const interactive = card.get('type') !== 'link';
|
2018-10-29 14:44:04 +01:00
|
|
|
const className = classnames('status-card', { horizontal, compact, interactive });
|
2018-09-10 17:53:28 +02:00
|
|
|
const title = interactive ? <a className='status-card__title' href={card.get('url')} title={card.get('title')} rel='noopener' target='_blank'><strong>{card.get('title')}</strong></a> : <strong className='status-card__title' title={card.get('title')}>{card.get('title')}</strong>;
|
2018-10-29 14:56:53 +01:00
|
|
|
const ratio = card.get('width') / card.get('height');
|
|
|
|
const height = (compact && !embedded) ? (width / (16 / 9)) : (width / ratio);
|
2018-09-10 17:53:28 +02:00
|
|
|
|
|
|
|
const description = (
|
|
|
|
<div className='status-card__content'>
|
|
|
|
{title}
|
2018-10-29 14:44:04 +01:00
|
|
|
{!(horizontal || compact) && <p className='status-card__description'>{trim(card.get('description') || '', maxDescription)}</p>}
|
2018-09-10 17:53:28 +02:00
|
|
|
<span className='status-card__host'>{provider}</span>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
let embed = '';
|
|
|
|
let thumbnail = <div style={{ backgroundImage: `url(${card.get('image')})`, width: horizontal ? width : null, height: horizontal ? height : null }} className='status-card__image-image' />;
|
|
|
|
|
|
|
|
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'>
|
|
|
|
{thumbnail}
|
|
|
|
|
|
|
|
<div className='status-card__actions'>
|
|
|
|
<div>
|
|
|
|
<button onClick={this.handleEmbedClick}><i className={`fa fa-${iconVariant}`} /></button>
|
2018-10-29 14:44:04 +01:00
|
|
|
{horizontal && <a href={card.get('url')} target='_blank' rel='noopener'><i className='fa fa-external-link' /></a>}
|
2018-09-10 17:53:28 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={className} ref={this.setRef}>
|
|
|
|
{embed}
|
2018-10-29 14:44:04 +01:00
|
|
|
{!compact && description}
|
2018-09-10 17:53:28 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
} else if (card.get('image')) {
|
|
|
|
embed = (
|
|
|
|
<div className='status-card__image'>
|
|
|
|
{thumbnail}
|
|
|
|
</div>
|
|
|
|
);
|
2019-01-10 20:19:45 +01:00
|
|
|
} else {
|
|
|
|
embed = (
|
|
|
|
<div className='status-card__image'>
|
|
|
|
<i className='fa fa-file-text' />
|
|
|
|
</div>
|
|
|
|
);
|
2017-04-27 14:42:22 +02:00
|
|
|
}
|
2018-09-10 17:53:28 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<a href={card.get('url')} className={className} target='_blank' rel='noopener' ref={this.setRef}>
|
|
|
|
{embed}
|
|
|
|
{description}
|
|
|
|
</a>
|
|
|
|
);
|
2017-04-27 14:42:22 +02:00
|
|
|
}
|
2017-05-20 17:31:47 +02:00
|
|
|
|
2017-04-21 20:05:35 +02:00
|
|
|
}
|