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.

85 lines
2.2 KiB

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