Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mermaid charts convert to text on live reload until page refresh #197

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}`);
});
Loading