Merge branch 'media-ratio' into 'develop'

Rich media improvements, fixes #633

Closes #633

See merge request soapbox-pub/soapbox-fe!497
link-previews
Alex Gleason 3 years ago
commit ed38c7a421

@ -45,6 +45,7 @@ const addAutoPlay = html => {
} }
iframe.src += 'autoplay=1&auto_play=1'; iframe.src += 'autoplay=1&auto_play=1';
iframe.allow = 'autoplay';
// DOM parser creates html/body elements around original HTML fragment, // 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 // so we need to get innerHTML out of the body and not the entire document
@ -71,7 +72,7 @@ export default class Card extends React.PureComponent {
}; };
state = { state = {
width: this.props.defaultWidth || 280, width: this.props.defaultWidth || 467,
embedded: false, embedded: false,
}; };
@ -121,12 +122,10 @@ export default class Card extends React.PureComponent {
renderVideo() { renderVideo() {
const { card } = this.props; const { card } = this.props;
const cardWidth = card.get('width', card.getIn(['pleroma', 'opengraph', 'width']));
const cardHeight = card.get('height', card.getIn(['pleroma', 'opengraph', 'height']));
const html = card.get('html', card.getIn(['pleroma', 'opengraph', 'html'])); const html = card.get('html', card.getIn(['pleroma', 'opengraph', 'html']));
const content = { __html: addAutoPlay(html) }; const content = { __html: addAutoPlay(html) };
const { width } = this.state; const { width } = this.state;
const ratio = cardWidth / cardHeight; const ratio = this.getRatio(card);
const height = width / ratio; const height = width / ratio;
return ( return (
@ -139,6 +138,21 @@ export default class Card extends React.PureComponent {
); );
} }
getRatio = card => {
const width = card.get('width', card.getIn(['pleroma', 'opengraph', 'width']));
const height = card.get('height', card.getIn(['pleroma', 'opengraph', 'height']));
const ratio = width / height;
// Invalid dimensions, fall back to 16:9
if (typeof width !== 'number' || typeof height !== 'number') {
return 16 / 9;
}
// Constrain to a sane limit
// https://en.wikipedia.org/wiki/Aspect_ratio_(image)
return Math.min(Math.max(9 / 16, ratio), 4);
}
render() { render() {
const { card, maxDescription, compact } = this.props; const { card, maxDescription, compact } = this.props;
const { width, embedded } = this.state; const { width, embedded } = this.state;
@ -154,7 +168,7 @@ export default class Card extends React.PureComponent {
const horizontal = (!compact && cardWidth > cardHeight && (cardWidth + 100 >= width)) || interactive || embedded; const horizontal = (!compact && cardWidth > cardHeight && (cardWidth + 100 >= width)) || interactive || embedded;
const className = classnames('status-card', { horizontal, compact, interactive }); const className = classnames('status-card', { horizontal, compact, interactive });
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>; 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>;
const ratio = cardWidth / cardHeight; const ratio = this.getRatio(card);
const height = (compact && !embedded) ? (width / (16 / 9)) : (width / ratio); const height = (compact && !embedded) ? (width / (16 / 9)) : (width / ratio);
const description = ( const description = (

@ -545,8 +545,8 @@ a.status-card {
.status-card-video, .status-card-video,
.status-card-audio { .status-card-audio {
iframe { iframe {
width: 100%; width: 100% !important;
height: 100%; height: 100% !important;
} }
} }

Loading…
Cancel
Save