From draw.io it is possible to embed the diagram as an HTML, however, when trying to use the generated HTML in a react app, nothing appears on the screen. The following script is loaded into the HTML page as well: <script type="text/javascript" src=".min.js"></script>
Any idea why this isn't working?
If I put the exact same code and HTML in a file without React, everything works as expected.
JS
const mystyle = {
maxWidth: "100%",
border: "1px solid transparent"
}
ReactDOM.render(
<div className="mxgraph" style={mystyle} data-mxgraph="{"highlight":"#0000ff","nav":true,"resize":true,"toolbar":"zoom layers lightbox","edit":"_blank","xml":"<mxfile host=\"app.diagrams\" modified=\"2021-04-08T16:37:39.233Z\" agent=\"5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36\" etag=\"O3fNti_B0JlDfU6ko1tb\" version=\"14.5.8\" type=\"google\"><diagram id=\"3VQJ_haBxNLfYGOJBekl\" name=\"Page-1\">zZZdb5swFIZ/DZeRAqQ0vUwJ68c0dVJUTdoNcu0TbBUwsg8D9utngiEgWjW7ibiKec4by+exZXD8MKsfFCn4D8kgdbw1qx1/73ieG6y35qcljSXu+rYjiRKsY+szOIi/YIM9LQUDbVmHUMoURTGFVOY5UJwwopSsprGjTNkEFCSByTJacKAkhVnsl2DIO7q9GaUfQSQch/5sJSN92ALNCZPVCPmR44dKSuxGWR1C2tqbevn2SXVYmIIcL/nDHW1U9DtSm9fb77s4eno5Pq9WdpY/JC1twzQVjr8zbE80f5NEsdCQrgNsei1KljmDdmbX8e8rLhAOBaFttTInwTCOWWrL+h2QcvtwlDke7EQf9NAvCBRCPUK2pweQGaBqTMRWVxvr154wr3+uztvl9nvAR1sVWEbsCUmGqc8SzcB6/A+n3sypyIsSY4byZDZIzQru35QZJe0oqoGWKPJkV4inNrk3weUKd4MLhG+vKdyfCYdeaUwKEZf0S++vGkKiYcHaN97StG9m2lvZhFLQOk4IQkWaD8UPN4sRvzvFF+w92C7N+83Muyzx0gvm5RRd9g1zd8mVflXlwUx5oUCbDkF9afxZy/znkF6udfOSvJ5283j+8jnVRh+QfvQP</diagram></mxfile>"}"></div>,
document.getElementById('root')
);
HTML
<div id="root"></div>
From draw.io it is possible to embed the diagram as an HTML, however, when trying to use the generated HTML in a react app, nothing appears on the screen. The following script is loaded into the HTML page as well: <script type="text/javascript" src="https://viewer.diagrams/js/viewer-static.min.js"></script>
Any idea why this isn't working?
If I put the exact same code and HTML in a file without React, everything works as expected.
JS
const mystyle = {
maxWidth: "100%",
border: "1px solid transparent"
}
ReactDOM.render(
<div className="mxgraph" style={mystyle} data-mxgraph="{"highlight":"#0000ff","nav":true,"resize":true,"toolbar":"zoom layers lightbox","edit":"_blank","xml":"<mxfile host=\"app.diagrams\" modified=\"2021-04-08T16:37:39.233Z\" agent=\"5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36\" etag=\"O3fNti_B0JlDfU6ko1tb\" version=\"14.5.8\" type=\"google\"><diagram id=\"3VQJ_haBxNLfYGOJBekl\" name=\"Page-1\">zZZdb5swFIZ/DZeRAqQ0vUwJ68c0dVJUTdoNcu0TbBUwsg8D9utngiEgWjW7ibiKec4by+exZXD8MKsfFCn4D8kgdbw1qx1/73ieG6y35qcljSXu+rYjiRKsY+szOIi/YIM9LQUDbVmHUMoURTGFVOY5UJwwopSsprGjTNkEFCSByTJacKAkhVnsl2DIO7q9GaUfQSQch/5sJSN92ALNCZPVCPmR44dKSuxGWR1C2tqbevn2SXVYmIIcL/nDHW1U9DtSm9fb77s4eno5Pq9WdpY/JC1twzQVjr8zbE80f5NEsdCQrgNsei1KljmDdmbX8e8rLhAOBaFttTInwTCOWWrL+h2QcvtwlDke7EQf9NAvCBRCPUK2pweQGaBqTMRWVxvr154wr3+uztvl9nvAR1sVWEbsCUmGqc8SzcB6/A+n3sypyIsSY4byZDZIzQru35QZJe0oqoGWKPJkV4inNrk3weUKd4MLhG+vKdyfCYdeaUwKEZf0S++vGkKiYcHaN97StG9m2lvZhFLQOk4IQkWaD8UPN4sRvzvFF+w92C7N+83Muyzx0gvm5RRd9g1zd8mVflXlwUx5oUCbDkF9afxZy/znkF6udfOSvJ5283j+8jnVRh+QfvQP</diagram></mxfile>"}"></div>,
document.getElementById('root')
);
HTML
<div id="root"></div>
I was tasked with doing something similar, but with Vue JS
instead of React
. So with that caveat, I'm happy to share my experience.
Our goal was to embed a Drawio instance into a Vue ponent that could be used inside of a larger Vue application. To do this, we tried to get Drawio to spit out it's HTML that could then be encapsulated into a ponent. Unfortunetly, this didn't work for a number of reasons:
<script>
tag) inside Vue for security reasonsAfter many hours, we realized that without forking Drawio and significantly re-writing the library, this task was basically impossible.
So below was our non-ideal solution:
<iframe>
<iframe
><template>
<iframe ref="drawioIFrame" :src="iFrameSrc" />
</template>
<script lang="ts">
import { Component, Vue } from 'vue-property-decorator'
@Component
export default class DiagramFrame extends Vue {
get iFrameSrc(): string {
return `http://mydrawiourl.`
}
}
</script>
To municate between Vue and Drawio, we used window.postMessage. Getting a reference from the iframe, we can send a message like this:
drawIoWindow.postMessage(message, "*")
And within Drawio:
window.parent.postMessage({
message
}, "*");
To listen for message ing from the other side, we used the following:
Vue:
mounted() {
window.addEventListener('message', this.handleMessagesFromDrawIo)
}
beforeDestroy() {
window.removeEventListener('message', this.handleMessagesFromDrawIo)
}
Drawio:
mxEvent.addListener(window, 'message', mxUtils.bind(this, messageHandler));
Though non-ideal, this pattern has actually worked quite well for us.
you should use document.createElement("script"); inside a useEffect hook:
useEffect(() => {
// Load diagrams viewer script
const script = document.createElement("script");
script.src = "https://viewer.diagrams/js/viewer-static.min.js";
script.async = true;
document.body.appendChild(script);
}, []);
then add your diagram in the body:
<div
className="mxgraph"
style={{
maxWidth: "100%",
border: "1px solid transparent",
overflow: "hidden",
position: "relative",
width: "100%"
}}
data-mxgraph={YOUR_DIAGRAM_DATA}
/>