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 button
If 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:
- Draw.io Embed-Diagrams - presents diagrams as PNG files, can click-to-open them as a SVG
- Supports links, pages and layers but only via popup
- https://github.com/jgraph/drawio-html5
- Requires the 'host application' to provide the data to the tool, rather than the image data coming from a URL
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:
- Include a link to the stylesheet for this tools: https://laingsimon.github.io/render-diagram/drawio-renderer.css
- Include a link to the draw.io render script: https://www.draw.io/js/viewer.min.js
Once the page has finished loading - Look for elements with the
drawio-diagram
css-class - For each element, render the diagram based on whether it has a
data-diagram-data
ordata-diagram-url
attribute- Pass any settings in
drawio-diagram-options
(json blob) to draw.io
- Pass any settings in
Diagrams can only be embedded in one of the following element types:
div
span
section
body
- - Html
- - GitHub pages
- - GitHub flavoured markdown; script tags are modified in the content
- - Regular markdown
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 is less clear (although would become common practice) that the PNG contains diagram data
- An XML - or other identified file - would convey this meaning more effectively
- PNGs are non-interactive therefore the following functionality from draw.io isn't supported
- Links
- Pages
- Layers
It was desired that a tool should be able to:
- Render a diagram from a hosted file (so that becomes the only file requiring an edit if the diagram needs to be changed)
- Render a diagram in a familiar fashion - as similar to
img
tags as possible - Render a diagram in SVG format to preserve Link, Page and Layer support
- Render a diagram from any webpage and (github) markdown content
All but the last point has been covered, due to enforced limitations over the <script>
tag within markdown content.
- [All]: GitHub flavoured markdown; script tags are modified in the content
- [Relative content]: Diagram content must be present on the same origin/host/address, or CORS must be enabled on the source of the content