]> BookStack Code Mirror - bookstack/blob - resources/js/services/drawio.js
Added test for logical-theme-system command registration
[bookstack] / resources / js / services / drawio.js
1 let iFrame = null;
2 let lastApprovedOrigin;
3 let onInit, onSave;
4
5 /**
6  * Show the draw.io editor.
7  * @param {String} drawioUrl
8  * @param {Function} onInitCallback - Must return a promise with the xml to load for the editor.
9  * @param {Function} onSaveCallback - Is called with the drawing data on save.
10  */
11 function show(drawioUrl, onInitCallback, onSaveCallback) {
12     onInit = onInitCallback;
13     onSave = onSaveCallback;
14
15     iFrame = document.createElement('iframe');
16     iFrame.setAttribute('frameborder', '0');
17     window.addEventListener('message', drawReceive);
18     iFrame.setAttribute('src', drawioUrl);
19     iFrame.setAttribute('class', 'fullscreen');
20     iFrame.style.backgroundColor = '#FFFFFF';
21     document.body.appendChild(iFrame);
22     lastApprovedOrigin = (new URL(drawioUrl)).origin;
23 }
24
25 function close() {
26     drawEventClose();
27 }
28
29 /**
30  * Receive and handle a message event from the draw.io window.
31  * @param {MessageEvent} event
32  */
33 function drawReceive(event) {
34     if (!event.data || event.data.length < 1) return;
35     if (event.origin !== lastApprovedOrigin) return;
36
37     const message = JSON.parse(event.data);
38     if (message.event === 'init') {
39         drawEventInit();
40     } else if (message.event === 'exit') {
41         drawEventClose();
42     } else if (message.event === 'save') {
43         drawEventSave(message);
44     } else if (message.event === 'export') {
45         drawEventExport(message);
46     }
47 }
48
49 function drawEventExport(message) {
50     if (onSave) {
51         onSave(message.data);
52     }
53 }
54
55 function drawEventSave(message) {
56     drawPostMessage({action: 'export', format: 'xmlpng', xml: message.xml, spin: 'Updating drawing'});
57 }
58
59 function drawEventInit() {
60     if (!onInit) return;
61     onInit().then(xml => {
62         drawPostMessage({action: 'load', autosave: 1, xml: xml});
63     });
64 }
65
66 function drawEventClose() {
67     window.removeEventListener('message', drawReceive);
68     if (iFrame) document.body.removeChild(iFrame);
69 }
70
71 function drawPostMessage(data) {
72     iFrame.contentWindow.postMessage(JSON.stringify(data), lastApprovedOrigin);
73 }
74
75 async function upload(imageData, pageUploadedToId) {
76     let data = {
77         image: imageData,
78         uploaded_to: pageUploadedToId,
79     };
80     const resp = await window.$http.post(window.baseUrl(`/images/drawio`), data);
81     return resp.data;
82 }
83
84 /**
85  * Load an existing image, by fetching it as Base64 from the system.
86  * @param drawingId
87  * @returns {Promise<string>}
88  */
89 async function load(drawingId) {
90     const resp = await window.$http.get(window.baseUrl(`/images/drawio/base64/${drawingId}`));
91     return `data:image/png;base64,${resp.data.content}`;
92 }
93
94 export default {show, close, upload, load};
Morty Proxy This is a proxified and sanitized view of the page, visit original site.