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.
76 lines
2.4 KiB
76 lines
2.4 KiB
7 years ago
|
import React, { PureComponent, Fragment } from 'react';
|
||
7 years ago
|
import ReactDOM from 'react-dom';
|
||
|
import PropTypes from 'prop-types';
|
||
|
import { IntlProvider, addLocaleData } from 'react-intl';
|
||
|
import { getLocale } from 'mastodon/locales';
|
||
|
import MediaGallery from 'flavours/glitch/components/media_gallery';
|
||
7 years ago
|
import Video from 'flavours/glitch/features/video';
|
||
|
import Card from 'flavours/glitch/features/status/components/card';
|
||
7 years ago
|
import ModalRoot from 'flavours/glitch/components/modal_root';
|
||
|
import MediaModal from 'flavours/glitch/features/ui/components/media_modal';
|
||
|
import { fromJS } from 'immutable';
|
||
|
|
||
|
const { localeData, messages } = getLocale();
|
||
|
addLocaleData(localeData);
|
||
|
|
||
7 years ago
|
const MEDIA_COMPONENTS = { MediaGallery, Video, Card };
|
||
|
|
||
|
export default class MediaContainer extends PureComponent {
|
||
7 years ago
|
|
||
|
static propTypes = {
|
||
|
locale: PropTypes.string.isRequired,
|
||
7 years ago
|
components: PropTypes.object.isRequired,
|
||
7 years ago
|
};
|
||
|
|
||
|
state = {
|
||
|
media: null,
|
||
|
index: null,
|
||
|
};
|
||
|
|
||
|
handleOpenMedia = (media, index) => {
|
||
7 years ago
|
document.body.classList.add('media-standalone__body');
|
||
7 years ago
|
this.setState({ media, index });
|
||
|
}
|
||
|
|
||
|
handleCloseMedia = () => {
|
||
7 years ago
|
document.body.classList.remove('media-standalone__body');
|
||
7 years ago
|
this.setState({ media: null, index: null });
|
||
|
}
|
||
|
|
||
|
render () {
|
||
7 years ago
|
const { locale, components } = this.props;
|
||
7 years ago
|
|
||
|
return (
|
||
|
<IntlProvider locale={locale} messages={messages}>
|
||
7 years ago
|
<Fragment>
|
||
|
{[].map.call(components, (component, i) => {
|
||
|
const componentName = component.getAttribute('data-component');
|
||
|
const Component = MEDIA_COMPONENTS[componentName];
|
||
|
const { media, card, ...props } = JSON.parse(component.getAttribute('data-props'));
|
||
|
|
||
|
Object.assign(props, {
|
||
|
...(media ? { media: fromJS(media) } : {}),
|
||
|
...(card ? { card: fromJS(card) } : {}),
|
||
|
});
|
||
7 years ago
|
|
||
|
return ReactDOM.createPortal(
|
||
7 years ago
|
<Component onOpenMedia={this.handleOpenMedia} {...props} key={`media-${i}`} />,
|
||
|
component,
|
||
7 years ago
|
);
|
||
|
})}
|
||
|
<ModalRoot onClose={this.handleCloseMedia}>
|
||
|
{this.state.media === null || this.state.index === null ? null : (
|
||
|
<MediaModal
|
||
|
media={this.state.media}
|
||
|
index={this.state.index}
|
||
|
onClose={this.handleCloseMedia}
|
||
|
/>
|
||
|
)}
|
||
|
</ModalRoot>
|
||
7 years ago
|
</Fragment>
|
||
7 years ago
|
</IntlProvider>
|
||
|
);
|
||
|
}
|
||
|
|
||
|
}
|