1(function () { 2 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 10 11 const items = (e.clipboardData || e.originalEvent.clipboardData).items; 12 for (let index in items) { 13 const item = items[index]; 14 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()); 21 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); 44 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 }, 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); 74 } 75 }); 76 } 77 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); 90 91})(); 92