forked from SiriusXT/trilium-drawio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trilium-drawio.js
303 lines (280 loc) · 12.9 KB
/
trilium-drawio.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
trilium-drawio
https://github.com/SiriusXT/trilium-drawio
version:0.4
*/
var defaultTheme = "0" //0:light;1:dark;,2:Follow software theme styles
var currentNoteId;
var themeStyle = getComputedStyle(document.documentElement).getPropertyValue('--theme-style');
var $last_image_wrapper;//Used to detect tab switching
var last_noteId;//For detection and switching of new tab pages
var editor = 'https://embed.diagrams.net/?embed=1&ui=min&spin=1&proto=json&configure=1&libraries=1&noSaveBtn=1';
var id_svg_dict = {}
var noteId = ''
function edit(noteId) {
$('div.component.note-split:not(.hidden-ext) div.component.scrolling-container div.note-detail.component div.note-detail-image-wrapper').off("click");
var svg = id_svg_dict[noteId];
$("div.component.note-split:not(.hidden-ext) .note-detail-image-wrapper .note-detail-image-view").css("display", "none");
var div_iframe = document.createElement('div');
div_iframe.classList.add("iframe-drawio");
var iframe = document.createElement('iframe');
iframe.setAttribute('frameborder', '0');
if (themeStyle.indexOf('dark') >= 0 && defaultTheme != 0) { div_iframe.classList.add("dark"); }
iframe.setAttribute('src', editor);
document.querySelector('div.component.note-split:not(.hidden-ext) .note-detail-image-wrapper').appendChild(div_iframe);
div_iframe.appendChild(iframe);
$('div.component.note-split:not(.hidden-ext) .note-detail-image-wrapper div.iframe-drawio').prepend(`<div class="drawio-iframe" style=" position: absolute; border-radius: 5px; color:#837d7d; box-shadow: inset 0 0 0 1px rgb(0 0 0 / 11%), inset 0 -1px 0 0 rgb(0 0 0 / 8%), 0 1px 2px 0 rgb(0 0 0 / 4%);
right: 8px; top: 8px; padding: 0px 4px; ">
<div class="drawio-switch-theme bx" title="Drawio Switch Theme" style="cursor: pointer; padding: 6px;"></div>
<div class="drawio-switch-full-screen bx" title="Drawio Switch Full Screen" style=" cursor: pointer; padding: 6px;">
</div><div class="drawio-save-and-close bx" title="Drawio Close and Exit" style=" cursor: pointer; padding: 6px;">
</div></div>`);
$('div.component.note-split:not(.hidden-ext) .note-detail-image-wrapper div.iframe-drawio .drawio-switch-theme.bx').click(function (event) {//Drawio Switch Theme
event.stopPropagation();
var $iframe_tmp = $("div.component.note-split:not(.hidden-ext) div.note-detail-image-wrapper div.iframe-drawio");
if ($iframe_tmp.length > 0) {
if ($iframe_tmp.hasClass("dark")) {
$iframe_tmp.removeClass("dark");
}
else {
$iframe_tmp.addClass("dark");
}
}
else {
const $iframe_tmp = $("body > div.iframe-drawio");
if ($iframe_tmp.hasClass("dark")) {
$iframe_tmp.removeClass("dark");
}
else {
$iframe_tmp.addClass("dark");
}
}
});
$('div.component.note-split:not(.hidden-ext) .note-detail-image-wrapper div.iframe-drawio .drawio-switch-full-screen.bx').click(function (event) {//Drawio full screen
event.stopPropagation();
const $iframe_tmp = $("div.component.note-split:not(.hidden-ext) .note-detail-image-wrapper div.iframe-drawio");
if ($iframe_tmp.length > 0) {
$iframe_tmp.appendTo($(parent.document).find("body"));
$iframe_tmp.css("position", "fixed");
$(".tab-row-filler").css("-webkit-app-region", "none");
}
else {
const $iframe_tmp = $("body > div.iframe-drawio");
$iframe_tmp.appendTo($("div.component.note-split:not(.hidden-ext) .note-detail-image-wrapper"));
$iframe_tmp.css("position", "");
$(".tab-row-filler").css("-webkit-app-region", "drag");
}
});
//Close and exit button
$('div.component.note-split:not(.hidden-ext) .note-detail-image-wrapper div.iframe-drawio .drawio-save-and-close.bx').click(function (event) {
event.stopPropagation();
close();
})
var close = function () {
id_svg_dict[noteId] = svg;
window.removeEventListener('message', receive);
if ($("div.component.note-split:not(.hidden-ext) .note-detail-image-wrapper div.iframe-drawio").length > 0) {
$("div.component.note-split:not(.hidden-ext) .note-detail-image-wrapper div.iframe-drawio").remove();
}
else {
$("body > div.iframe-drawio").remove();
$(".tab-row-filler").css("-webkit-app-region", "drag");
}
$("div.component.note-split:not(.hidden-ext) .note-detail-image-wrapper img.note-detail-image-view").css("display", "block");
var img = $("div.component.note-split:not(.hidden-ext) .note-detail-image-wrapper img.note-detail-image-view");
img.attr("src", img.attr("src"));
///////////////////////////////////
$('div.component.note-split:not(.hidden-ext) div.component.scrolling-container div.note-detail.component div.note-detail-image-wrapper').click(noteId, function () {
edit(noteId);
});
};
var receive = function (evt) {
if (noteId != currentNoteId || div_iframe == undefined || iframe.contentWindow == null) { return; }
if (evt.data.length > 0) {
var msg = JSON.parse(evt.data);
// If configure=1 URL parameter is used the application
// waits for this message. For configuration options see
// https://desk.draw.io/support/solutions/articles/16000058316
if (msg.event == 'configure') {
iframe.contentWindow.postMessage(JSON.stringify({
action: 'configure',
config: { defaultFonts: ["Helvetica", "Verdana", "Times New Roman", "SimSun"], css: " body.geEditor > div.mxWindow:nth-of-type(3) { right: 10px !important; top:63px !important; left: unset !important;} body.geEditor > div.mxWindow:nth-of-type(2) { left: 10px !important; top:63px !important; " }
}), '*');
}
else if (msg.event == 'init') {
iframe.contentWindow.postMessage(JSON.stringify({ action: 'load', autosave: 1, xml: svg, saveAndExit: '0', noExitBtn: '1', saveAndExit: '0' }), '*'); //noExitBtn:'1',saveAndExit: '1',
}
else if (msg.event == 'export') {
// Extracts SVG DOM from data URI to enable links
//svg = atob(msg.data.substring(msg.data.indexOf(',') + 1));
var base64String = msg.data.substring(msg.data.indexOf(',') + 1);
const bytes = new Uint8Array(atob(base64String).split('').map(c => c.charCodeAt(0)));
const decoder = new TextDecoder('utf-8');
const decodedString = decoder.decode(bytes);
svg = decodedString;
id_svg_dict[noteId] = svg;
api.runAsyncOnBackendWithManualTransactionHandling(async (noteId, svg) => {
const note = await api.getNote(noteId);
note.setContent(svg);
}, [noteId, svg]);
}
else if (msg.event == 'autosave') {
iframe.contentWindow.postMessage(JSON.stringify({
action: 'export',
format: 'xmlsvg', spin: 'Updating page',
}), '*');
}
else if (msg.event == 'save') {
iframe.contentWindow.postMessage(JSON.stringify({
action: 'export',
format: 'xmlsvg', spin: 'Updating page'
}), '*');
}
else if (msg.event == 'exit') {
close();
}
}
};
window.addEventListener('message', receive);
};
function addClick(noteId, autoEdit) {
var $img = $('div.component.note-split:not(.hidden-ext) div.component.scrolling-container div.note-detail.component img.note-detail-image-view');
if (!$img.hasClass('dark') && !$img.hasClass('light')) {
$('div.component.note-split:not(.hidden-ext) div.component.scrolling-container div.note-detail.component div.note-detail-image-wrapper').off("click");
$('div.component.note-split:not(.hidden-ext) div.component.scrolling-container div.note-detail.component div.note-detail-image-wrapper').click(noteId, function () {
edit(noteId);
});
}
if (themeStyle.indexOf('dark') >= 0) { $img.addClass('dark'); }
else if (themeStyle.indexOf('light') >= 0) { $img.addClass('light'); }
if (autoEdit) {
edit(noteId);
}
}
class DrawiIo extends api.NoteContextAwareWidget {
get position() {
return 100;
}
get parentWidget() {
return 'center-pane';
}
doRender() {
this.$widget = $(`<style type="text/css">
div.iframe-drawio{
width: 100%;
height: 100%;
}
img.note-detail-image-view{
transform: none !important;
width: max-content;
max-width: 100%;
height: max-content;
max-height: 100%;
}
img.note-detail-image-view.dark {
filter: invert(88%) hue-rotate(180deg);
}
iframe{
z-index: 100;
}
div.iframe-drawio.dark{
filter: invert(88%) hue-rotate(180deg);
}
.iframe-drawio iframe {
border:0;
right:0;
bottom:0;
width:100%;
height:100%
}
div.iframe-drawio{
z-index: 100;
border:0;
right:0;
bottom:0;
width:100%;
height:100%
}
.drawio-switch-theme.bx::before {
content: "\\ec34";
}
.drawio-switch-full-screen.bx::before {
content: "\\eaeb";
}
.drawio-save-and-close.bx::before {
content: "\\ec8d";
}
</style>`);
return this.$widget;
}
async refreshWithNote(note) {
var noteId = note.noteId;
currentNoteId = noteId;
var autoEdit = false;
id_svg_dict[noteId] = (await note.getNoteComplement()).content;
if (note.hasLabel("originalFileName") && note.getLabel("originalFileName").value == "drawio.svg" && (await note.getNoteComplement()).content == undefined) {
id_svg_dict[noteId] = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="1px" height="1px" viewBox="-0.5 -0.5 1 1" content="<mxfile host="app.diagrams.net" modified="2023-04-29T09:11:12.196Z" agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/112.0" etag="fO2zVAtDBdoeKHVc3k6l" version="21.2.3">
 <diagram name="Page-1" id="mQcrQwb1aOjnsej_quHy">
 <mxGraphModel dx="1238" dy="773" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
 <root>
 <mxCell id="0" />
 <mxCell id="1" parent="0" />
 </root>
 </mxGraphModel>
 </diagram>
</mxfile>
" resource="https://app.diagrams.net/?src=about#"><defs/><g/></svg>`;
note.mime = "image/svg+xml";
autoEdit = true;
api.runAsyncOnBackendWithManualTransactionHandling(async (NoteId, svg) => {
const Note = await api.getNote(NoteId);
Note.setContent(svg);
Note.mime = "image/svg+xml";
Note.title = Note.title + ".drawio.svg"
Note.save();
}, [noteId, id_svg_dict[noteId]]);
}
$(document).ready(function () {
var ischangeTab = false;
if ($last_image_wrapper != undefined && $last_image_wrapper.length > 0) {
window.last_image_wrapper = $last_image_wrapper;
$.each($last_image_wrapper.parents(), function (index, value) {
var truecount = 0;
$.each(value.classList, function (index1, classL) {
if (classL == "note-split") { truecount += 1; }
else if (classL == "hidden-ext") { truecount += 1; }
});
if (truecount == 2) {
ischangeTab = true;
}
});
}
if (last_noteId != undefined && last_noteId == noteId) {
ischangeTab = true;
}
$last_image_wrapper = $("div.component.note-split:not(.hidden-ext) div.scrolling-container.component");
last_noteId = noteId;
if (!ischangeTab) {
if ($("div.component.note-split:not(.hidden-ext) div.note-detail-image-wrapper div.iframe-drawio").length > 0) { $("div.component.note-split:not(.hidden-ext) div.note-detail-image-wrapper div.iframe-drawio").remove(); }
if ($("div.component.note-split:not(.hidden-ext) .note-detail-printable.component div.note-detail-image-wrapper img.note-detail-image-view").length > 0) { $("div.component.note-split:not(.hidden-ext) .note-detail-printable.component div.note-detail-image-wrapper img.note-detail-image-view").css("display", "block"); }
$('div.component.note-split:not(.hidden-ext) .note-detail-printable.component .note-detail-image-wrapper').off("click");
var $img = $('div.component.note-split:not(.hidden-ext) .note-detail-printable.component img.note-detail-image-view');
if ($img.length > 0) {
if ($img.hasClass('dark')) { $img.removeClass('dark') }
if ($img.hasClass('light')) { $img.removeClass('light') }
}
}
if (note.mime != "image/svg+xml" || id_svg_dict[noteId].indexOf("mxfile") < 0) { return; }
setTimeout(function () {
if ($("div.component.note-split:not(.hidden-ext) .note-detail-image-wrapper div.iframe-drawio").length > 0) { return; };//When switching tabs, if the iframe is already loaded, return
addClick(noteId, autoEdit);
}, 10);
$("div.ribbon-tab-title.active").click();
});
}
async entitiesReloadedEvent({ loadResults }) {
if (loadResults.isNoteContentReloaded(this.noteId)) {
this.refresh();
}
}
}
module.exports = new DrawiIo();
window.onbeforeunload = function () {
if ($("div.component.note-split:not(.hidden-ext) .note-detail-image-wrapper div.iframe-drawio").length > 0) {
$("div.component.note-split:not(.hidden-ext) .note-detail-image-wrapper div.iframe-drawio").remove();
}
else {
$("body > div.iframe-drawio").remove();
$(".tab-row-filler").css("-webkit-app-region", "drag");
}
};