2018-01-20 11:32:13 -05:00
|
|
|
let iFrame = null;
|
2021-05-24 19:05:20 -04:00
|
|
|
let lastApprovedOrigin;
|
2018-01-20 11:32:13 -05:00
|
|
|
let onInit, onSave;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the draw.io editor.
|
2020-04-05 12:27:16 -04:00
|
|
|
* @param {String} drawioUrl
|
|
|
|
* @param {Function} onInitCallback - Must return a promise with the xml to load for the editor.
|
|
|
|
* @param {Function} onSaveCallback - Is called with the drawing data on save.
|
2018-01-20 11:32:13 -05:00
|
|
|
*/
|
2020-04-05 12:27:16 -04:00
|
|
|
function show(drawioUrl, onInitCallback, onSaveCallback) {
|
2018-01-20 11:32:13 -05:00
|
|
|
onInit = onInitCallback;
|
|
|
|
onSave = onSaveCallback;
|
|
|
|
|
|
|
|
iFrame = document.createElement('iframe');
|
|
|
|
iFrame.setAttribute('frameborder', '0');
|
|
|
|
window.addEventListener('message', drawReceive);
|
2020-04-05 12:27:16 -04:00
|
|
|
iFrame.setAttribute('src', drawioUrl);
|
2018-01-20 11:32:13 -05:00
|
|
|
iFrame.setAttribute('class', 'fullscreen');
|
|
|
|
iFrame.style.backgroundColor = '#FFFFFF';
|
2018-01-20 15:40:21 -05:00
|
|
|
document.body.appendChild(iFrame);
|
2021-05-24 19:05:20 -04:00
|
|
|
lastApprovedOrigin = (new URL(drawioUrl)).origin;
|
2018-01-20 11:32:13 -05:00
|
|
|
}
|
|
|
|
|
2018-01-20 15:40:21 -05:00
|
|
|
function close() {
|
2018-01-20 11:32:13 -05:00
|
|
|
drawEventClose();
|
|
|
|
}
|
|
|
|
|
2021-05-24 19:05:20 -04:00
|
|
|
/**
|
|
|
|
* Receive and handle a message event from the draw.io window.
|
|
|
|
* @param {MessageEvent} event
|
|
|
|
*/
|
2018-01-20 11:32:13 -05:00
|
|
|
function drawReceive(event) {
|
|
|
|
if (!event.data || event.data.length < 1) return;
|
2021-05-24 19:05:20 -04:00
|
|
|
if (event.origin !== lastApprovedOrigin) return;
|
|
|
|
|
|
|
|
const message = JSON.parse(event.data);
|
2018-01-20 11:32:13 -05:00
|
|
|
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);
|
2022-04-20 18:32:02 -04:00
|
|
|
} else if (message.event === 'configure') {
|
|
|
|
drawEventConfigure();
|
2018-01-20 11:32:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function drawEventExport(message) {
|
|
|
|
if (onSave) {
|
|
|
|
onSave(message.data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function drawEventSave(message) {
|
|
|
|
drawPostMessage({action: 'export', format: 'xmlpng', xml: message.xml, spin: 'Updating drawing'});
|
|
|
|
}
|
|
|
|
|
|
|
|
function drawEventInit() {
|
|
|
|
if (!onInit) return;
|
|
|
|
onInit().then(xml => {
|
2018-01-20 15:40:21 -05:00
|
|
|
drawPostMessage({action: 'load', autosave: 1, xml: xml});
|
2018-01-20 11:32:13 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:32:02 -04:00
|
|
|
function drawEventConfigure() {
|
|
|
|
const config = {};
|
|
|
|
window.$events.emitPublic(iFrame, 'editor-drawio::configure', {config});
|
|
|
|
drawPostMessage({action: 'configure', config});
|
|
|
|
}
|
|
|
|
|
2018-01-20 11:32:13 -05:00
|
|
|
function drawEventClose() {
|
|
|
|
window.removeEventListener('message', drawReceive);
|
|
|
|
if (iFrame) document.body.removeChild(iFrame);
|
|
|
|
}
|
|
|
|
|
|
|
|
function drawPostMessage(data) {
|
2021-05-24 19:05:20 -04:00
|
|
|
iFrame.contentWindow.postMessage(JSON.stringify(data), lastApprovedOrigin);
|
2018-01-20 15:40:21 -05:00
|
|
|
}
|
|
|
|
|
2019-04-27 09:18:00 -04:00
|
|
|
async function upload(imageData, pageUploadedToId) {
|
|
|
|
let data = {
|
|
|
|
image: imageData,
|
|
|
|
uploaded_to: pageUploadedToId,
|
|
|
|
};
|
|
|
|
const resp = await window.$http.post(window.baseUrl(`/images/drawio`), data);
|
|
|
|
return resp.data;
|
|
|
|
}
|
|
|
|
|
2019-05-04 10:48:15 -04:00
|
|
|
/**
|
|
|
|
* Load an existing image, by fetching it as Base64 from the system.
|
|
|
|
* @param drawingId
|
|
|
|
* @returns {Promise<string>}
|
|
|
|
*/
|
|
|
|
async function load(drawingId) {
|
2023-01-26 07:16:23 -05: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 10:48:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export default {show, close, upload, load};
|