-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathembed.js
More file actions
70 lines (64 loc) · 2.16 KB
/
embed.js
File metadata and controls
70 lines (64 loc) · 2.16 KB
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
// Edits an image with drawio class on double click
document.addEventListener('dblclick', function(evt)
{
var url = 'https://test.draw.io/?dev=1&embed=1&ui=atlas&spin=1&modified=unsavedChanges&proto=json';
var source = evt.srcElement || evt.target;
if (source.nodeName == 'IMG' && source.className == 'drawio')
{
// Checks if the elt is inside a content editable element
var parent = source;
while (parent != null && parent.nodeType == 1 &&
parent.getAttribute('contentEditable') != 'true')
{
parent = parent.parentNode;
}
if (parent != null && parent.nodeType == 1 && parent.getAttribute('contentEditable') == 'true')
{
if (source.drawIoWindow == null || source.drawIoWindow.closed)
{
// Implements protocol for loading and exporting with embedded XML
var receive = function(evt)
{
if (evt.data.length > 0 && evt.source == source.drawIoWindow)
{
var msg = JSON.parse(evt.data);
// Received if the editor is ready
if (msg.event == 'init')
{
// Sends the data URI with embedded XML to editor
source.drawIoWindow.postMessage(JSON.stringify({action: 'load', xmlpng: source.getAttribute('src')}), '*');
}
// Received if the user clicks save
else if (msg.event == 'save')
{
// Sends a request to export the diagram as XML with embedded PNG
source.drawIoWindow.postMessage(JSON.stringify({action: 'export', format: 'xmlpng', spinKey: 'saving'}), '*');
}
// Received if the export request was processed
else if (msg.event == 'export')
{
// Updates the data URI of the image
source.setAttribute('src', msg.data);
}
// Received if the user clicks exit or after export
if (msg.event == 'exit' || msg.event == 'export')
{
// Closes the editor
window.removeEventListener('message', receive);
source.drawIoWindow.close();
source.drawIoWindow = null;
}
}
};
// Opens the editor
window.addEventListener('message', receive);
source.drawIoWindow = window.open(url);
}
else
{
// Shows existing editor window
source.drawIoWindow.focus();
}
}
}
}, true);