Skip to content

Commit 5ec2775

Browse files
leogdiondaveverwer
authored andcommitted
adding code comments
1 parent 33aa795 commit 5ec2775

File tree

1 file changed

+40
-36
lines changed

1 file changed

+40
-36
lines changed

FrontEnd/scripts/controllers/readme_controller.js

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,62 +16,66 @@ import { Controller } from '@hotwired/stimulus'
1616
import mermaid from 'mermaid'
1717

1818
export class ReadmeController extends Controller {
19-
notifyObservers(darkMode) {
20-
console.log("dark mode is", darkMode)
19+
onThemeChange(darkMode) {
20+
// If `darkMode` isn't passed, get the current value
2121
if (darkMode == undefined) {
2222
darkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
23-
console.log("actually dark mode is", darkMode)
23+
console.log('actually dark mode is', darkMode)
2424
}
25-
25+
2626
// Get all mermaid diagrams
27-
const mermaidDivs = document.querySelectorAll('pre[lang="mermaid"]');
27+
const mermaidDivs = document.querySelectorAll('pre[lang="mermaid"]')
2828

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);
29+
for (const div of Array.from(mermaidDivs)) {
30+
// Get the diagram code
31+
const json = div.parentElement.parentElement.getAttribute('data-json')
32+
if (!json) {
33+
continue
34+
}
35+
const diagramCode = JSON.parse(json).data
36+
if (diagramCode) {
37+
try {
38+
// Clear the existing diagram
39+
div.innerHTML = diagramCode
40+
div.removeAttribute('data-processed')
41+
} catch (error) {
42+
console.error('Error re-rendering mermaid diagram:', error)
43+
}
44+
}
4445
}
45-
}
46-
}
4746

48-
mermaid.initialize({
49-
theme: darkMode ? 'dark' : undefined,
50-
'nodeSpacing': 50, 'rankSpacing': 50, 'curve': 'basis'
51-
})
47+
mermaid.initialize({
48+
theme: darkMode ? 'dark' : undefined,
49+
nodeSpacing: 50,
50+
rankSpacing: 50,
51+
curve: 'basis',
52+
})
53+
54+
// Rather then preprocess the HTML in Swift we just change the selector to use `lang` attribute instead of `class`
5255
mermaid.run({
5356
querySelector: 'pre[lang="mermaid"]',
54-
});
57+
})
5558
}
5659
navigateToAnchorFromLocation() {
60+
// listen for changes to color scheme
5761
if (typeof window !== 'undefined') {
58-
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
59-
62+
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)')
63+
6064
// Add listener for system preference changes
6165
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")
66+
this.onThemeChange(e.matches)
67+
})
68+
}
69+
70+
// setup mermaid diagrams
71+
this.onThemeChange()
72+
6873
// If the browser has an anchor in the URL that may be inside the README then
6974
// we should attempt to scroll it into view once the README is loaded.
7075
const hash = window.location.hash
7176
if (hash == '') return // No anchor on the URL so we do nothing.
7277

7378
const hashElement = this.element.querySelector(hash)
7479
if (hashElement) hashElement.scrollIntoView()
75-
7680
}
7781
}

0 commit comments

Comments
 (0)