Add mini-live editing to example.html

This commit is contained in:
Sidharth Vinod
2023-09-18 23:29:27 +05:30
parent 136f1c50e7
commit 0ff3ba30b7

View File

@@ -3,10 +3,23 @@
<html> <html>
<head> <head>
<title>Mermaid development page</title> <title>Mermaid development page</title>
<style>
.container {
display: flex;
flex-direction: row;
}
#code {
max-width: 30vw;
width: 30vw;
}
#dynamicDiagram {
flex: 1;
}
</style>
</head> </head>
<body> <body>
<pre class="mermaid">info</pre>
<pre id="diagram" class="mermaid"> <pre id="diagram" class="mermaid">
graph TB graph TB
a --> b a --> b
@@ -15,22 +28,35 @@ graph TB
c --> d c --> d
</pre> </pre>
<div id="dynamicDiagram"></div> <hr />
Type code to view diagram:
<div class="container">
<textarea name="code" id="code" cols="30" rows="10"></textarea>
<div id="dynamicDiagram"></div>
</div>
<pre class="mermaid">info</pre>
<script type="module"> <script type="module">
import mermaid from '/mermaid.esm.mjs'; import mermaid from '/mermaid.esm.mjs';
mermaid.parseError = function (err, hash) { async function render(str) {
console.error('Mermaid error: ', err); const { svg } = await mermaid.render('dynamic', str);
}; document.getElementById('dynamicDiagram').innerHTML = svg;
}
const storeKey = window.location.pathname;
const code = localStorage.getItem(storeKey);
if (code) {
document.getElementById('code').value = code;
await render(code);
}
mermaid.initialize({ mermaid.initialize({
startOnLoad: true, startOnLoad: true,
logLevel: 0, logLevel: 0,
}); });
const value = `graph TD\nHello --> World`; document.getElementById('code').addEventListener('input', async (e) => {
const el = document.getElementById('dynamicDiagram'); const value = e.target.value;
const { svg } = await mermaid.render('dd', value); localStorage.setItem(storeKey, value);
console.log(svg); await render(value);
el.innerHTML = svg; });
</script> </script>
<script src="/dev/reload.js"></script> <script src="/dev/reload.js"></script>