]> BookStack Code Mirror - bookstack/blob - resources/js/services/drawio.js
Fix Crowdin name in the language_request issue template
[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     } else if (message.event === 'configure') {
47         drawEventConfigure();
48     }
49 }
50
51 function drawEventExport(message) {
52     if (onSave) {
53         onSave(message.data);
54     }
55 }
56
57 function drawEventSave(message) {
58     drawPostMessage({action: 'export', format: 'xmlpng', xml: message.xml, spin: 'Updating drawing'});
59 }
60
61 function drawEventInit() {
62     if (!onInit) return;
63     onInit().then(xml => {
64         drawPostMessage({action: 'load', autosave: 1, xml: xml});
65     });
66 }
67
68 function drawEventConfigure() {
69     const config = {};
70     window.$events.emitPublic(iFrame, 'editor-drawio::configure', {config});
71     drawPostMessage({action: 'configure', config});
72 }
73
74 function drawEventClose() {
75     window.removeEventListener('message', drawReceive);
76     if (iFrame) document.body.removeChild(iFrame);
77 }
78
79 function drawPostMessage(data) {
80     iFrame.contentWindow.postMessage(JSON.stringify(data), lastApprovedOrigin);
81 }
82
83 async function upload(imageData, pageUploadedToId) {
84     let data = {
85         image: imageData,
86         uploaded_to: pageUploadedToId,
87     };
88     const resp = await window.$http.post(window.baseUrl(`/images/drawio`), data);
89     return resp.data;
90 }
91
92 /**
93  * Load an existing image, by fetching it as Base64 from the system.
94  * @param drawingId
95  * @returns {Promise<string>}
96  */
97 async function load(drawingId) {
98     const resp = await window.$http.get(window.baseUrl(`/images/drawio/base64/${drawingId}`));
99     return `data:image/png;base64,${resp.data.content}`;
100 }
101
102 export default {show, close, upload, load};
Morty Proxy This is a proxified and sanitized view of the page, visit original site.