|
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | 15 | import { Controller } from '@hotwired/stimulus' |
| 16 | +import mermaid from 'mermaid' |
16 | 17 |
|
17 | 18 | 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 | + } |
18 | 56 | 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") |
19 | 68 | // If the browser has an anchor in the URL that may be inside the README then |
20 | 69 | // we should attempt to scroll it into view once the README is loaded. |
21 | 70 | const hash = window.location.hash |
22 | 71 | if (hash == '') return // No anchor on the URL so we do nothing. |
23 | 72 |
|
24 | 73 | const hashElement = this.element.querySelector(hash) |
25 | 74 | if (hashElement) hashElement.scrollIntoView() |
| 75 | + |
26 | 76 | } |
27 | 77 | } |
0 commit comments