diff options
author | william-allspice <william@allspice.io> | 2024-11-26 07:37:24 +0100 |
---|---|---|
committer | Earl Warren <contact@earl-warren.org> | 2024-12-03 10:19:22 +0100 |
commit | 2faccf5374034e09bce5277d78e9328f9b10c2a5 (patch) | |
tree | f5caefa4280bb19c6e0a2cb6856b464fa46f9f6d /web_src | |
parent | Fix a bug in actions artifact test (#32672) (diff) | |
download | forgejo-2faccf5374034e09bce5277d78e9328f9b10c2a5.tar.xz forgejo-2faccf5374034e09bce5277d78e9328f9b10c2a5.zip |
Fix race condition in mermaid observer (#32599)
This Pull Request addresses a race condition in the updateIframeHeight
function where it is sometimes called when the iframe is not fully
loaded or accessible resulting in an alarming error message for the
user.
To address this we:
1. Add defensive programming within the updateIframeHeight function
2. Delay instantiating the intersection observer until the iframe has
loaded
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
(cherry picked from commit 88f5d33ab267f330ffaf02eb019e772ed06ed34f)
Diffstat (limited to 'web_src')
-rw-r--r-- | web_src/js/markup/mermaid.js | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/web_src/js/markup/mermaid.js b/web_src/js/markup/mermaid.js index e420ff12a2..cf69739fee 100644 --- a/web_src/js/markup/mermaid.js +++ b/web_src/js/markup/mermaid.js @@ -57,16 +57,12 @@ export async function renderMermaid() { mermaidBlock.append(btn); const updateIframeHeight = () => { - iframe.style.height = `${iframe.contentWindow.document.body.clientHeight}px`; + const body = iframe.contentWindow?.document?.body; + if (body) { + iframe.style.height = `${body.clientHeight}px`; + } }; - // update height when element's visibility state changes, for example when the diagram is inside - // a <details> + <summary> block and the <details> block becomes visible upon user interaction, it - // would initially set a incorrect height and the correct height is set during this callback. - (new IntersectionObserver(() => { - updateIframeHeight(); - }, {root: document.documentElement})).observe(iframe); - iframe.addEventListener('load', () => { pre.replaceWith(mermaidBlock); mermaidBlock.classList.remove('tw-hidden'); @@ -75,6 +71,13 @@ export async function renderMermaid() { mermaidBlock.classList.remove('is-loading'); iframe.classList.remove('tw-invisible'); }, 0); + + // update height when element's visibility state changes, for example when the diagram is inside + // a <details> + <summary> block and the <details> block becomes visible upon user interaction, it + // would initially set a incorrect height and the correct height is set during this callback. + (new IntersectionObserver(() => { + updateIframeHeight(); + }, {root: document.documentElement})).observe(iframe); }); document.body.append(mermaidBlock); |