-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmermaid-init.js
More file actions
49 lines (40 loc) · 1.47 KB
/
mermaid-init.js
File metadata and controls
49 lines (40 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
(() => {
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
const getThemeButton = (themeName) =>
document.getElementById(themeName) || document.getElementById(`mdbook-theme-${themeName}`);
const bindClick = (themeName, onClick) => {
const el = getThemeButton(themeName);
if (el) {
el.addEventListener('click', onClick);
}
};
for (const darkTheme of darkThemes) {
bindClick(darkTheme, () => {
if (lastThemeWasLight) {
window.location.reload();
}
});
}
for (const lightTheme of lightThemes) {
bindClick(lightTheme, () => {
if (!lastThemeWasLight) {
window.location.reload();
}
});
}
})();