summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnthonyTornetta <cornchipgonecodin@gmail.com>2023-08-06 22:14:20 -0400
committerJan-Erik Rediger <janerik@fnordig.de>2024-01-05 14:23:46 +0200
commitca18754266fc1da8270bb5e7a9a9dcace3dfc944 (patch)
treea7f14f1fdb76590257d6943ed0186eee6dea38a9
parent590c1b054c15a5710302fc27d3a0359973479d50 (diff)
Add support for dark theme with mermaid graphs
-rw-r--r--src/bin/assets/mermaid-init.js36
1 files changed, 35 insertions, 1 deletions
diff --git a/src/bin/assets/mermaid-init.js b/src/bin/assets/mermaid-init.js
index 313a6e8..15a7f4e 100644
--- a/src/bin/assets/mermaid-init.js
+++ b/src/bin/assets/mermaid-init.js
@@ -1 +1,35 @@
-mermaid.initialize({startOnLoad:true});
+(() => {
+ const darkThemes = ['ayu', 'navy', 'coal'];
+ const lightThemes = ['light', 'rust'];
+
+ const classList = document.getElementsByTagName('html')[0].classList;
+
+ let lastThemeWasLight = true;
+ for (const cssClass of classList) {
+ if (darkThemes.includes(cssClass)) {
+ lastThemeWasLight = false;
+ break;
+ }
+ }
+
+ const theme = lastThemeWasLight ? 'default' : 'dark';
+ mermaid.initialize({ startOnLoad: true, theme });
+
+ // Simplest way to make mermaid re-render the diagrams in the new theme is via refreshing the page
+
+ for (const darkTheme of darkThemes) {
+ document.getElementById(darkTheme).addEventListener('click', () => {
+ if (lastThemeWasLight) {
+ window.location.reload();
+ }
+ });
+ }
+
+ for (const lightTheme of lightThemes) {
+ document.getElementById(lightTheme).addEventListener('click', () => {
+ if (!lastThemeWasLight) {
+ window.location.reload();
+ }
+ });
+ }
+})();