2020-07-09 06:01:30 -05:00
|
|
|
import Blurhash from 'flavours/glitch/components/blurhash';
|
2019-10-24 15:44:42 -05:00
|
|
|
import classNames from 'classnames';
|
2019-09-09 09:41:41 -05:00
|
|
|
import Icon from 'flavours/glitch/components/icon';
|
2020-07-09 06:01:30 -05:00
|
|
|
import { autoPlayGif, displayMedia, useBlurhash } from 'flavours/glitch/util/initial_state';
|
2019-05-02 01:34:32 -05:00
|
|
|
import { isIOS } from 'flavours/glitch/util/is_mobile';
|
2019-10-24 15:44:42 -05:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
2017-05-19 18:28:25 -05:00
|
|
|
|
2017-06-23 12:36:54 -05:00
|
|
|
export default class MediaItem extends ImmutablePureComponent {
|
2017-05-19 18:28:25 -05:00
|
|
|
|
|
|
|
static propTypes = {
|
2019-05-02 01:34:32 -05:00
|
|
|
attachment: ImmutablePropTypes.map.isRequired,
|
|
|
|
displayWidth: PropTypes.number.isRequired,
|
|
|
|
onOpenMedia: PropTypes.func.isRequired,
|
2017-05-19 18:28:25 -05:00
|
|
|
};
|
|
|
|
|
2018-10-02 08:48:55 -05:00
|
|
|
state = {
|
2019-05-02 01:34:32 -05:00
|
|
|
visible: displayMedia !== 'hide_all' && !this.props.attachment.getIn(['status', 'sensitive']) || displayMedia === 'show_all',
|
|
|
|
loaded: false,
|
2018-10-02 08:48:55 -05:00
|
|
|
};
|
|
|
|
|
2019-05-02 01:34:32 -05:00
|
|
|
handleImageLoad = () => {
|
|
|
|
this.setState({ loaded: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
handleMouseEnter = e => {
|
|
|
|
if (this.hoverToPlay()) {
|
|
|
|
e.target.play();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleMouseLeave = e => {
|
|
|
|
if (this.hoverToPlay()) {
|
|
|
|
e.target.pause();
|
|
|
|
e.target.currentTime = 0;
|
2017-05-19 18:28:25 -05:00
|
|
|
}
|
2019-05-02 01:34:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
hoverToPlay () {
|
|
|
|
return !autoPlayGif && ['gifv', 'video'].indexOf(this.props.attachment.get('type')) !== -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleClick = e => {
|
|
|
|
if (e.button === 0 && !(e.ctrlKey || e.metaKey)) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
if (this.state.visible) {
|
|
|
|
this.props.onOpenMedia(this.props.attachment);
|
|
|
|
} else {
|
|
|
|
this.setState({ visible: true });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const { attachment, displayWidth } = this.props;
|
|
|
|
const { visible, loaded } = this.state;
|
|
|
|
|
|
|
|
const width = `${Math.floor((displayWidth - 4) / 3) - 4}px`;
|
|
|
|
const height = width;
|
|
|
|
const status = attachment.get('status');
|
2020-07-10 15:09:28 -05:00
|
|
|
const title = status.get('spoiler_text') || attachment.get('description');
|
2019-05-02 01:34:32 -05:00
|
|
|
|
2020-07-10 15:09:28 -05:00
|
|
|
let thumbnail, label, icon, content;
|
2019-05-02 01:34:32 -05:00
|
|
|
|
2020-07-10 15:09:28 -05:00
|
|
|
if (!visible) {
|
|
|
|
icon = (
|
2019-08-20 12:46:41 -05:00
|
|
|
<span className='account-gallery__item__icons'>
|
2020-07-10 15:09:28 -05:00
|
|
|
<Icon id='eye-slash' />
|
2019-08-20 12:46:41 -05:00
|
|
|
</span>
|
|
|
|
);
|
2020-07-10 15:09:28 -05:00
|
|
|
} else {
|
|
|
|
if (['audio', 'video'].includes(attachment.get('type'))) {
|
|
|
|
content = (
|
|
|
|
<img
|
|
|
|
src={attachment.get('preview_url') || attachment.getIn(['account', 'avatar_static'])}
|
|
|
|
alt={attachment.get('description')}
|
|
|
|
onLoad={this.handleImageLoad}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
if (attachment.get('type') === 'audio') {
|
|
|
|
label = <Icon id='music' />;
|
|
|
|
} else {
|
|
|
|
label = <Icon id='play' />;
|
|
|
|
}
|
|
|
|
} else if (attachment.get('type') === 'image') {
|
|
|
|
const focusX = attachment.getIn(['meta', 'focus', 'x']) || 0;
|
|
|
|
const focusY = attachment.getIn(['meta', 'focus', 'y']) || 0;
|
|
|
|
const x = ((focusX / 2) + .5) * 100;
|
|
|
|
const y = ((focusY / -2) + .5) * 100;
|
|
|
|
|
|
|
|
content = (
|
|
|
|
<img
|
|
|
|
src={attachment.get('preview_url')}
|
|
|
|
alt={attachment.get('description')}
|
|
|
|
style={{ objectPosition: `${x}% ${y}%` }}
|
|
|
|
onLoad={this.handleImageLoad}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} else if (attachment.get('type') === 'gifv') {
|
|
|
|
content = (
|
2019-05-02 01:34:32 -05:00
|
|
|
<video
|
|
|
|
className='media-gallery__item-gifv-thumbnail'
|
|
|
|
aria-label={attachment.get('description')}
|
|
|
|
role='application'
|
|
|
|
src={attachment.get('url')}
|
|
|
|
onMouseEnter={this.handleMouseEnter}
|
|
|
|
onMouseLeave={this.handleMouseLeave}
|
2020-07-10 15:09:28 -05:00
|
|
|
autoPlay={!isIOS() && autoPlayGif}
|
2019-05-02 01:34:32 -05:00
|
|
|
loop
|
|
|
|
muted
|
|
|
|
/>
|
2020-07-10 15:09:28 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
label = 'GIF';
|
|
|
|
}
|
|
|
|
|
|
|
|
thumbnail = (
|
|
|
|
<div className='media-gallery__gifv'>
|
|
|
|
{content}
|
2019-10-02 20:34:58 -05:00
|
|
|
|
2020-11-01 11:31:39 -06:00
|
|
|
{label && <span className='media-gallery__gifv__label'>{label}</span>}
|
2019-05-02 01:34:32 -05:00
|
|
|
</div>
|
2018-10-02 08:48:55 -05:00
|
|
|
);
|
2017-05-19 18:28:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2019-05-02 01:34:32 -05:00
|
|
|
<div className='account-gallery__item' style={{ width, height }}>
|
2019-10-24 15:44:42 -05:00
|
|
|
<a className='media-gallery__item-thumbnail' href={status.get('url')} onClick={this.handleClick} title={title} target='_blank' rel='noopener noreferrer'>
|
2020-07-09 06:01:30 -05:00
|
|
|
<Blurhash
|
|
|
|
hash={attachment.get('blurhash')}
|
2020-07-10 15:09:28 -05:00
|
|
|
className={classNames('media-gallery__preview', { 'media-gallery__preview--hidden': visible && loaded })}
|
2020-07-09 06:01:30 -05:00
|
|
|
dummy={!useBlurhash}
|
|
|
|
/>
|
2020-07-10 15:09:28 -05:00
|
|
|
|
2019-05-04 13:06:17 -05:00
|
|
|
{visible ? thumbnail : icon}
|
2019-05-02 01:34:32 -05:00
|
|
|
</a>
|
2017-05-19 18:28:25 -05:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2017-05-20 10:31:47 -05:00
|
|
|
|
2017-05-19 18:28:25 -05:00
|
|
|
}
|