From b1dd8097879a814e649836729eb1ff052903369f Mon Sep 17 00:00:00 2001 From: Daksh Pokar Date: Thu, 17 Oct 2024 16:24:17 -0500 Subject: [PATCH] fix: address iframe resizing issue in jupyter notebooks --- maidr/core/maidr.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/maidr/core/maidr.py b/maidr/core/maidr.py index 6e9d385..e296107 100644 --- a/maidr/core/maidr.py +++ b/maidr/core/maidr.py @@ -193,24 +193,36 @@ def generate_iframe_script(unique_id: str) -> str: resizing_script = f""" function resizeIframe() {{ let iframe = document.getElementById('{unique_id}'); + if (iframe && iframe.contentWindow && iframe.contentWindow.document) {{ let iframeDocument = iframe.contentWindow.document; - let brailleContainer = iframeDocument.getElementById('braille-div'); + let brailleContainer = iframeDocument.getElementById('braille-input'); + + iframe.style.height = 'auto'; + let height = iframeDocument.body.scrollHeight; if (brailleContainer && brailleContainer === iframeDocument.activeElement) {{ - height *= 1.35; # Increase height by 35% if braille-container is in focus + height += 100; + }}else{{ + height += 50 }} - iframe.style.height = (height + 150) + 'px'; + + iframe.style.height = (height) + 'px'; iframe.style.width = iframeDocument.body.scrollWidth + 'px'; }} }} let iframe = document.getElementById('{unique_id}'); + resizeIframe(); iframe.onload = function() {{ resizeIframe(); iframe.contentWindow.addEventListener('resize', resizeIframe); - iframe.contentWindow.document.addEventListener('focusin', resizeIframe); - iframe.contentWindow.document.addEventListener('focusout', resizeIframe); }}; + iframe.contentWindow.document.addEventListener('focusin', () => {{ + resizeIframe(); + }}); + iframe.contentWindow.document.addEventListener('focusout', () => {{ + resizeIframe(); + }}); """ return resizing_script