1 // Docs: https://www.diagrams.net/doc/faq/embed-mode
2 import * as store from './store';
3 import {ConfirmDialog} from "../components";
5 type DrawioExportEventResponse = {
13 type DrawioSaveEventResponse = {
18 let iFrame: HTMLIFrameElement|null = null;
19 let lastApprovedOrigin: string;
20 let onInit: () => Promise<string>;
21 let onSave: (data: string) => Promise<any>;
22 const saveBackupKey = 'last-drawing-save';
24 function drawPostMessage(data: Record<any, any>): void {
25 iFrame?.contentWindow?.postMessage(JSON.stringify(data), lastApprovedOrigin);
28 function drawEventExport(message: DrawioExportEventResponse) {
29 store.set(saveBackupKey, message.data);
31 onSave(message.data).then(() => {
32 store.del(saveBackupKey);
37 function drawEventSave(message: DrawioSaveEventResponse) {
39 action: 'export', format: 'xmlpng', xml: message.xml, spin: 'Updating drawing',
43 function drawEventInit() {
45 onInit().then(xml => {
46 drawPostMessage({action: 'load', autosave: 1, xml});
50 function drawEventConfigure() {
53 window.$events.emitPublic(iFrame, 'editor-drawio::configure', {config});
54 drawPostMessage({action: 'configure', config});
58 function drawEventClose() {
59 // eslint-disable-next-line no-use-before-define
60 window.removeEventListener('message', drawReceive);
61 if (iFrame) document.body.removeChild(iFrame);
65 * Receive and handle a message event from the draw.io window.
67 function drawReceive(event: MessageEvent) {
68 if (!event.data || event.data.length < 1) return;
69 if (event.origin !== lastApprovedOrigin) return;
71 const message = JSON.parse(event.data);
72 if (message.event === 'init') {
74 } else if (message.event === 'exit') {
76 } else if (message.event === 'save') {
77 drawEventSave(message as DrawioSaveEventResponse);
78 } else if (message.event === 'export') {
79 drawEventExport(message as DrawioExportEventResponse);
80 } else if (message.event === 'configure') {
86 * Attempt to prompt and restore unsaved drawing content if existing.
87 * @returns {Promise<void>}
89 async function attemptRestoreIfExists() {
90 const backupVal = await store.get(saveBackupKey);
91 const dialogEl = document.getElementById('unsaved-drawing-dialog');
94 console.error('Missing expected unsaved-drawing dialog');
97 if (backupVal && dialogEl) {
98 const dialog = window.$components.firstOnElement(dialogEl, 'confirm-dialog') as ConfirmDialog;
99 const restore = await dialog.show();
101 onInit = async () => backupVal;
107 * Show the draw.io editor.
108 * onSaveCallback must return a promise that resolves on successful save and errors on failure.
109 * onInitCallback must return a promise with the xml to load for the editor.
110 * Will attempt to provide an option to restore unsaved changes if found to exist.
111 * onSaveCallback Is called with the drawing data on save.
113 export async function show(drawioUrl: string, onInitCallback: () => Promise<string>, onSaveCallback: (data: string) => Promise<void>): Promise<void> {
114 onInit = onInitCallback;
115 onSave = onSaveCallback;
117 await attemptRestoreIfExists();
119 iFrame = document.createElement('iframe');
120 iFrame.setAttribute('frameborder', '0');
121 window.addEventListener('message', drawReceive);
122 iFrame.setAttribute('src', drawioUrl);
123 iFrame.setAttribute('class', 'fullscreen');
124 iFrame.style.backgroundColor = '#FFFFFF';
125 document.body.appendChild(iFrame);
126 lastApprovedOrigin = (new URL(drawioUrl)).origin;
129 export async function upload(imageData: string, pageUploadedToId: string): Promise<{}|string> {
132 uploaded_to: pageUploadedToId,
134 const resp = await window.$http.post(window.baseUrl('/images/drawio'), data);
138 export function close() {
143 * Load an existing image, by fetching it as Base64 from the system.
145 export async function load(drawingId: string): Promise<string> {
147 const resp = await window.$http.get(window.baseUrl(`/images/drawio/base64/${drawingId}`));
148 return `data:image/png;base64,${resp.data.content}`;
150 if (error instanceof window.$http.HttpError) {
151 window.$events.showResponseError(error);