Render draw.io diagrams from a URL or embedded into a webpage
Render diagrams from draw.io from a URL or embedded into a webpage, whilst maintaining interactive support in the diagram, e.g. links, layers and pages.
Include this script anywhere:
<script src="https://laingsimon.github.io/render-diagram/drawio-renderer.js"></script>
Then include diagrams using any of the below approaches:
<div class="drawio-diagram" data-diagram-data="diagram-content"></div>
This is an analogue of <img src="data:image/png;base64,xxxx"/>
and is equivalent to embedding the diagram based on the embed option in draw.io.
<div class="drawio-diagram" data-diagram-url="url"></div>
This is an analogue of <img src="url"/>
.
<div class="drawio-diagram" data-diagram-options='{options}'></div>
NOTE: Make sure the options use double quotes to encapsulate property names, e.g. “toolbar”. One of the easiest ways to achieve this is to use single quotes to encapsulate the json blob, as shown above.
Any option that is valid for the draw.io renderer is valid here, some notable examples are:
option | data type | purpose | default | example |
---|---|---|---|---|
highlight |
colour-string | The colour to highlight links with | none |
blue |
target |
string | The window name to open links in | - | _blank |
lightbox |
boolean | Whether to enable the lightbox view of the diagram | false |
true |
nav |
boolean | Whether to permit navigation from links | true |
true |
toolbar |
space-delimited-string | Which buttons to show in the toolbar, see below | - | zoom layers |
Some of the known toolbar options are:
zoom
- Whether to show the zoom buttonslayers
- Whether to show the layers buttonpages
- Whether to show the pages buttonIf this option is missing or empty then the toolbar will be hidden.
See other examples and test cases here
Open draw.io with drawing content by providing a URL. This is possible with draw.io, but only where the URL is publicly accessible. This facility loads the content in the browser (rather than server side) therefore can be hosted privately by the user/organisation.
Security: The diagram data is accessed in the same manner as if the diagram was downloaded by the user from the given url, the diagram data is not sent/retrieved by any other service.
There are some other tools available that permit similar behaviour, namely:
The following link format can be used to open diagrams in draw.io for editing. The url must be accessible publicly so draw.io can access the drawing.
https://www.draw.io/#Uhttps://laingsimon.github.io/render-diagram/Sample%20file.xml
The above script will perform the following steps:
drawio-diagram
css-classdata-diagram-data
or data-diagram-url
attribute
drawio-diagram-options
(json blob) to draw.ioDiagrams can only be embedded in one of the following element types:
div
span
section
body
Diagrams can be easily embedded within a page by using tooling within Draw.io. This works without any issue, however requires the data about the diagram to be embedded in other content. Diagrams drawn from a file (as produced by saving the diagram to a file) isn’t natively supported.
This means that you should save the diagram as a PNG with the data embedded. Once again this works fine, and achieves the expected behaviour as part of this tool - but with the following drawbacks
It was desired that a tool should be able to:
img
tags as possibleAll but the last point has been covered, due to enforced limitations over the <script>
tag within markdown content.