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.

97 lines
2.7 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import ImmutablePureComponent from 'react-immutable-pure-component';
  4. import { defineMessages, FormattedMessage, injectIntl } from 'react-intl';
  5. import api from 'mastodon/api';
  6. import IconButton from 'mastodon/components/icon_button';
  7. const messages = defineMessages({
  8. close: { id: 'lightbox.close', defaultMessage: 'Close' },
  9. });
  10. export default @injectIntl
  11. class EmbedModal extends ImmutablePureComponent {
  12. static propTypes = {
  13. url: PropTypes.string.isRequired,
  14. onClose: PropTypes.func.isRequired,
  15. onError: PropTypes.func.isRequired,
  16. intl: PropTypes.object.isRequired,
  17. }
  18. state = {
  19. loading: false,
  20. oembed: null,
  21. };
  22. componentDidMount () {
  23. const { url } = this.props;
  24. this.setState({ loading: true });
  25. api().post('/api/web/embed', { url }).then(res => {
  26. this.setState({ loading: false, oembed: res.data });
  27. const iframeDocument = this.iframe.contentWindow.document;
  28. iframeDocument.open();
  29. iframeDocument.write(res.data.html);
  30. iframeDocument.close();
  31. iframeDocument.body.style.margin = 0;
  32. this.iframe.width = iframeDocument.body.scrollWidth;
  33. this.iframe.height = iframeDocument.body.scrollHeight;
  34. }).catch(error => {
  35. this.props.onError(error);
  36. });
  37. }
  38. setIframeRef = c => {
  39. this.iframe = c;
  40. }
  41. handleTextareaClick = (e) => {
  42. e.target.select();
  43. }
  44. render () {
  45. const { intl, onClose } = this.props;
  46. const { oembed } = this.state;
  47. return (
  48. <div className='modal-root__modal report-modal embed-modal'>
  49. <div className='report-modal__target'>
  50. <IconButton className='media-modal__close' title={intl.formatMessage(messages.close)} icon='times' onClick={onClose} size={16} />
  51. <FormattedMessage id='status.embed' defaultMessage='Embed' />
  52. </div>
  53. <div className='report-modal__container embed-modal__container' style={{ display: 'block' }}>
  54. <p className='hint'>
  55. <FormattedMessage id='embed.instructions' defaultMessage='Embed this status on your website by copying the code below.' />
  56. </p>
  57. <input
  58. type='text'
  59. className='embed-modal__html'
  60. readOnly
  61. value={oembed && oembed.html || ''}
  62. onClick={this.handleTextareaClick}
  63. />
  64. <p className='hint'>
  65. <FormattedMessage id='embed.preview' defaultMessage='Here is what it will look like:' />
  66. </p>
  67. <iframe
  68. className='embed-modal__iframe'
  69. frameBorder='0'
  70. ref={this.setIframeRef}
  71. sandbox='allow-same-origin'
  72. title='preview'
  73. />
  74. </div>
  75. </div>
  76. );
  77. }
  78. }