logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

embed_modal.js (2270B)


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