react-drawio icon indicating copy to clipboard operation
react-drawio copied to clipboard

Programatically create a diagram with Mermaid

Open aounleonardo opened this issue 1 year ago • 2 comments

I'm trying to programmatically load a diagram written in Mermaid (say graph TD;\n A-->B;\n).

Manually, this would be done by clicking on "Arrange" then hovering over "Insert" then "Advanced" and then clicking on "Mermaid...". Is there something you could expose that would allow me to call these buttons or call the underlying code that DrawIO uses?

I dived into and and this is the line where the diagram gets generated, it's inside this function called generateMermaidImage and I even found where exactly it happens in the mermaid.min.js: search for "try{await j.renderer.draw(s,i,Aje,j)}".

However, I can't figure out how can I call this from outside of the <DrawIoEmbed/> component

Screenshot 2024-09-13 at 18 15 59

aounleonardo avatar Sep 13 '24 17:09 aounleonardo

I also did some investigation but unfortunately I can't find a way to do this programatically. Everything is rendered in an iframe so we can't access that, and I can't find any documentation on how to handle a mermaid format in the embedded version. If you manage to transform the mermaid code into drawio supported svg/xml, I'm happy to integrate the code into the component for easier use!

marcveens avatar Sep 13 '24 20:09 marcveens

Thanks @marcveens for your quick answer! I'll try to either:

  1. convert Mermaid to svg/xml
  2. See if drawio could expose the "Insert Mermaid code" in a way that would allow you to expose that to us

Cheers 😄

aounleonardo avatar Sep 16 '24 19:09 aounleonardo