2017-05-20 09:28:25 +10:00
|
|
|
import React from 'react';
|
2019-05-02 16:34:32 +10:00
|
|
|
import PropTypes from 'prop-types';
|
2017-05-20 09:28:25 +10:00
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
2019-05-09 02:01:33 +10:00
|
|
|
import Icon from 'mastodon/components/icon';
|
2019-05-02 16:34:32 +10:00
|
|
|
import { autoPlayGif, displayMedia } from 'mastodon/initial_state';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import { decode } from 'blurhash';
|
|
|
|
import { isIOS } from 'mastodon/is_mobile';
|
2017-05-20 09:28:25 +10:00
|
|
|
|
2017-06-24 03:36:54 +10:00
|
|
|
export default class MediaItem extends ImmutablePureComponent {
|
2017-05-20 09:28:25 +10:00
|
|
|
|
|
|
|
static propTypes = {
|
2019-05-02 16:34:32 +10:00
|
|
|
attachment: ImmutablePropTypes.map.isRequired,
|
|
|
|
displayWidth: PropTypes.number.isRequired,
|
|
|
|
onOpenMedia: PropTypes.func.isRequired,
|
2017-05-20 09:28:25 +10:00
|
|
|
};
|
|
|
|
|
2018-03-08 18:57:21 +11:00
|
|
|
state = {
|
2019-05-02 16:34:32 +10:00
|
|
|
visible: displayMedia !== 'hide_all' && !this.props.attachment.getIn(['status', 'sensitive']) || displayMedia === 'show_all',
|
|
|
|
loaded: false,
|
2018-03-08 18:57:21 +11:00
|
|
|
};
|
|
|
|
|
2019-05-02 16:34:32 +10:00
|
|
|
componentDidMount () {
|
|
|
|
if (this.props.attachment.get('blurhash')) {
|
|
|
|
this._decode();
|
2018-03-08 18:57:21 +11:00
|
|
|
}
|
2019-05-02 16:34:32 +10:00
|
|
|
}
|
2018-03-08 18:57:21 +11:00
|
|
|
|
2019-05-02 16:34:32 +10:00
|
|
|
componentDidUpdate (prevProps) {
|
|
|
|
if (prevProps.attachment.get('blurhash') !== this.props.attachment.get('blurhash') && this.props.attachment.get('blurhash')) {
|
|
|
|
this._decode();
|
|
|
|
}
|
2018-03-08 18:57:21 +11:00
|
|
|
}
|
|
|
|
|
2019-05-02 16:34:32 +10:00
|
|
|
_decode () {
|
|
|
|
const hash = this.props.attachment.get('blurhash');
|
|
|
|
const pixels = decode(hash, 32, 32);
|
|
|
|
|
|
|
|
if (pixels) {
|
|
|
|
const ctx = this.canvas.getContext('2d');
|
|
|
|
const imageData = new ImageData(pixels, 32, 32);
|
|
|
|
|
|
|
|
ctx.putImageData(imageData, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setCanvasRef = c => {
|
|
|
|
this.canvas = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-20 09:28:25 +10:00
|
|
|
}
|
2019-05-02 16:34:32 +10: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');
|
2019-05-06 13:33:56 +10:00
|
|
|
const title = status.get('spoiler_text') || attachment.get('description');
|
2019-05-02 16:34:32 +10:00
|
|
|
|
|
|
|
let thumbnail = '';
|
2019-05-09 02:01:33 +10:00
|
|
|
let icon;
|
2019-05-02 16:34:32 +10:00
|
|
|
|
|
|
|
if (attachment.get('type') === 'unknown') {
|
|
|
|
// Skip
|
|
|
|
} 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;
|
|
|
|
|
|
|
|
thumbnail = (
|
|
|
|
<img
|
|
|
|
src={attachment.get('preview_url')}
|
|
|
|
alt={attachment.get('description')}
|
|
|
|
title={attachment.get('description')}
|
|
|
|
style={{ objectPosition: `${x}% ${y}%` }}
|
|
|
|
onLoad={this.handleImageLoad}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} else if (['gifv', 'video'].indexOf(attachment.get('type')) !== -1) {
|
|
|
|
const autoPlay = !isIOS() && autoPlayGif;
|
|
|
|
|
|
|
|
thumbnail = (
|
|
|
|
<div className={classNames('media-gallery__gifv', { autoplay: autoPlay })}>
|
|
|
|
<video
|
|
|
|
className='media-gallery__item-gifv-thumbnail'
|
|
|
|
aria-label={attachment.get('description')}
|
|
|
|
title={attachment.get('description')}
|
|
|
|
role='application'
|
|
|
|
src={attachment.get('url')}
|
|
|
|
onMouseEnter={this.handleMouseEnter}
|
|
|
|
onMouseLeave={this.handleMouseLeave}
|
|
|
|
autoPlay={autoPlay}
|
|
|
|
loop
|
|
|
|
muted
|
|
|
|
/>
|
2017-05-20 09:28:25 +10:00
|
|
|
|
2019-05-02 16:34:32 +10:00
|
|
|
<span className='media-gallery__gifv__label'>GIF</span>
|
|
|
|
</div>
|
2018-03-08 18:57:21 +11:00
|
|
|
);
|
2017-05-20 09:28:25 +10:00
|
|
|
}
|
|
|
|
|
2019-05-09 02:01:33 +10:00
|
|
|
if (!visible) {
|
|
|
|
icon = (
|
|
|
|
<span className='account-gallery__item__icons'>
|
|
|
|
<Icon id='eye-slash' />
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-05-20 09:28:25 +10:00
|
|
|
return (
|
2019-05-02 16:34:32 +10:00
|
|
|
<div className='account-gallery__item' style={{ width, height }}>
|
2019-05-06 13:33:56 +10:00
|
|
|
<a className='media-gallery__item-thumbnail' href={status.get('url')} target='_blank' onClick={this.handleClick} title={title}>
|
2019-05-02 16:34:32 +10:00
|
|
|
<canvas width={32} height={32} ref={this.setCanvasRef} className={classNames('media-gallery__preview', { 'media-gallery__preview--hidden': visible && loaded })} />
|
|
|
|
{visible && thumbnail}
|
2019-05-09 02:01:33 +10:00
|
|
|
{!visible && icon}
|
2019-05-02 16:34:32 +10:00
|
|
|
</a>
|
2017-05-20 09:28:25 +10:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2017-05-21 01:31:47 +10:00
|
|
|
|
2017-05-20 09:28:25 +10:00
|
|
|
}
|