Skip to content

Commit

Permalink
update email iframe height on message focus
Browse files Browse the repository at this point in the history
  • Loading branch information
jorsn committed Jun 4, 2024
1 parent d6724e1 commit 977f0fe
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions ui/thread-view.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,42 @@
Do not add 'allow-scripts' to the sandbox attribute. This would allow the
document to execute scripts itself.
-->
<iframe class="body_iframe" sandbox="allow-same-origin" onload="sizeIframe(this);" srcdoc=""></iframe>
<iframe class="body_iframe" sandbox="allow-same-origin" onload="handleLoadIframe(this);" srcdoc=""></iframe>
</div>

<script>
function onSetFocused(element, callback) {
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.target.classList.contains("focused")) {
callback(mutation.target);
}
});
});

observer.observe(element, { attributeFilter: ["class"] });

return observer.disconnect;
}

function sizeIframe(iframe) {
iframe.style.height = (iframe.contentWindow.document.body.scrollHeight + 20) + 'px';
iframe.height = 0; // hack, no idea why it only works with this
iframe.height = iframe.contentWindow.document.body.scrollHeight;
}

function getParentWithClass(element, c) {
p = element.parentElement;
while (!p.classList.contains(c)) {
p = p.parentElement;
}

return p;
}

function handleLoadIframe(iframe) {
sizeIframe(iframe);
onSetFocused(getParentWithClass(iframe, "email"), _ => sizeIframe(iframe));
};
</script>

</body>
Expand Down

0 comments on commit 977f0fe

Please sign in to comment.