2023-02-10 09:25:21 +00:00
|
|
|
// This is the initialization loading the CryptPad libraries
|
|
|
|
define([
|
|
|
|
'/common/sframe-app-framework.js',
|
|
|
|
'/customize/messages.js', // translation keys
|
2023-02-14 08:15:55 +00:00
|
|
|
'/bower_components/pako/dist/pako.min.js',
|
|
|
|
'/bower_components/js-base64/base64.js',
|
2023-02-10 09:25:21 +00:00
|
|
|
'less!/drawio/app.less',
|
|
|
|
'css!/drawio/drawio.css',
|
|
|
|
], function (
|
|
|
|
Framework,
|
|
|
|
Messages,
|
2023-02-14 08:15:55 +00:00
|
|
|
pako,
|
|
|
|
base64) {
|
2023-02-10 09:25:21 +00:00
|
|
|
|
2023-02-14 08:15:55 +00:00
|
|
|
console.log('XXX base64', base64);
|
2023-02-10 09:25:21 +00:00
|
|
|
|
|
|
|
// This is the main initialization loop
|
|
|
|
var onFrameworkReady = function (framework) {
|
|
|
|
const drawioFrame = document.querySelector('#cp-app-drawio-content');
|
2023-02-14 08:15:55 +00:00
|
|
|
let lastContent = '';
|
|
|
|
let drawIoInitalized = false;
|
2023-02-10 09:25:21 +00:00
|
|
|
|
|
|
|
const postMessageToDrawio = function(msg) {
|
2023-02-14 08:15:55 +00:00
|
|
|
if (!drawIoInitalized) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-10 09:25:21 +00:00
|
|
|
console.log('XXX postMessageToDrawio', msg);
|
|
|
|
drawioFrame.contentWindow.postMessage(JSON.stringify(msg), '*');
|
|
|
|
};
|
|
|
|
|
|
|
|
const onDrawioInit = function(data) {
|
2023-02-14 08:15:55 +00:00
|
|
|
drawIoInitalized = true;
|
|
|
|
|
2023-02-10 09:25:21 +00:00
|
|
|
postMessageToDrawio({
|
|
|
|
action: 'load',
|
|
|
|
xml: lastContent,
|
|
|
|
autosave: 1
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const onDrawioChange = function(newXml) {
|
2023-02-14 08:15:55 +00:00
|
|
|
newXml = decompressDrawioXml(newXml);
|
2023-02-10 09:25:21 +00:00
|
|
|
if (lastContent != newXml) {
|
|
|
|
lastContent = newXml;
|
|
|
|
framework.localChange();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const onDrawioAutodave = function(data) {
|
|
|
|
onDrawioChange(data.xml);
|
|
|
|
}
|
|
|
|
|
|
|
|
const drawioHandlers = {
|
|
|
|
init: onDrawioInit,
|
|
|
|
autosave: onDrawioAutodave,
|
|
|
|
};
|
|
|
|
|
|
|
|
// This is the function from which you will receive updates from CryptPad
|
|
|
|
framework.onContentUpdate(function (newContent) {
|
|
|
|
console.log("Content should be updated to " + newContent);
|
|
|
|
lastContent = newContent.content;
|
|
|
|
postMessageToDrawio({
|
|
|
|
action: 'merge',
|
|
|
|
xml: newContent.content,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// This is the function called to get the current state of the data in your app
|
|
|
|
framework.setContentGetter(function () {
|
|
|
|
console.log("Content current value is " + lastContent);
|
|
|
|
return {
|
|
|
|
content: lastContent
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
// This is called when the history is synced. "onContentUpdate" has already been called with the full content and the loading screen is being removed.
|
|
|
|
framework.onReady(function (newPad) {
|
|
|
|
// Here you can focus any editable part, check the integrity of the current data or intialize some values
|
|
|
|
});
|
|
|
|
|
|
|
|
// starting the CryptPad framework
|
|
|
|
framework.start();
|
|
|
|
|
2023-02-14 08:15:55 +00:00
|
|
|
drawioFrame.src = '/bower_components/drawio/src/main/webapp/index.html?'
|
|
|
|
+ new URLSearchParams({
|
|
|
|
// pages: 0,
|
|
|
|
dev: 1,
|
|
|
|
test: 1,
|
|
|
|
stealth: 1,
|
|
|
|
embed: 1,
|
|
|
|
drafts: 0,
|
|
|
|
noSaveBtn: 1,
|
|
|
|
modified: 'unsavedChanges',
|
|
|
|
proto: 'json',
|
|
|
|
});
|
2023-02-10 09:25:21 +00:00
|
|
|
|
|
|
|
window.addEventListener("message", (event) => {
|
|
|
|
if (event.source == drawioFrame.contentWindow) {
|
|
|
|
const data = JSON.parse(event.data);
|
|
|
|
console.log('XXX', data);
|
|
|
|
const eventType = data.event;
|
|
|
|
const handler = drawioHandlers[eventType];
|
|
|
|
if (handler) {
|
|
|
|
handler(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, false);
|
|
|
|
};
|
|
|
|
|
2023-02-14 08:15:55 +00:00
|
|
|
// As described here: https://drawio-app.com/extracting-the-xml-from-mxfiles/
|
|
|
|
let decompressDrawioXml = function(xmlDocStr) {
|
|
|
|
const TEXT_NODE = 3;
|
|
|
|
|
|
|
|
const parser = new DOMParser();
|
|
|
|
const doc = parser.parseFromString(xmlDocStr, "application/xml");
|
|
|
|
|
|
|
|
doc.firstChild.removeAttribute('modified');
|
|
|
|
doc.firstChild.removeAttribute('agent');
|
|
|
|
doc.firstChild.removeAttribute('etag');
|
|
|
|
|
|
|
|
const errorNode = doc.querySelector("parsererror");
|
|
|
|
if (errorNode) {
|
|
|
|
console.error("error while parsing", errorNode);
|
|
|
|
return xmlStr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const diagrams = doc.querySelectorAll('diagram');
|
|
|
|
|
|
|
|
diagrams.forEach((diagram) => {
|
|
|
|
if (diagram.firstChild && diagram.firstChild.nodeType == TEXT_NODE) {
|
|
|
|
const innerText = diagram.firstChild.nodeValue;
|
|
|
|
const bin = base64.toUint8Array(innerText);
|
|
|
|
const xmlUrlStr = pako.inflateRaw(bin, {to: 'string'});
|
|
|
|
const xmlStr = decodeURIComponent(xmlUrlStr);
|
|
|
|
const diagramDoc = parser.parseFromString(xmlStr, "application/xml");
|
|
|
|
diagram.replaceChild(diagramDoc.firstChild, diagram.firstChild);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return new XMLSerializer().serializeToString(doc);
|
|
|
|
}
|
|
|
|
|
2023-02-10 09:25:21 +00:00
|
|
|
// Framework initialization
|
|
|
|
Framework.create({
|
|
|
|
toolbarContainer: '#cme_toolbox',
|
|
|
|
contentContainer: '#cp-app-drawio-editor'
|
|
|
|
}, function (framework) {
|
|
|
|
onFrameworkReady(framework);
|
|
|
|
});
|
|
|
|
});
|