闭社主体 forked from https://github.com/tootsuite/mastodon
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.

84 lines
2.1 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 axios from 'axios';
  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. axios.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.height = iframeDocument.body.scrollHeight + 'px';
  28. });
  29. }
  30. setIframeRef = c => {
  31. this.iframe = c;
  32. }
  33. handleTextareaClick = (e) => {
  34. e.target.select();
  35. }
  36. render () {
  37. const { oembed } = this.state;
  38. return (
  39. <div className='modal-root__modal embed-modal'>
  40. <h4><FormattedMessage id='status.embed' defaultMessage='Embed' /></h4>
  41. <div className='embed-modal__container'>
  42. <p className='hint'>
  43. <FormattedMessage id='embed.instructions' defaultMessage='Embed this status on your website by copying the code below.' />
  44. </p>
  45. <input
  46. type='text'
  47. className='embed-modal__html'
  48. readOnly
  49. value={oembed && oembed.html || ''}
  50. onClick={this.handleTextareaClick}
  51. />
  52. <p className='hint'>
  53. <FormattedMessage id='embed.preview' defaultMessage='Here is what it will look like:' />
  54. </p>
  55. <iframe
  56. className='embed-modal__iframe'
  57. scrolling='no'
  58. frameBorder='0'
  59. ref={this.setIframeRef}
  60. title='preview'
  61. />
  62. </div>
  63. </div>
  64. );
  65. }
  66. }