2023-04-20 17:25:48 +02:00
|
|
|
// Docs: https://www.diagrams.net/doc/faq/embed-mode
|
2023-08-22 20:30:39 +02:00
|
|
|
import * as store from './store';
|
2023-04-20 17:25:48 +02:00
|
|
|
|
2018-01-20 17:32:13 +01:00
|
|
|
let iFrame = null;
|
2021-05-25 01:05:20 +02:00
|
|
|
let lastApprovedOrigin;
|
2023-08-22 20:30:39 +02:00
|
|
|
let onInit;
|
|
|
|
let onSave;
|
|
|
|
const saveBackupKey = 'last-drawing-save';
|
2018-01-20 17:32:13 +01:00
|
|
|
|
2023-04-19 16:20:04 +02:00
|
|
|
function drawPostMessage(data) {
|
|
|
|
iFrame.contentWindow.postMessage(JSON.stringify(data), lastApprovedOrigin);
|
2018-01-20 17:32:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function drawEventExport(message) {
|
2023-08-22 20:30:39 +02:00
|
|
|
store.set(saveBackupKey, message.data);
|
2018-01-20 17:32:13 +01:00
|
|
|
if (onSave) {
|
2023-08-22 20:30:39 +02:00
|
|
|
onSave(message.data).then(() => {
|
|
|
|
store.del(saveBackupKey);
|
|
|
|
});
|
2018-01-20 17:32:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function drawEventSave(message) {
|
2023-04-18 23:20:02 +02:00
|
|
|
drawPostMessage({
|
|
|
|
action: 'export', format: 'xmlpng', xml: message.xml, spin: 'Updating drawing',
|
|
|
|
});
|
2018-01-20 17:32:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function drawEventInit() {
|
|
|
|
if (!onInit) return;
|
|
|
|
onInit().then(xml => {
|
2023-04-18 23:20:02 +02:00
|
|
|
drawPostMessage({action: 'load', autosave: 1, xml});
|
2018-01-20 17:32:13 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-21 00:32:02 +02:00
|
|
|
function drawEventConfigure() {
|
|
|
|
const config = {};
|
|
|
|
window.$events.emitPublic(iFrame, 'editor-drawio::configure', {config});
|
|
|
|
drawPostMessage({action: 'configure', config});
|
|
|
|
}
|
|
|
|
|
2018-01-20 17:32:13 +01:00
|
|
|
function drawEventClose() {
|
2023-04-19 16:20:04 +02:00
|
|
|
// eslint-disable-next-line no-use-before-define
|
2018-01-20 17:32:13 +01:00
|
|
|
window.removeEventListener('message', drawReceive);
|
|
|
|
if (iFrame) document.body.removeChild(iFrame);
|
|
|
|
}
|
|
|
|
|
2023-04-19 16:20:04 +02:00
|
|
|
/**
|
|
|
|
* Receive and handle a message event from the draw.io window.
|
|
|
|
* @param {MessageEvent} event
|
|
|
|
*/
|
|
|
|
function drawReceive(event) {
|
|
|
|
if (!event.data || event.data.length < 1) return;
|
|
|
|
if (event.origin !== lastApprovedOrigin) return;
|
|
|
|
|
|
|
|
const message = JSON.parse(event.data);
|
|
|
|
if (message.event === 'init') {
|
|
|
|
drawEventInit();
|
|
|
|
} else if (message.event === 'exit') {
|
|
|
|
drawEventClose();
|
|
|
|
} else if (message.event === 'save') {
|
|
|
|
drawEventSave(message);
|
|
|
|
} else if (message.event === 'export') {
|
|
|
|
drawEventExport(message);
|
|
|
|
} else if (message.event === 'configure') {
|
|
|
|
drawEventConfigure();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-23 15:16:20 +02:00
|
|
|
/**
|
|
|
|
* Attempt to prompt and restore unsaved drawing content if existing.
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
async function attemptRestoreIfExists() {
|
|
|
|
const backupVal = await store.get(saveBackupKey);
|
|
|
|
const dialogEl = document.getElementById('unsaved-drawing-dialog');
|
|
|
|
|
|
|
|
if (!dialogEl) {
|
|
|
|
console.error('Missing expected unsaved-drawing dialog');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (backupVal) {
|
|
|
|
/** @var {ConfirmDialog} */
|
|
|
|
const dialog = window.$components.firstOnElement(dialogEl, 'confirm-dialog');
|
|
|
|
const restore = await dialog.show();
|
|
|
|
if (restore) {
|
|
|
|
onInit = async () => backupVal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-19 16:20:04 +02:00
|
|
|
/**
|
|
|
|
* Show the draw.io editor.
|
2023-08-22 20:30:39 +02:00
|
|
|
* onSaveCallback must return a promise that resolves on successful save and errors on failure.
|
|
|
|
* onInitCallback must return a promise with the xml to load for the editor.
|
2023-08-23 15:16:20 +02:00
|
|
|
* Will attempt to provide an option to restore unsaved changes if found to exist.
|
2023-04-19 16:20:04 +02:00
|
|
|
* @param {String} drawioUrl
|
2023-08-22 20:30:39 +02:00
|
|
|
* @param {Function<Promise<String>>} onInitCallback
|
|
|
|
* @param {Function<Promise>} onSaveCallback - Is called with the drawing data on save.
|
2023-04-19 16:20:04 +02:00
|
|
|
*/
|
2023-08-23 15:16:20 +02:00
|
|
|
export async function show(drawioUrl, onInitCallback, onSaveCallback) {
|
2023-04-19 16:20:04 +02:00
|
|
|
onInit = onInitCallback;
|
|
|
|
onSave = onSaveCallback;
|
|
|
|
|
2023-08-23 15:16:20 +02:00
|
|
|
await attemptRestoreIfExists();
|
|
|
|
|
2023-04-19 16:20:04 +02:00
|
|
|
iFrame = document.createElement('iframe');
|
|
|
|
iFrame.setAttribute('frameborder', '0');
|
|
|
|
window.addEventListener('message', drawReceive);
|
|
|
|
iFrame.setAttribute('src', drawioUrl);
|
|
|
|
iFrame.setAttribute('class', 'fullscreen');
|
|
|
|
iFrame.style.backgroundColor = '#FFFFFF';
|
|
|
|
document.body.appendChild(iFrame);
|
|
|
|
lastApprovedOrigin = (new URL(drawioUrl)).origin;
|
2018-01-20 21:40:21 +01:00
|
|
|
}
|
|
|
|
|
2023-04-19 16:20:04 +02:00
|
|
|
export async function upload(imageData, pageUploadedToId) {
|
2023-04-18 23:20:02 +02:00
|
|
|
const data = {
|
2019-04-27 15:18:00 +02:00
|
|
|
image: imageData,
|
|
|
|
uploaded_to: pageUploadedToId,
|
|
|
|
};
|
2023-04-18 23:20:02 +02:00
|
|
|
const resp = await window.$http.post(window.baseUrl('/images/drawio'), data);
|
2019-04-27 15:18:00 +02:00
|
|
|
return resp.data;
|
|
|
|
}
|
|
|
|
|
2023-04-19 16:20:04 +02:00
|
|
|
export function close() {
|
|
|
|
drawEventClose();
|
|
|
|
}
|
|
|
|
|
2019-05-04 16:48:15 +02:00
|
|
|
/**
|
|
|
|
* Load an existing image, by fetching it as Base64 from the system.
|
|
|
|
* @param drawingId
|
|
|
|
* @returns {Promise<string>}
|
|
|
|
*/
|
2023-04-19 16:20:04 +02:00
|
|
|
export async function load(drawingId) {
|
2023-01-26 13:16:23 +01:00
|
|
|
try {
|
|
|
|
const resp = await window.$http.get(window.baseUrl(`/images/drawio/base64/${drawingId}`));
|
|
|
|
return `data:image/png;base64,${resp.data.content}`;
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof window.$http.HttpError) {
|
|
|
|
window.$events.showResponseError(error);
|
|
|
|
}
|
|
|
|
close();
|
|
|
|
throw error;
|
|
|
|
}
|
2019-05-04 16:48:15 +02:00
|
|
|
}
|