-
Notifications
You must be signed in to change notification settings - Fork 10
/
script.js
105 lines (92 loc) · 3.31 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
(function () {
/**
* Handle pasting of files
*
* @param {ClipboardEvent} e
*/
function handlePaste(e) {
if (!document.getElementById('wiki__text')) return; // only when editing
const items = (e.clipboardData || e.originalEvent.clipboardData).items;
for (let index in items) {
const item = items[index];
if (item.kind === 'file') {
const reader = new FileReader();
reader.onload = event => {
uploadData(event.target.result);
};
reader.readAsDataURL(item.getAsFile());
// we had at least one file, prevent default
e.preventDefault();
e.stopPropagation();
}
}
}
/**
* Uploads the given dataURL to the server and displays a progress dialog
*
* @param {string} dataURL
*/
function uploadData(dataURL) {
// create dialog
const offset = document.querySelectorAll('.plugin_imagepaste').length * 3;
const box = document.createElement('div');
box.className = 'plugin_imagepaste';
box.innerText = LANG.plugins.imgpaste.inprogress;
box.style.position = 'fixed';
box.style.top = offset + 'em';
box.style.left = '1em';
document.querySelector('.dokuwiki').append(box);
// upload via AJAX
jQuery.ajax({
url: DOKU_BASE + 'lib/exe/ajax.php',
type: 'POST',
data: {
call: 'plugin_imgpaste',
data: dataURL,
id: JSINFO.id
},
// insert syntax and close dialog
success: function (data) {
box.classList.remove('info');
box.classList.add('success');
box.innerText = data.message;
setTimeout(() => {
box.remove();
}, 1000);
insertSyntax(data.id);
},
// display error and close dialog
error: function (xhr, status, error) {
box.classList.remove('info');
box.classList.add('error');
box.innerText = error;
setTimeout(() => {
box.remove();
}, 1000);
}
});
}
/**
* Inserts the given ID into the current editor
*
* @todo add support for other editors like CKEditor
* @param {string} id The newly uploaded file ID
*/
function insertSyntax(id) {
// TODO remove the "if" check after LinkWizard.createRelativeID() is available in stable (after Kaos)
if (typeof LinkWizard !== 'undefined' && typeof LinkWizard.createRelativeID === 'function') {
id = LinkWizard.createRelativeID(JSINFO.id, id);
} else {
id = ':' + id;
}
if (typeof window.proseMirrorIsActive !== 'undefined' && window.proseMirrorIsActive === true) {
const pm = window.Prosemirror.view;
const imageNode = pm.state.schema.nodes.image.create({id: id});
pm.dispatch(pm.state.tr.replaceSelectionWith(imageNode));
} else {
insertAtCarret('wiki__text', '{{' + id + '}}');
}
}
// main
window.addEventListener('paste', handlePaste);
})();