Skip to content

Commit dd5d2ba

Browse files
committed
improve mermaid loading sequence
1 parent 38759ce commit dd5d2ba

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

web_src/css/base.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/* non-color variables */
1010
--border-radius: 0.28571429rem;
1111
--opacity-disabled: 0.55;
12-
--height-loading: 12rem;
12+
--height-loading: 16rem;
1313
/* base colors */
1414
--color-primary: #4183c4;
1515
--color-primary-contrast: #ffffff;

web_src/css/helpers.css

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
.gt-overflow-x-scroll { overflow-x: scroll !important; }
2626
.gt-cursor-default { cursor: default !important; }
2727
.gt-items-start { align-items: flex-start !important; }
28-
.gt-whitespace-pre { white-space: pre !important }
28+
.gt-whitespace-pre { white-space: pre !important; }
29+
.gt-invisible { visibility: hidden !important; }
2930

3031
.gt-mono {
3132
font-family: var(--fonts-monospace) !important;

web_src/js/markup/mermaid.js

+17-14
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ import {displayError} from './common.js';
44

55
const {mermaidMaxSourceCharacters} = window.config;
66

7-
const iframeCss = `
8-
:root {color-scheme: normal}
9-
body {margin: 0; padding: 0; overflow: hidden}
10-
#mermaid {display: block; margin: 0 auto}
11-
`;
7+
const iframeCss = `:root {color-scheme: normal}
8+
body {margin: 0; padding: 0; overflow: hidden}
9+
#mermaid {display: block; margin: 0 auto}`;
1210

1311
export async function renderMermaid() {
1412
const els = document.querySelectorAll('.markup code.language-mermaid');
@@ -45,23 +43,28 @@ export async function renderMermaid() {
4543
const {svg} = await mermaid.render('mermaid', source);
4644

4745
const iframe = document.createElement('iframe');
48-
iframe.classList.add('markup-render');
49-
iframe.sandbox = 'allow-scripts allow-same-origin';
46+
iframe.classList.add('markup-render', 'gt-invisible');
5047
iframe.srcdoc = `<html><head><style>${iframeCss}</style></head><body>${svg}</body></html>`;
51-
iframe.addEventListener('load', () => {
52-
const height = iframe.contentWindow.document.body.clientHeight;
53-
iframe.style.height = `${height}px`;
54-
});
5548

5649
const mermaidBlock = document.createElement('div');
57-
mermaidBlock.classList.add('mermaid-block');
50+
mermaidBlock.classList.add('mermaid-block', 'is-loading', 'gt-hidden');
5851
mermaidBlock.append(iframe);
5952

6053
const btn = makeCodeCopyButton();
6154
btn.setAttribute('data-clipboard-text', source);
62-
6355
mermaidBlock.append(btn);
64-
pre.replaceWith(mermaidBlock);
56+
57+
iframe.addEventListener('load', () => {
58+
pre.replaceWith(mermaidBlock);
59+
mermaidBlock.classList.remove('gt-hidden');
60+
iframe.style.height = `${iframe.contentWindow.document.body.clientHeight}px`;
61+
setTimeout(() => { // avoid flash of iframe background
62+
mermaidBlock.classList.remove('is-loading');
63+
iframe.classList.remove('gt-invisible');
64+
}, 0);
65+
});
66+
67+
document.body.append(mermaidBlock);
6568
} catch (err) {
6669
displayError(pre, err);
6770
}

0 commit comments

Comments
 (0)