Fix height for SandboxFrame (#676)
This commit is contained in:
parent
5d4b193865
commit
6dba2e4792
|
@ -15,6 +15,7 @@ interface SandboxFrameState {
|
||||||
|
|
||||||
export class SandboxFrame extends React.PureComponent<SandboxFrameProps, SandboxFrameState> {
|
export class SandboxFrame extends React.PureComponent<SandboxFrameProps, SandboxFrameState> {
|
||||||
private resizeObserver: ResizeObserver;
|
private resizeObserver: ResizeObserver;
|
||||||
|
private mutationObserver: MutationObserver;
|
||||||
|
|
||||||
constructor(props: SandboxFrameProps) {
|
constructor(props: SandboxFrameProps) {
|
||||||
super(props);
|
super(props);
|
||||||
|
@ -43,22 +44,26 @@ export class SandboxFrame extends React.PureComponent<SandboxFrameProps, Sandbox
|
||||||
|
|
||||||
componentWillUnmount(): void {
|
componentWillUnmount(): void {
|
||||||
this.resizeObserver?.disconnect();
|
this.resizeObserver?.disconnect();
|
||||||
|
this.mutationObserver?.disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
onFrameLoad(event: React.SyntheticEvent<HTMLIFrameElement, Event>): void {
|
onFrameLoad(event: React.SyntheticEvent<HTMLIFrameElement, Event>): void {
|
||||||
const doc = (event.target as HTMLIFrameElement).contentDocument;
|
const doc = (event.target as HTMLIFrameElement).contentDocument;
|
||||||
copyStyles(document, doc);
|
copyStyles(document, doc);
|
||||||
|
|
||||||
this.setState({
|
this.setState({ frameBody: doc.body });
|
||||||
frameBody: doc.body,
|
|
||||||
frameHeight: doc.body.scrollHeight,
|
|
||||||
});
|
|
||||||
|
|
||||||
|
this.mutationObserver = new MutationObserver(() => {
|
||||||
|
const bodyFirstElementChild = this.state.frameBody?.firstElementChild;
|
||||||
|
if (!this.resizeObserver && bodyFirstElementChild) {
|
||||||
this.resizeObserver = new ResizeObserver(() =>
|
this.resizeObserver = new ResizeObserver(() =>
|
||||||
this.setState({
|
this.setState({
|
||||||
frameHeight: this.state.frameBody.scrollHeight,
|
frameHeight: this.state.frameBody?.firstElementChild.scrollHeight,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
this.resizeObserver.observe(doc.body);
|
this.resizeObserver.observe(bodyFirstElementChild);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
this.mutationObserver.observe(doc.body, { childList: true });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue