Spaces:
Runtime error
Runtime error
File size: 1,333 Bytes
db5855f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import type { IResizeMessage, IScrollMessage } from './iframe-message-emitter';
const notebooksSelectorElement = document.getElementById('notebooks-selector') as HTMLIFrameElement;
if (!notebooksSelectorElement) {
throw new Error('Unable to find notebooks selector iframe element.');
}
function setIframeHeight(iframeElement: HTMLIFrameElement, heightPx: number): void {
iframeElement.style.height = `${heightPx}px`;
}
function setInitialIframeHeight(iframeElement: HTMLIFrameElement): void {
const iframeBodyHeight = iframeElement.contentDocument?.body?.offsetHeight;
if (iframeBodyHeight) {
setIframeHeight(iframeElement, iframeBodyHeight);
}
}
window.onmessage = (message: MessageEvent<IResizeMessage | IScrollMessage>) => {
const { origin: allowedOrigin } = new URL(
import.meta.env.PROD ? (import.meta.env.VITE_APP_LOCATION as string) : import.meta.url
);
if (message.origin !== allowedOrigin) {
return;
}
if (message.data.type === 'resize' && message.data.height) {
notebooksSelectorElement.style.height = message.data.height + 'px';
return;
}
if (message.data.type === 'scroll') {
notebooksSelectorElement.scrollIntoView({ behavior: 'smooth' });
return;
}
};
setInitialIframeHeight(notebooksSelectorElement);
export {};
|