2017-08-31 01:38:35 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component';
|
2019-10-03 01:17:29 +00:00
|
|
|
import { defineMessages, FormattedMessage, injectIntl } from 'react-intl';
|
2022-10-11 08:32:31 +00:00
|
|
|
import api from 'flavours/glitch/api';
|
2019-10-03 01:17:29 +00:00
|
|
|
import IconButton from 'flavours/glitch/components/icon_button';
|
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
close: { id: 'lightbox.close', defaultMessage: 'Close' },
|
|
|
|
});
|
2017-08-31 01:38:35 +00:00
|
|
|
|
2019-09-09 13:16:08 +00:00
|
|
|
export default @injectIntl
|
|
|
|
class EmbedModal extends ImmutablePureComponent {
|
2017-08-31 01:38:35 +00:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
url: PropTypes.string.isRequired,
|
|
|
|
onClose: PropTypes.func.isRequired,
|
2019-02-10 20:28:29 +00:00
|
|
|
onError: PropTypes.func.isRequired,
|
2017-08-31 01:38:35 +00:00
|
|
|
intl: PropTypes.object.isRequired,
|
2023-02-03 19:52:07 +00:00
|
|
|
};
|
2017-08-31 01:38:35 +00:00
|
|
|
|
|
|
|
state = {
|
|
|
|
loading: false,
|
|
|
|
oembed: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
componentDidMount () {
|
|
|
|
const { url } = this.props;
|
|
|
|
|
|
|
|
this.setState({ loading: true });
|
|
|
|
|
2018-01-09 16:51:14 +00:00
|
|
|
api().post('/api/web/embed', { url }).then(res => {
|
2017-08-31 01:38:35 +00: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-09 14:23:44 +00:00
|
|
|
this.iframe.width = iframeDocument.body.scrollWidth;
|
|
|
|
this.iframe.height = iframeDocument.body.scrollHeight;
|
2019-02-10 20:28:29 +00:00
|
|
|
}).catch(error => {
|
|
|
|
this.props.onError(error);
|
2017-08-31 01:38:35 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
setIframeRef = c => {
|
|
|
|
this.iframe = c;
|
2023-02-03 19:52:07 +00:00
|
|
|
};
|
2017-08-31 01:38:35 +00:00
|
|
|
|
|
|
|
handleTextareaClick = (e) => {
|
|
|
|
e.target.select();
|
2023-02-03 19:52:07 +00:00
|
|
|
};
|
2017-08-31 01:38:35 +00:00
|
|
|
|
|
|
|
render () {
|
2019-10-03 01:17:29 +00:00
|
|
|
const { intl, onClose } = this.props;
|
2017-08-31 01:38:35 +00:00
|
|
|
const { oembed } = this.state;
|
|
|
|
|
|
|
|
return (
|
2019-10-03 01:17:29 +00:00
|
|
|
<div className='modal-root__modal report-modal embed-modal'>
|
|
|
|
<div className='report-modal__target'>
|
|
|
|
<IconButton className='media-modal__close' title={intl.formatMessage(messages.close)} icon='times' onClick={onClose} size={16} />
|
|
|
|
<FormattedMessage id='status.embed' defaultMessage='Embed' />
|
|
|
|
</div>
|
2017-08-31 01:38:35 +00:00
|
|
|
|
2019-10-03 01:17:29 +00:00
|
|
|
<div className='report-modal__container embed-modal__container' style={{ display: 'block' }}>
|
2017-08-31 01:38:35 +00:00
|
|
|
<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}
|
2018-12-17 20:42:18 +00:00
|
|
|
sandbox='allow-same-origin'
|
2017-08-31 01:38:35 +00:00
|
|
|
title='preview'
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|