fix: GitHub Gist embed reliability, closes #1400

This commit is contained in:
Tom Moor
2020-08-09 21:53:57 -07:00
parent e2bd03494d
commit 07d488c826

View File

@@ -13,31 +13,16 @@ type Props = {|
|};
class Gist extends React.Component<Props> {
iframeNode: ?HTMLIFrameElement;
static ENABLED = [URL_REGEX];
componentDidMount() {
this.updateIframeContent();
}
get id() {
const gistUrl = new URL(this.props.attrs.href);
return gistUrl.pathname.split("/")[2];
}
updateIframeContent() {
const id = this.id;
const iframe = this.iframeNode;
updateIframeContent = (iframe: ?HTMLIFrameElement) => {
if (!iframe) return;
// We need to add some temporary content to the iframe for the document
// to be available, otherwise it's undefined on first load
const temp = document.getElementById("gist");
if (temp) {
temp.innerHTML = "";
temp.appendChild(iframe);
}
const id = this.id;
// $FlowFixMe
let doc = iframe.document;
@@ -56,16 +41,14 @@ class Gist extends React.Component<Props> {
doc.open();
doc.writeln(iframeHtml);
doc.close();
}
};
render() {
const id = this.id;
return (
<iframe
ref={(ref) => {
this.iframeNode = ref;
}}
ref={this.updateIframeContent}
type="text/html"
frameBorder="0"
width="100%"