Skip to content

Commit bf92fad

Browse files
committed
fixing theme switching
1 parent af60b58 commit bf92fad

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

FrontEnd/scripts/controllers/readme_controller.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,65 @@
1313
// limitations under the License.
1414

1515
import { Controller } from '@hotwired/stimulus'
16+
import mermaid from 'mermaid'
1617

1718
export class ReadmeController extends Controller {
19+
notifyObservers(darkMode) {
20+
console.log("dark mode is", darkMode)
21+
if (darkMode == undefined) {
22+
darkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
23+
console.log("actually dark mode is", darkMode)
24+
}
25+
26+
// Get all mermaid diagrams
27+
const mermaidDivs = document.querySelectorAll('pre[lang="mermaid"]');
28+
29+
console.log("mermaidDivs", mermaidDivs.length)
30+
// Re-render each diagram
31+
for (const div of Array.from(mermaidDivs)) {
32+
const json = div.parentElement.parentElement.getAttribute('data-json');
33+
if (!json) {
34+
continue
35+
}
36+
const diagramContent = JSON.parse(json).data;
37+
if (diagramContent) {
38+
try {
39+
// Clear the existing diagram
40+
div.innerHTML = diagramContent;
41+
div.removeAttribute('data-processed')
42+
} catch (error) {
43+
console.error('Error re-rendering mermaid diagram:', error);
44+
}
45+
}
46+
}
47+
48+
mermaid.initialize({
49+
theme: darkMode ? 'dark' : undefined,
50+
'nodeSpacing': 50, 'rankSpacing': 50, 'curve': 'basis'
51+
})
52+
mermaid.run({
53+
querySelector: 'pre[lang="mermaid"]',
54+
});
55+
}
1856
navigateToAnchorFromLocation() {
57+
if (typeof window !== 'undefined') {
58+
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
59+
60+
// Add listener for system preference changes
61+
mediaQuery.addEventListener('change', (e) => {
62+
console.log("dark mode changed")
63+
this.notifyObservers(e.matches);
64+
});
65+
}
66+
this.notifyObservers();
67+
console.log("Mermaid initialized")
1968
// If the browser has an anchor in the URL that may be inside the README then
2069
// we should attempt to scroll it into view once the README is loaded.
2170
const hash = window.location.hash
2271
if (hash == '') return // No anchor on the URL so we do nothing.
2372

2473
const hashElement = this.element.querySelector(hash)
2574
if (hashElement) hashElement.scrollIntoView()
75+
2676
}
2777
}

0 commit comments

Comments
 (0)