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.

121 lines
3.9 KiB

  1. import React, { PureComponent, Fragment } from 'react';
  2. import ReactDOM from 'react-dom';
  3. import PropTypes from 'prop-types';
  4. import { IntlProvider, addLocaleData } from 'react-intl';
  5. import { fromJS } from 'immutable';
  6. import { getLocale } from 'mastodon/locales';
  7. import { getScrollbarWidth } from 'mastodon/utils/scrollbar';
  8. import MediaGallery from 'mastodon/components/media_gallery';
  9. import Poll from 'mastodon/components/poll';
  10. import Hashtag from 'mastodon/components/hashtag';
  11. import ModalRoot from 'mastodon/components/modal_root';
  12. import MediaModal from 'mastodon/features/ui/components/media_modal';
  13. import Video from 'mastodon/features/video';
  14. import Card from 'mastodon/features/status/components/card';
  15. import Audio from 'mastodon/features/audio';
  16. const { localeData, messages } = getLocale();
  17. addLocaleData(localeData);
  18. const MEDIA_COMPONENTS = { MediaGallery, Video, Card, Poll, Hashtag, Audio };
  19. export default class MediaContainer extends PureComponent {
  20. static propTypes = {
  21. locale: PropTypes.string.isRequired,
  22. components: PropTypes.object.isRequired,
  23. };
  24. state = {
  25. media: null,
  26. index: null,
  27. time: null,
  28. backgroundColor: null,
  29. options: null,
  30. };
  31. handleOpenMedia = (media, index) => {
  32. document.body.classList.add('with-modals--active');
  33. document.documentElement.style.marginRight = `${getScrollbarWidth()}px`;
  34. this.setState({ media, index });
  35. }
  36. handleOpenVideo = (options) => {
  37. const { components } = this.props;
  38. const { media } = JSON.parse(components[options.componetIndex].getAttribute('data-props'));
  39. const mediaList = fromJS(media);
  40. document.body.classList.add('with-modals--active');
  41. document.documentElement.style.marginRight = `${getScrollbarWidth()}px`;
  42. this.setState({ media: mediaList, options });
  43. }
  44. handleCloseMedia = () => {
  45. document.body.classList.remove('with-modals--active');
  46. document.documentElement.style.marginRight = 0;
  47. this.setState({
  48. media: null,
  49. index: null,
  50. time: null,
  51. backgroundColor: null,
  52. options: null,
  53. });
  54. }
  55. setBackgroundColor = color => {
  56. this.setState({ backgroundColor: color });
  57. }
  58. render () {
  59. const { locale, components } = this.props;
  60. return (
  61. <IntlProvider locale={locale} messages={messages}>
  62. <Fragment>
  63. {[].map.call(components, (component, i) => {
  64. const componentName = component.getAttribute('data-component');
  65. const Component = MEDIA_COMPONENTS[componentName];
  66. const { media, card, poll, hashtag, ...props } = JSON.parse(component.getAttribute('data-props'));
  67. Object.assign(props, {
  68. ...(media ? { media: fromJS(media) } : {}),
  69. ...(card ? { card: fromJS(card) } : {}),
  70. ...(poll ? { poll: fromJS(poll) } : {}),
  71. ...(hashtag ? { hashtag: fromJS(hashtag) } : {}),
  72. ...(componentName === 'Video' ? {
  73. componetIndex: i,
  74. onOpenVideo: this.handleOpenVideo,
  75. } : {
  76. onOpenMedia: this.handleOpenMedia,
  77. }),
  78. });
  79. return ReactDOM.createPortal(
  80. <Component {...props} key={`media-${i}`} />,
  81. component,
  82. );
  83. })}
  84. <ModalRoot backgroundColor={this.state.backgroundColor} onClose={this.handleCloseMedia}>
  85. {this.state.media && (
  86. <MediaModal
  87. media={this.state.media}
  88. index={this.state.index || 0}
  89. currentTime={this.state.options?.startTime}
  90. autoPlay={this.state.options?.autoPlay}
  91. volume={this.state.options?.defaultVolume}
  92. onClose={this.handleCloseMedia}
  93. onChangeBackgroundColor={this.setBackgroundColor}
  94. />
  95. )}
  96. </ModalRoot>
  97. </Fragment>
  98. </IntlProvider>
  99. );
  100. }
  101. }