gab-social/app/javascript/gabsocial/components/modal/embed_modal.js

94 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-07-02 08:10:25 +01:00
import ImmutablePureComponent from 'react-immutable-pure-component';
import { defineMessages, injectIntl } from 'react-intl';
2020-02-24 23:25:55 +00:00
import api from '../../api'
2019-07-02 08:10:25 +01:00
const messages = defineMessages({
embed: { id: 'status.embed', defaultMessage: 'Embed' },
instructions: { id: 'embed.instructions', defaultMessage: 'Embed this status on your website by copying the code below.' },
preview: { id: 'embed.preview', defaultMessage: 'Here is what it will look like:' },
});
2020-02-25 16:04:44 +00:00
export default
@injectIntl
2019-07-02 08:10:25 +01:00
class EmbedModal extends ImmutablePureComponent {
static propTypes = {
url: PropTypes.string.isRequired,
onClose: PropTypes.func.isRequired,
onError: PropTypes.func.isRequired,
intl: PropTypes.object.isRequired,
}
state = {
loading: false,
oembed: null,
};
componentDidMount () {
const { url } = this.props;
this.setState({ loading: true });
api().post('/api/web/embed', { url }).then(res => {
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;
this.iframe.width = iframeDocument.body.scrollWidth;
this.iframe.height = iframeDocument.body.scrollHeight;
}).catch(error => {
this.props.onError(error);
});
}
setIframeRef = c => {
this.iframe = c;
}
handleTextareaClick = (e) => {
e.target.select();
}
render () {
const { oembed, intl } = this.state;
2019-07-02 08:10:25 +01:00
return (
<div className='modal-root__modal embed-modal'>
<h4>{intl.formatMessage(messages.embed)}</h4>
2019-07-02 08:10:25 +01:00
<div className='embed-modal__container'>
<p className='hint'>
{intl.formatMessage(messages.instructions)}
2019-07-02 08:10:25 +01:00
</p>
<input
type='text'
className='embed-modal__html'
readOnly
value={oembed && oembed.html || ''}
onClick={this.handleTextareaClick}
/>
<p className='hint'>
{intl.formatMessage(messages.preview)}
2019-07-02 08:10:25 +01:00
</p>
<iframe
className='embed-modal__iframe'
frameBorder='0'
ref={this.setIframeRef}
sandbox='allow-same-origin'
title='preview'
/>
</div>
</div>
);
}
}