2017-08-31 11:38:35 +10:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
|
|
|
import { FormattedMessage, injectIntl } from 'react-intl';
|
2018-01-09 06:01:33 +11:00
|
|
|
import api from '../../../api';
|
2017-08-31 11:38:35 +10:00
|
|
|
|
2018-09-15 01:59:48 +10:00
|
|
|
export default @injectIntl
|
|
|
|
class EmbedModal extends ImmutablePureComponent {
|
2017-08-31 11:38:35 +10:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
url: PropTypes.string.isRequired,
|
|
|
|
onClose: PropTypes.func.isRequired,
|
2018-04-02 22:51:02 +10:00
|
|
|
onError: PropTypes.func.isRequired,
|
2017-08-31 11:38:35 +10:00
|
|
|
intl: PropTypes.object.isRequired,
|
|
|
|
}
|
|
|
|
|
|
|
|
state = {
|
|
|
|
loading: false,
|
|
|
|
oembed: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
componentDidMount () {
|
|
|
|
const { url } = this.props;
|
|
|
|
|
|
|
|
this.setState({ loading: true });
|
|
|
|
|
2018-01-09 06:01:33 +11:00
|
|
|
api().post('/api/web/embed', { url }).then(res => {
|
2017-08-31 11:38:35 +10:00
|
|
|
this.setState({ loading: false, oembed: res.data });
|
|
|
|
|
|
|
|
const iframeDocument = this.iframe.contentWindow.document;
|
|
|
|
|
|
|
|
iframeDocument.open();
|
|
|
|
iframeDocument.write(res.data.html);
|
|
|
|
iframeDocument.close();
|
|
|
|
|
|
|
|
iframeDocument.body.style.margin = 0;
|
2017-09-10 00:23:44 +10:00
|
|
|
this.iframe.width = iframeDocument.body.scrollWidth;
|
|
|
|
this.iframe.height = iframeDocument.body.scrollHeight;
|
2018-04-02 22:51:02 +10:00
|
|
|
}).catch(error => {
|
|
|
|
this.props.onError(error);
|
2017-08-31 11:38:35 +10:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
setIframeRef = c => {
|
|
|
|
this.iframe = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleTextareaClick = (e) => {
|
|
|
|
e.target.select();
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const { oembed } = this.state;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className='modal-root__modal embed-modal'>
|
|
|
|
<h4><FormattedMessage id='status.embed' defaultMessage='Embed' /></h4>
|
|
|
|
|
|
|
|
<div className='embed-modal__container'>
|
|
|
|
<p className='hint'>
|
|
|
|
<FormattedMessage id='embed.instructions' defaultMessage='Embed this status on your website by copying the code below.' />
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<input
|
|
|
|
type='text'
|
|
|
|
className='embed-modal__html'
|
|
|
|
readOnly
|
|
|
|
value={oembed && oembed.html || ''}
|
|
|
|
onClick={this.handleTextareaClick}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<p className='hint'>
|
|
|
|
<FormattedMessage id='embed.preview' defaultMessage='Here is what it will look like:' />
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<iframe
|
|
|
|
className='embed-modal__iframe'
|
|
|
|
frameBorder='0'
|
|
|
|
ref={this.setIframeRef}
|
|
|
|
title='preview'
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|