forked from drowl87/hextra_mirror
fix: rerender mermaid diagrams on theme change (#509)
Make mermaid render diagrams on manual toggle between themes, after initial page load
This commit is contained in:
parent
0dcf7e7a40
commit
bd34a5bad3
@ -37,9 +37,32 @@
|
|||||||
{{- $mermaidJS := resources.Get "lib/mermaid/mermaid.min.js" | fingerprint -}}
|
{{- $mermaidJS := resources.Get "lib/mermaid/mermaid.min.js" | fingerprint -}}
|
||||||
<script defer src="{{ $mermaidJS.RelPermalink }}" integrity="{{ $mermaidJS.Data.Integrity }}"></script>
|
<script defer src="{{ $mermaidJS.RelPermalink }}" integrity="{{ $mermaidJS.Data.Integrity }}"></script>
|
||||||
<script>
|
<script>
|
||||||
document.addEventListener("DOMContentLoaded", function () {
|
document.addEventListener("DOMContentLoaded", () => {
|
||||||
|
// Store original mermaid code for each diagram
|
||||||
|
document.querySelectorAll(".mermaid").forEach(el => {
|
||||||
|
el.dataset.original = el.innerHTML;
|
||||||
|
});
|
||||||
|
|
||||||
const theme = document.documentElement.classList.contains("dark") ? "dark" : "default";
|
const theme = document.documentElement.classList.contains("dark") ? "dark" : "default";
|
||||||
mermaid.initialize({ startOnLoad: true, theme: theme });
|
mermaid.initialize({ startOnLoad: true, theme: theme });
|
||||||
|
|
||||||
|
let timeout;
|
||||||
|
new MutationObserver(() => {
|
||||||
|
clearTimeout(timeout);
|
||||||
|
timeout = setTimeout(() => {
|
||||||
|
const theme = document.documentElement.classList.contains("dark") ? "dark" : "default";
|
||||||
|
document.querySelectorAll(".mermaid").forEach(el => {
|
||||||
|
// Reset to original content, preserving HTML
|
||||||
|
el.innerHTML = el.dataset.original;
|
||||||
|
el.removeAttribute("data-processed");
|
||||||
|
});
|
||||||
|
mermaid.initialize({ startOnLoad: true, theme: theme });
|
||||||
|
mermaid.init();
|
||||||
|
}, 150);
|
||||||
|
}).observe(document.documentElement, {
|
||||||
|
attributes: true,
|
||||||
|
attributeFilter: ["class"]
|
||||||
|
});
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user