Skip to content

Commit

Permalink
Merge pull request #197 from jannis-baum/issue/194-mermaid-charts-con…
Browse files Browse the repository at this point in the history
…vert-to-text-on-live-reload-until-page-refresh

Mermaid charts convert to text on live reload until page refresh
  • Loading branch information
jannis-baum authored Jan 21, 2025
2 parents 355ebb5 + 7d35b0d commit 040995c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 59 deletions.
19 changes: 2 additions & 17 deletions src/routes/viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,13 @@ router.get(/.*/, async (req: Request, res: Response) => {
${body}
</div>
</body>
<script>
window.VIV_PORT = "${config.port}";
window.VIV_PATH = "${urlToPath(req.path)}";
</script>
${config.scripts ? `<script type="text/javascript">${config.scripts}</script>` : ''}
<script type="module">
import mermaid from '/static/mermaid/mermaid.esm.min.mjs';
const darkModePreference = window.matchMedia("(prefers-color-scheme: dark)");
mermaid.initialize({ startOnLoad: true, theme: darkModePreference.matches ? 'dark' : 'default' })
function updateTheme() {
if (document.getElementsByClassName('mermaid').length > 0) {
window.location.reload()
}
}
darkModePreference.addEventListener("change", () => updateTheme());
// deprecated method for backward compatibility
darkModePreference.addEventListener(() => updateTheme());
</script>
<script type="text/javascript" src="/static/client.js"></script>
<script type="module" src="/static/client.mjs"></script>
</html>
`);
});
Expand Down
42 changes: 0 additions & 42 deletions static/client.js

This file was deleted.

67 changes: 67 additions & 0 deletions static/client.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* --------------------------------------------------------------------------
* MERMAID ------------------------------------------------------------------ */

import mermaid from '/static/mermaid/mermaid.esm.min.mjs';

const darkModePreference = window.matchMedia('(prefers-color-scheme: dark)');
mermaid.initialize({ startOnLoad: true, theme: darkModePreference.matches ? 'dark' : 'default' });

function updateTheme() {
if (document.getElementsByClassName('mermaid').length > 0) {
window.location.reload();
}
}
try {
darkModePreference.addEventListener('change', () => updateTheme());
} catch {
try {
// deprecated method for backward compatibility
darkModePreference.addEventListener(() => updateTheme());
} catch {}
}

/* --------------------------------------------------------------------------
* WEBSOCKET COMMUNICATION WITH SERVER -------------------------------------- */

const ws = new WebSocket(`ws://localhost:${window.VIV_PORT}`);

ws.addEventListener('message', async (event) => {
const fields = event.data.toString().split(': ');
if (fields.length < 2) return;
const [key, ...values] = fields;
const value = values.join(': ');

switch (key) {
case 'UPDATE':
document.getElementById('body-content').innerHTML = value;
await mermaid.run({ querySelector: '.mermaid' });
break;

case 'SCROLL':
let line = parseInt(value);
while (line) {
const targets = document.querySelectorAll(`[data-source-line="${line - 1}"]`);
if (targets.length) {
targets[0].scrollIntoView({
behavior: 'smooth',
block: 'nearest',
});
break;
}
line -= 1;
}
break;

case 'RELOAD':
window.location.reload();
break;

case 'PRINT':
console.log(value);
break;
}
});

ws.addEventListener('open', () => {
ws.send(`PATH: ${window.VIV_PATH}`);
});

0 comments on commit 040995c

Please sign in to comment.