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.
133 lines
3.3 KiB
133 lines
3.3 KiB
7 years ago
|
// Package imports //
|
||
8 years ago
|
import React from 'react';
|
||
8 years ago
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
||
8 years ago
|
import PropTypes from 'prop-types';
|
||
8 years ago
|
|
||
7 years ago
|
// Mastodon imports //
|
||
|
import { isIOS } from '../../../../mastodon/is_mobile';
|
||
8 years ago
|
|
||
7 years ago
|
export default class StatusGalleryItem extends React.PureComponent {
|
||
8 years ago
|
|
||
8 years ago
|
static propTypes = {
|
||
|
attachment: ImmutablePropTypes.map.isRequired,
|
||
|
index: PropTypes.number.isRequired,
|
||
|
size: PropTypes.number.isRequired,
|
||
7 years ago
|
letterbox: PropTypes.bool,
|
||
8 years ago
|
onClick: PropTypes.func.isRequired,
|
||
8 years ago
|
autoPlayGif: PropTypes.bool.isRequired,
|
||
8 years ago
|
};
|
||
|
|
||
|
handleClick = (e) => {
|
||
8 years ago
|
const { index, onClick } = this.props;
|
||
|
|
||
|
if (e.button === 0) {
|
||
|
e.preventDefault();
|
||
|
onClick(index);
|
||
|
}
|
||
|
|
||
|
e.stopPropagation();
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
|
render () {
|
||
7 years ago
|
const { attachment, index, size, letterbox } = this.props;
|
||
8 years ago
|
|
||
|
let width = 50;
|
||
|
let height = 100;
|
||
|
let top = 'auto';
|
||
|
let left = 'auto';
|
||
|
let bottom = 'auto';
|
||
|
let right = 'auto';
|
||
|
|
||
|
if (size === 1) {
|
||
|
width = 100;
|
||
|
}
|
||
|
|
||
|
if (size === 4 || (size === 3 && index > 0)) {
|
||
|
height = 50;
|
||
|
}
|
||
|
|
||
|
if (size === 2) {
|
||
|
if (index === 0) {
|
||
|
right = '2px';
|
||
|
} else {
|
||
|
left = '2px';
|
||
|
}
|
||
|
} else if (size === 3) {
|
||
|
if (index === 0) {
|
||
|
right = '2px';
|
||
|
} else if (index > 0) {
|
||
|
left = '2px';
|
||
|
}
|
||
|
|
||
|
if (index === 1) {
|
||
|
bottom = '2px';
|
||
|
} else if (index > 1) {
|
||
|
top = '2px';
|
||
|
}
|
||
|
} else if (size === 4) {
|
||
|
if (index === 0 || index === 2) {
|
||
|
right = '2px';
|
||
|
}
|
||
|
|
||
|
if (index === 1 || index === 3) {
|
||
|
left = '2px';
|
||
|
}
|
||
|
|
||
|
if (index < 2) {
|
||
|
bottom = '2px';
|
||
|
} else {
|
||
|
top = '2px';
|
||
|
}
|
||
|
}
|
||
|
|
||
|
let thumbnail = '';
|
||
|
|
||
|
if (attachment.get('type') === 'image') {
|
||
7 years ago
|
const previewUrl = attachment.get('preview_url');
|
||
|
const previewWidth = attachment.getIn(['meta', 'small', 'width']);
|
||
|
|
||
|
const originalUrl = attachment.get('url');
|
||
|
const originalWidth = attachment.getIn(['meta', 'original', 'width']);
|
||
|
|
||
|
const srcSet = `${originalUrl} ${originalWidth}w, ${previewUrl} ${previewWidth}w`;
|
||
|
const sizes = `(min-width: 1025px) ${320 * (width / 100)}px, ${width}vw`;
|
||
|
|
||
8 years ago
|
thumbnail = (
|
||
7 years ago
|
<a
|
||
8 years ago
|
className='media-gallery__item-thumbnail'
|
||
7 years ago
|
href={attachment.get('remote_url') || originalUrl}
|
||
8 years ago
|
onClick={this.handleClick}
|
||
|
target='_blank'
|
||
7 years ago
|
>
|
||
7 years ago
|
<img className={letterbox ? 'letterbox' : ''} src={previewUrl} srcSet={srcSet} sizes={sizes} alt='' />
|
||
7 years ago
|
</a>
|
||
8 years ago
|
);
|
||
|
} else if (attachment.get('type') === 'gifv') {
|
||
8 years ago
|
const autoPlay = !isIOS() && this.props.autoPlayGif;
|
||
|
|
||
|
thumbnail = (
|
||
8 years ago
|
<div className={`media-gallery__gifv ${autoPlay ? 'autoplay' : ''}`}>
|
||
8 years ago
|
<video
|
||
7 years ago
|
className={`media-gallery__item-gifv-thumbnail${letterbox ? ' letterbox' : ''}`}
|
||
8 years ago
|
role='application'
|
||
8 years ago
|
src={attachment.get('url')}
|
||
|
onClick={this.handleClick}
|
||
|
autoPlay={autoPlay}
|
||
8 years ago
|
loop
|
||
|
muted
|
||
8 years ago
|
/>
|
||
|
|
||
|
<span className='media-gallery__gifv__label'>GIF</span>
|
||
|
</div>
|
||
|
);
|
||
8 years ago
|
}
|
||
|
|
||
|
return (
|
||
8 years ago
|
<div className='media-gallery__item' key={attachment.get('id')} style={{ left: left, top: top, right: right, bottom: bottom, width: `${width}%`, height: `${height}%` }}>
|
||
8 years ago
|
{thumbnail}
|
||
|
</div>
|
||
|
);
|
||
|
}
|
||
|
|
||
8 years ago
|
}
|