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.

88 lines
2.5 KiB

  1. import React from 'react';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import PropTypes from 'prop-types';
  4. import Video from 'mastodon/features/video';
  5. import ImmutablePureComponent from 'react-immutable-pure-component';
  6. import { FormattedMessage } from 'react-intl';
  7. import classNames from 'classnames';
  8. import Icon from 'mastodon/components/icon';
  9. export const previewState = 'previewVideoModal';
  10. export default class VideoModal extends ImmutablePureComponent {
  11. static propTypes = {
  12. media: ImmutablePropTypes.map.isRequired,
  13. status: ImmutablePropTypes.map,
  14. options: PropTypes.shape({
  15. startTime: PropTypes.number,
  16. autoPlay: PropTypes.bool,
  17. defaultVolume: PropTypes.number,
  18. }),
  19. onClose: PropTypes.func.isRequired,
  20. };
  21. static contextTypes = {
  22. router: PropTypes.object,
  23. };
  24. componentDidMount () {
  25. if (this.context.router) {
  26. const history = this.context.router.history;
  27. history.push(history.location.pathname, previewState);
  28. this.unlistenHistory = history.listen(() => {
  29. this.props.onClose();
  30. });
  31. }
  32. }
  33. componentWillUnmount () {
  34. if (this.context.router) {
  35. this.unlistenHistory();
  36. if (this.context.router.history.location.state === previewState) {
  37. this.context.router.history.goBack();
  38. }
  39. }
  40. }
  41. handleStatusClick = e => {
  42. if (e.button === 0 && !(e.ctrlKey || e.metaKey)) {
  43. e.preventDefault();
  44. this.context.router.history.push(`/statuses/${this.props.status.get('id')}`);
  45. }
  46. }
  47. render () {
  48. const { media, status, onClose } = this.props;
  49. const options = this.props.options || {};
  50. return (
  51. <div className='modal-root__modal video-modal'>
  52. <div className='video-modal__container'>
  53. <Video
  54. preview={media.get('preview_url')}
  55. frameRate={media.getIn(['meta', 'original', 'frame_rate'])}
  56. blurhash={media.get('blurhash')}
  57. src={media.get('url')}
  58. currentTime={options.startTime}
  59. autoPlay={options.autoPlay}
  60. volume={options.defaultVolume}
  61. onCloseVideo={onClose}
  62. detailed
  63. alt={media.get('description')}
  64. />
  65. </div>
  66. {status && (
  67. <div className={classNames('media-modal__meta')}>
  68. <a href={status.get('url')} onClick={this.handleStatusClick}><Icon id='comments' /> <FormattedMessage id='lightbox.view_context' defaultMessage='View context' /></a>
  69. </div>
  70. )}
  71. </div>
  72. );
  73. }
  74. }