Skip to content

Commit cd1906a

Browse files
Use the Mermaid API directly to fix nested diagram inside ui.dialog (#4692)
This PR fix #4518 magically by invoking the Mermaid API exactly as prescribed here: https://mermaid.js.org/config/usage.html#api-usage I think the commit wraps it up very well: > Dear mermaid: we'll call API ourselves thankyouverymuch Their code: ```js element = document.querySelector('#graphDiv'); const graphDefinition = 'graph TB\na-->b'; const { svg, bindFunctions } = await mermaid.render('graphDiv', graphDefinition); element.innerHTML = svg; // This can also be written as `bindFunctions?.(element);` using the `?` shorthand. if (bindFunctions) { bindFunctions(element); } ``` My code: ```js let element = queue.shift(); // same mindset let innertext = this.content; // mine's from Vue const { svg, bindFunctions } = await mermaid.render('graphDiv', innertext); // literally the same except variable names element.innerHTML = svg; // literally the same if (bindFunctions) { bindFunctions(element); // literally the same } ``` If I had to guess, the reason it was fixed, is because '#graphDiv' does not exist, and so Mermaid just "assumes full size" and renders the proper diagram without being pinched to absolutely tiny. Note that there are a couple of shortcomings which I haven't tested / haven't fixed, so don't merge just yet. - [x] what happens when we change the content? Does the mermaid diagram update appropriately? - [x] As of how, if there is syntax error, we lose the good-ol "Mermaid Bomb" error screen. Should we have better means? - [x] Are the Mermaid config respected (`{'securityLevel': 'loose', ...}` - allow running JavaScript when a node is clicked; `{'logLevel': 'info', ...}` - log debug info to the console) respected under this approach? - [x] Does this PR impact existing code? If so, how much? - [x] Passing 'graphDiv' when '#graphDiv' doesn't exist seems kinda sketchy. Are we absolutely sure that's the way to go? --- ![image](https://github.com/user-attachments/assets/f9eb99ef-5072-4af8-bc32-5b6ea1f09497) Perfect diagram, despite disable cache is false / disable cache is on and no network throttle. --------- Co-authored-by: Falko Schindler <falko@zauberzeug.com>
1 parent 291d291 commit cd1906a

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

nicegui/elements/mermaid.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import mermaid from "mermaid";
22

3-
let is_running = false;
4-
const queue = [];
5-
63
export default {
74
template: `<div></div>`,
85
data: () => ({
@@ -24,20 +21,18 @@ export default {
2421
async update(content) {
2522
if (this.last_content === content) return;
2623
this.last_content = content;
27-
this.$el.innerHTML = content;
28-
this.$el.removeAttribute("data-processed");
29-
queue.push(this.$el);
30-
if (is_running) return;
31-
is_running = true;
32-
while (queue.length) {
33-
try {
34-
await mermaid.run({ nodes: [queue.shift()] });
35-
} catch (error) {
36-
console.error(error);
37-
this.$emit("error", error);
38-
}
24+
try {
25+
const { svg, bindFunctions } = await mermaid.render(this.$el.id + "_mermaid", content);
26+
this.$el.innerHTML = svg;
27+
bindFunctions?.(this.$el);
28+
} catch (error) {
29+
const { svg, bindFunctions } = await mermaid.render(this.$el.id + "_mermaid", "error");
30+
this.$el.innerHTML = svg;
31+
bindFunctions?.(this.$el);
32+
const mermaidErrorFormat = { str: error.message, message: error.message, hash: error.name, error };
33+
console.error(mermaidErrorFormat);
34+
this.$emit("error", mermaidErrorFormat);
3935
}
40-
is_running = false;
4136
},
4237
},
4338
props: {

0 commit comments

Comments
 (0)