Skip to content

Commit cc81bb5

Browse files
committed
simplified JavaScript
This mostly replaces jQuery code with leaner vanilla JS.
1 parent 20d44e1 commit cc81bb5

File tree

2 files changed

+80
-120
lines changed

2 files changed

+80
-120
lines changed

jquery.paste_image_reader.js

Lines changed: 0 additions & 64 deletions
This file was deleted.

script.js

Lines changed: 80 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,91 @@
1-
/* DOKUWIKI:include jquery.paste_image_reader.js */
1+
(function () {
22

3-
jQuery(function () {
4-
var _didInit = false;
5-
function init() {
6-
if (!jQuery('#wiki__text').length || _didInit) return;
7-
_didInit = true;
8-
jQuery('html').pasteImageReader({
9-
callback: function (x) {
10-
if (!jQuery('#wiki__text').length) return;
3+
/**
4+
* Handle pasting of files
5+
*
6+
* @param {ClipboardEvent} e
7+
*/
8+
function handlePaste(e) {
9+
if (!document.getElementById('wiki__text')) return; // only when editing
1110

12-
console.log(x);
11+
const items = (e.clipboardData || e.originalEvent.clipboardData).items;
12+
for (let index in items) {
13+
const item = items[index];
1314

14-
// create dialog
15-
var offset = jQuery('.plugin_imagepaste').length * 20;
16-
var $box = jQuery('<div><div class="content">' + LANG.plugins.imgpaste.inprogress + '</div></div>');
17-
$box.dialog({
18-
title: 'Upload',
19-
dialogClass: 'plugin_imagepaste',
20-
closeOnEscape: false,
21-
resizable: false,
22-
position: {
23-
my: 'center+' + offset + ' center+' + offset
24-
},
25-
appendTo: '.dokuwiki'
26-
});
15+
if (item.kind === 'file') {
16+
const reader = new FileReader();
17+
reader.onload = event => {
18+
uploadData(event.target.result);
19+
};
20+
reader.readAsDataURL(item.getAsFile());
2721

28-
// upload via AJAX
29-
jQuery.ajax({
30-
url: DOKU_BASE + 'lib/exe/ajax.php',
31-
type: 'POST',
32-
data: {
33-
call: 'plugin_imgpaste',
34-
data: x.dataURL,
35-
id: JSINFO.id
36-
},
22+
// we had at least one file, prevent default
23+
e.preventDefault();
24+
e.stopPropagation();
25+
}
26+
}
27+
}
28+
29+
/**
30+
* Uploads the given dataURL to the server and displays a progress dialog
31+
*
32+
* @param {string} dataURL
33+
*/
34+
function uploadData(dataURL) {
35+
// create dialog
36+
const offset = document.querySelectorAll('.plugin_imagepaste').length * 3;
37+
const box = document.createElement('div');
38+
box.className = 'plugin_imagepaste';
39+
box.innerText = LANG.plugins.imgpaste.inprogress;
40+
box.style.position = 'fixed';
41+
box.style.top = offset + 'em';
42+
box.style.left = '1em';
43+
document.querySelector('.dokuwiki').append(box);
3744

38-
// insert syntax and close dialog
39-
success: function (data) {
40-
$box.find('.content').addClass('success').text(data.message);
41-
insertAtCarret('wiki__text', '{{:' + data.id + '}}');
42-
$box.delay(500).fadeOut(500, function () {
43-
$box.dialog('destroy').remove()
44-
});
45-
},
45+
// upload via AJAX
46+
jQuery.ajax({
47+
url: DOKU_BASE + 'lib/exe/ajax.php',
48+
type: 'POST',
49+
data: {
50+
call: 'plugin_imgpaste',
51+
data: dataURL,
52+
id: JSINFO.id
53+
},
4654

47-
// display error and close dialog
48-
error: function (xhr, status, error) {
49-
$box.find('.content').addClass('error').text(error);
50-
$box.delay(1000).fadeOut(500, function () {
51-
$box.dialog('destroy').remove()
52-
});
53-
}
54-
});
55+
// insert syntax and close dialog
56+
success: function (data) {
57+
box.classList.remove('info');
58+
box.classList.add('success');
59+
box.innerText = data.message;
60+
setTimeout(() => {
61+
box.remove();
62+
}, 1000);
63+
insertSyntax(data.id);
64+
},
65+
66+
// display error and close dialog
67+
error: function (xhr, status, error) {
68+
box.classList.remove('info');
69+
box.classList.add('error');
70+
box.innerText = error;
71+
setTimeout(() => {
72+
box.remove();
73+
}, 1000);
5574
}
5675
});
5776
}
5877

59-
init();
78+
/**
79+
* Inserts the given ID into the current editor
80+
*
81+
* @todo add suppprt for other editors like Prosemirror or CKEditor
82+
* @param {string} id The newly uploaded file ID
83+
*/
84+
function insertSyntax(id) {
85+
insertAtCarret('wiki__text', '{{:' + id + '}}');
86+
}
87+
88+
// main
89+
window.addEventListener('paste', handlePaste);
6090

61-
// fastwiki plugin support
62-
jQuery(window).on('fastwiki:afterSwitch', function(evt, viewMode, isSectionEdit, prevViewMode) {
63-
if (viewMode == 'edit' || isSectionEdit) {
64-
init();
65-
}
66-
});
67-
});
91+
})();

0 commit comments

Comments
 (0)