2019-10-25 05:44:42 +09:00
|
|
|
import PropTypes from 'prop-types';
|
2023-05-24 00:15:17 +09:00
|
|
|
|
|
|
|
import classNames from 'classnames';
|
|
|
|
|
2019-10-25 05:44:42 +09:00
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes';
|
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
2017-05-20 08:28:25 +09:00
|
|
|
|
2024-01-13 05:16:48 +09:00
|
|
|
import AudiotrackIcon from 'mastodon/../material-icons/400-24px/music_note.svg?react';
|
|
|
|
import PlayArrowIcon from 'mastodon/../material-icons/400-24px/play_arrow.svg?react';
|
|
|
|
import VisibilityOffIcon from 'mastodon/../material-icons/400-24px/visibility_off.svg?react';
|
2023-05-24 00:15:17 +09:00
|
|
|
import { Blurhash } from 'mastodon/components/blurhash';
|
|
|
|
import { Icon } from 'mastodon/components/icon';
|
|
|
|
import { autoPlayGif, displayMedia, useBlurhash } from 'mastodon/initial_state';
|
|
|
|
|
2017-06-24 02:36:54 +09:00
|
|
|
export default class MediaItem extends ImmutablePureComponent {
|
2017-05-20 08:28:25 +09:00
|
|
|
|
|
|
|
static propTypes = {
|
2019-05-02 15:34:32 +09:00
|
|
|
attachment: ImmutablePropTypes.map.isRequired,
|
|
|
|
displayWidth: PropTypes.number.isRequired,
|
|
|
|
onOpenMedia: PropTypes.func.isRequired,
|
2017-05-20 08:28:25 +09:00
|
|
|
};
|
|
|
|
|
2018-03-08 16:57:21 +09:00
|
|
|
state = {
|
2019-05-02 15:34:32 +09:00
|
|
|
visible: displayMedia !== 'hide_all' && !this.props.attachment.getIn(['status', 'sensitive']) || displayMedia === 'show_all',
|
|
|
|
loaded: false,
|
2018-03-08 16:57:21 +09:00
|
|
|
};
|
|
|
|
|
2019-05-02 15:34:32 +09:00
|
|
|
handleImageLoad = () => {
|
|
|
|
this.setState({ loaded: true });
|
2023-01-30 09:45:35 +09:00
|
|
|
};
|
2019-05-02 15:34:32 +09:00
|
|
|
|
|
|
|
handleMouseEnter = e => {
|
|
|
|
if (this.hoverToPlay()) {
|
|
|
|
e.target.play();
|
|
|
|
}
|
2023-01-30 09:45:35 +09:00
|
|
|
};
|
2019-05-02 15:34:32 +09:00
|
|
|
|
|
|
|
handleMouseLeave = e => {
|
|
|
|
if (this.hoverToPlay()) {
|
|
|
|
e.target.pause();
|
|
|
|
e.target.currentTime = 0;
|
2017-05-20 08:28:25 +09:00
|
|
|
}
|
2023-01-30 09:45:35 +09:00
|
|
|
};
|
2019-05-02 15:34:32 +09: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 });
|
|
|
|
}
|
|
|
|
}
|
2023-01-30 09:45:35 +09:00
|
|
|
};
|
2019-05-02 15:34:32 +09:00
|
|
|
|
|
|
|
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-11 05:09:28 +09:00
|
|
|
const title = status.get('spoiler_text') || attachment.get('description');
|
2019-05-02 15:34:32 +09:00
|
|
|
|
2020-07-11 05:09:28 +09:00
|
|
|
let thumbnail, label, icon, content;
|
2019-05-02 15:34:32 +09:00
|
|
|
|
2020-07-11 05:09:28 +09:00
|
|
|
if (!visible) {
|
|
|
|
icon = (
|
2019-08-21 02:46:41 +09:00
|
|
|
<span className='account-gallery__item__icons'>
|
2023-10-25 02:45:08 +09:00
|
|
|
<Icon id='eye-slash' icon={VisibilityOffIcon} />
|
2019-08-21 02:46:41 +09:00
|
|
|
</span>
|
|
|
|
);
|
2020-07-11 05:09:28 +09:00
|
|
|
} else {
|
|
|
|
if (['audio', 'video'].includes(attachment.get('type'))) {
|
|
|
|
content = (
|
|
|
|
<img
|
2023-03-23 13:17:29 +09:00
|
|
|
src={attachment.get('preview_url') || status.getIn(['account', 'avatar_static'])}
|
2020-07-11 05:09:28 +09:00
|
|
|
alt={attachment.get('description')}
|
2023-02-27 04:13:27 +09:00
|
|
|
lang={status.get('language')}
|
2020-07-11 05:09:28 +09:00
|
|
|
onLoad={this.handleImageLoad}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
if (attachment.get('type') === 'audio') {
|
2023-10-25 02:45:08 +09:00
|
|
|
label = <Icon id='music' icon={AudiotrackIcon} />;
|
2020-07-11 05:09:28 +09:00
|
|
|
} else {
|
2023-10-25 02:45:08 +09:00
|
|
|
label = <Icon id='play' icon={PlayArrowIcon} />;
|
2020-07-11 05:09:28 +09:00
|
|
|
}
|
|
|
|
} 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')}
|
2023-02-27 04:13:27 +09:00
|
|
|
lang={status.get('language')}
|
2020-07-11 05:09:28 +09:00
|
|
|
style={{ objectPosition: `${x}% ${y}%` }}
|
|
|
|
onLoad={this.handleImageLoad}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} else if (attachment.get('type') === 'gifv') {
|
|
|
|
content = (
|
2019-05-02 15:34:32 +09:00
|
|
|
<video
|
|
|
|
className='media-gallery__item-gifv-thumbnail'
|
|
|
|
aria-label={attachment.get('description')}
|
2022-12-16 02:37:47 +09:00
|
|
|
title={attachment.get('description')}
|
2023-02-27 04:13:27 +09:00
|
|
|
lang={status.get('language')}
|
2019-05-02 15:34:32 +09:00
|
|
|
role='application'
|
|
|
|
src={attachment.get('url')}
|
|
|
|
onMouseEnter={this.handleMouseEnter}
|
|
|
|
onMouseLeave={this.handleMouseLeave}
|
2022-11-28 04:46:23 +09:00
|
|
|
autoPlay={autoPlayGif}
|
|
|
|
playsInline
|
2019-05-02 15:34:32 +09:00
|
|
|
loop
|
|
|
|
muted
|
|
|
|
/>
|
2020-07-11 05:09:28 +09:00
|
|
|
);
|
|
|
|
|
|
|
|
label = 'GIF';
|
|
|
|
}
|
|
|
|
|
|
|
|
thumbnail = (
|
|
|
|
<div className='media-gallery__gifv'>
|
|
|
|
{content}
|
2017-05-20 08:28:25 +09:00
|
|
|
|
2023-07-26 10:33:31 +09:00
|
|
|
{label && (
|
|
|
|
<div className='media-gallery__item__badges'>
|
|
|
|
<span className='media-gallery__gifv__label'>{label}</span>
|
|
|
|
</div>
|
|
|
|
)}
|
2019-05-02 15:34:32 +09:00
|
|
|
</div>
|
2018-03-08 16:57:21 +09:00
|
|
|
);
|
2017-05-20 08:28:25 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2019-05-02 15:34:32 +09:00
|
|
|
<div className='account-gallery__item' style={{ width, height }}>
|
2023-02-13 23:12:14 +09:00
|
|
|
<a className='media-gallery__item-thumbnail' href={`/@${status.getIn(['account', 'acct'])}/${status.get('id')}`} onClick={this.handleClick} title={title} target='_blank' rel='noopener noreferrer'>
|
2020-07-09 20:01:30 +09:00
|
|
|
<Blurhash
|
|
|
|
hash={attachment.get('blurhash')}
|
2020-07-11 05:09:28 +09:00
|
|
|
className={classNames('media-gallery__preview', { 'media-gallery__preview--hidden': visible && loaded })}
|
2020-07-09 20:01:30 +09:00
|
|
|
dummy={!useBlurhash}
|
|
|
|
/>
|
2020-07-11 05:09:28 +09:00
|
|
|
|
|
|
|
{visible ? thumbnail : icon}
|
2019-05-02 15:34:32 +09:00
|
|
|
</a>
|
2017-05-20 08:28:25 +09:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2017-05-21 00:31:47 +09:00
|
|
|
|
2017-05-20 08:28:25 +09:00
|
|
|
}
|