1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
import _ from 'lodash';
import { createAlert, VARIANT_SUCCESS } from '~/alert';
import { darkModeEnabled } from '~/lib/utils/color_utils';
import { __ } from '~/locale';
import { setAttributes } from '~/lib/utils/dom_utils';
import {
DRAWIO_PARAMS,
DARK_BACKGROUND_COLOR,
DRAWIO_FRAME_ID,
DIAGRAM_BACKGROUND_COLOR,
DRAWIO_IFRAME_TIMEOUT,
DIAGRAM_MAX_SIZE,
} from './constants';
function updateDrawioEditorState(drawIOEditorState, data) {
Object.assign(drawIOEditorState, data);
}
function postMessageToDrawioEditor(drawIOEditorState, message) {
const { origin } = new URL(drawIOEditorState.drawioUrl);
drawIOEditorState.iframe.contentWindow.postMessage(JSON.stringify(message), origin);
}
function disposeDrawioEditor(drawIOEditorState) {
drawIOEditorState.disposeEventListener();
drawIOEditorState.iframe.remove();
}
function getSvg(data) {
const svgPath = atob(data.substring(data.indexOf(',') + 1));
return `<?xml version="1.0" encoding="UTF-8"?>\n\
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n\
${svgPath}`;
}
async function saveDiagram(drawIOEditorState, editorFacade) {
const { newDiagram, diagramMarkdown, filename, diagramSvg } = drawIOEditorState;
const filenameWithExt = filename.endsWith('.drawio.svg') ? filename : `${filename}.drawio.svg`;
postMessageToDrawioEditor(drawIOEditorState, {
action: 'spinner',
show: true,
messageKey: 'saving',
});
try {
const uploadResults = await editorFacade.uploadDiagram({
filename: filenameWithExt,
diagramSvg,
});
if (newDiagram) {
editorFacade.insertDiagram({ uploadResults });
} else {
editorFacade.updateDiagram({ diagramMarkdown, uploadResults });
}
createAlert({
message: __('Diagram saved successfully.'),
variant: VARIANT_SUCCESS,
fadeTransition: true,
});
setTimeout(() => disposeDrawioEditor(drawIOEditorState), 10);
} catch {
postMessageToDrawioEditor(drawIOEditorState, { action: 'spinner', show: false });
postMessageToDrawioEditor(drawIOEditorState, {
action: 'dialog',
titleKey: 'error',
modified: true,
buttonKey: 'close',
messageKey: 'errorSavingFile',
});
}
}
function promptName(drawIOEditorState, name, errKey) {
postMessageToDrawioEditor(drawIOEditorState, {
action: 'prompt',
titleKey: 'filename',
okKey: 'save',
defaultValue: name || '',
});
if (errKey !== null) {
postMessageToDrawioEditor(drawIOEditorState, {
action: 'dialog',
titleKey: 'error',
messageKey: errKey,
buttonKey: 'ok',
});
}
}
function sendLoadDiagramMessage(drawIOEditorState) {
postMessageToDrawioEditor(drawIOEditorState, {
action: 'load',
xml: drawIOEditorState.diagramSvg,
border: 8,
background: DIAGRAM_BACKGROUND_COLOR,
dark: drawIOEditorState.dark,
title: drawIOEditorState.filename,
});
}
async function loadExistingDiagram(drawIOEditorState, editorFacade) {
let diagram = null;
try {
diagram = await editorFacade.getDiagram();
} catch (e) {
throw new Error(__('Cannot load the diagram into the diagrams.net editor'));
}
if (diagram) {
const { diagramMarkdown, filename, diagramSvg, contentType, diagramURL } = diagram;
const resolvedURL = new URL(diagramURL, window.location.origin);
const diagramSvgSize = new Blob([diagramSvg]).size;
if (contentType !== 'image/svg+xml') {
throw new Error(__('The selected image is not a valid SVG diagram'));
}
if (resolvedURL.origin !== window.location.origin) {
throw new Error(__('The selected image is not an asset uploaded in the application'));
}
if (diagramSvgSize > DIAGRAM_MAX_SIZE) {
throw new Error(__('The selected image is too large.'));
}
updateDrawioEditorState(drawIOEditorState, {
newDiagram: false,
filename,
diagramMarkdown,
diagramSvg,
});
} else {
updateDrawioEditorState(drawIOEditorState, {
newDiagram: true,
});
}
sendLoadDiagramMessage(drawIOEditorState);
}
async function prepareEditor(drawIOEditorState, editorFacade) {
const { iframe } = drawIOEditorState;
iframe.style.cursor = 'wait';
try {
await loadExistingDiagram(drawIOEditorState, editorFacade);
iframe.style.visibility = 'visible';
iframe.style.cursor = '';
window.scrollTo(0, 0);
} catch (e) {
createAlert({
message: e.message,
error: e,
});
disposeDrawioEditor(drawIOEditorState);
}
}
function configureDrawIOEditor(drawIOEditorState) {
postMessageToDrawioEditor(drawIOEditorState, {
action: 'configure',
config: {
darkColor: DARK_BACKGROUND_COLOR,
settingsName: 'gitlab',
},
colorSchemeMeta: drawIOEditorState.dark, // For transparent iframe background in dark mode
});
updateDrawioEditorState(drawIOEditorState, {
initialized: true,
});
}
function onDrawIOEditorMessage(drawIOEditorState, editorFacade, evt) {
if (_.isNil(evt) || evt.source !== drawIOEditorState.iframe.contentWindow) {
return;
}
const msg = JSON.parse(evt.data);
if (msg.event === 'configure') {
configureDrawIOEditor(drawIOEditorState);
} else if (msg.event === 'init') {
prepareEditor(drawIOEditorState, editorFacade);
} else if (msg.event === 'exit') {
disposeDrawioEditor(drawIOEditorState);
} else if (msg.event === 'prompt') {
updateDrawioEditorState(drawIOEditorState, {
filename: msg.value,
});
if (!drawIOEditorState.filename) {
promptName(drawIOEditorState, 'diagram.drawio.svg', 'filenameShort');
} else {
saveDiagram(drawIOEditorState, editorFacade);
}
} else if (msg.event === 'export') {
updateDrawioEditorState(drawIOEditorState, {
diagramSvg: getSvg(msg.data),
});
// TODO Add this to draw.io editor configuration
sendLoadDiagramMessage(drawIOEditorState); // Save removes diagram from the editor, so we need to reload it.
postMessageToDrawioEditor(drawIOEditorState, { action: 'status', modified: true }); // And set editor modified flag to true.
if (!drawIOEditorState.filename) {
promptName(drawIOEditorState, 'diagram.drawio.svg', null);
} else {
saveDiagram(drawIOEditorState, editorFacade);
}
}
}
function createEditorIFrame(drawIOEditorState) {
const iframe = document.createElement('iframe');
setAttributes(iframe, {
id: DRAWIO_FRAME_ID,
src: drawIOEditorState.drawioUrl,
class: 'drawio-editor',
});
document.body.appendChild(iframe);
setTimeout(() => {
if (drawIOEditorState.initialized === false) {
disposeDrawioEditor(drawIOEditorState);
createAlert({ message: __('The diagrams.net editor could not be loaded.') });
}
}, DRAWIO_IFRAME_TIMEOUT);
updateDrawioEditorState(drawIOEditorState, {
iframe,
});
}
function attachDrawioIFrameMessageListener(drawIOEditorState, editorFacade) {
const evtHandler = (evt) => {
onDrawIOEditorMessage(drawIOEditorState, editorFacade, evt);
};
window.addEventListener('message', evtHandler);
// Stores a function in the editor state object that allows disposing
// the message event listener when the editor exits.
updateDrawioEditorState(drawIOEditorState, {
disposeEventListener: () => {
window.removeEventListener('message', evtHandler);
},
});
}
const createDrawioEditorState = ({ filename = null, drawioUrl }) => ({
newDiagram: true,
filename,
diagramSvg: null,
diagramMarkdown: null,
iframe: null,
isBusy: false,
initialized: false,
dark: darkModeEnabled(),
disposeEventListener: null,
drawioUrl,
});
export function launchDrawioEditor({ editorFacade, filename, drawioUrl = gon.diagramsnet_url }) {
const url = new URL(drawioUrl);
for (const [key, value] of Object.entries(DRAWIO_PARAMS)) {
url.searchParams.set(key, value);
}
const drawIOEditorState = createDrawioEditorState({ filename, drawioUrl: url.href });
// The execution order of these two functions matter
attachDrawioIFrameMessageListener(drawIOEditorState, editorFacade);
createEditorIFrame(drawIOEditorState);
}
|