Skip to content

Commit

Permalink
Fix race condition in mermaid observer (#32599) (#32673)
Browse files Browse the repository at this point in the history
Backport #32599 by william-allspice
  • Loading branch information
GiteaBot authored Nov 29, 2024
1 parent 27489f2 commit 4b73e92
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions web_src/js/markup/mermaid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);
Expand Down

0 comments on commit 4b73e92

Please sign in to comment.