forked from lejmr/dokuwiki-plugin-drawio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
executable file
·129 lines (114 loc) · 4.05 KB
/
script.js
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
// Embeded editor
var editor = 'https://www.draw.io/?embed=1&ui=atlas&spin=1&proto=json';
var initial = null;
var name = null;
var imagePointer = null;
function edit(image)
{
var iframe = document.createElement('iframe');
iframe.setAttribute('frameborder', '0');
iframe.setAttribute('class', 'drawio');
imagePointer = image;
var close = function()
{
window.removeEventListener('message', receive);
document.body.removeChild(iframe);
};
var draft = localStorage.getItem('.draft-' + name);
if (draft != null)
{
draft = JSON.parse(draft);
if (!confirm("A version of this page from " + new Date(draft.lastModified) + " is available. Would you like to continue editing?"))
{
draft = null;
}
}
var receive = function(evt)
{
if (evt.data.length > 0)
{
var msg = JSON.parse(evt.data);
if (msg.event == 'init')
{
if (draft != null)
{
iframe.contentWindow.postMessage(JSON.stringify({action: 'load',
autosave: 1, xml: draft.xml}), '*');
iframe.contentWindow.postMessage(JSON.stringify({action: 'status',
modified: true}), '*');
}
else
{
// Read from AJAX
jQuery.post(
DOKU_BASE + 'lib/exe/ajax.php',
{
call: 'plugin_drawio',
imageName: imagePointer.getAttribute('id'),
action: 'get'
},
function(data){
iframe.contentWindow.postMessage(JSON.stringify({action: 'load',
autosave: 1, xmlpng: data.content}), '*');
}
);
}
}
else if (msg.event == 'export')
{
image.setAttribute('src', msg.data);
localStorage.setItem(name, JSON.stringify({lastModified: new Date(), data: msg.data}));
localStorage.removeItem('.draft-' + name);
draft = null;
close();
// Save into dokuwiki
jQuery.post(
DOKU_BASE + 'lib/exe/ajax.php',
{
call: 'plugin_drawio',
imageName: imagePointer.getAttribute('id'),
content: msg.data,
action: 'save'
}
);
}
else if (msg.event == 'autosave')
{
localStorage.setItem('.draft-' + name, JSON.stringify({lastModified: new Date(), xml: msg.xml}));
}
else if (msg.event == 'save')
{
iframe.contentWindow.postMessage(JSON.stringify({action: 'export',
format: 'xmlpng', xml: msg.xml, spin: 'Updating page'}), '*');
localStorage.setItem('.draft-' + name, JSON.stringify({lastModified: new Date(), xml: msg.xml}));
}
else if (msg.event == 'exit')
{
localStorage.removeItem('.draft-' + name);
draft = null;
close();
}
}
};
window.addEventListener('message', receive);
iframe.setAttribute('src', editor);
document.body.appendChild(iframe);
};
// Toolbar menu items
function getImageName(){
seq = JSINFO.id.split(":");
seq = seq.slice(0,seq.length-1);
seq.push("diagram1");
return seq.join(":");
}
if (typeof window.toolbar !== 'undefined') {
toolbar[toolbar.length] = {
type: "format",
title: "",
icon: "../../plugins/drawio/icon.png",
key: "",
// open: "{{drawio>" + JSINFO.id + "}}",
open: "{{drawio>" + getImageName() + "}}",
close: ""
};
};