From d524c65209670811d72ed830ae76285c1d9f1850 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Sat, 23 Feb 2019 13:17:22 +0100 Subject: [PATCH] Handle copy/paste --- static/script.js | 60 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/static/script.js b/static/script.js index 1f88d95..a579a3e 100644 --- a/static/script.js +++ b/static/script.js @@ -25,28 +25,54 @@ dropZone.classList.remove(hoverClassName); }); - // This is the most important event, the event that gives access to files + // Handle copy/paste + dropZone.addEventListener("paste", function (e) { + e.preventDefault(); + + if (e.clipboardData.items.length != 1) { + return + } + + const item = e.clipboardData.items[0]; + if (item.type.indexOf("image") == -1) { + return; + } + + fetch('/', { + method: 'POST', + body: item.getAsFile(), + }) + .then(response => response.text()) + .then(body => { // Yes, this is ugly + document.open() + document.write(body) + document.close() + }) + + }); + dropZone.addEventListener("drop", function (e) { e.preventDefault(); dropZone.classList.remove(hoverClassName); const files = Array.from(e.dataTransfer.files); - if (files.length > 0) { - const data = new FormData(); - for (const file of files) { - data.append('file', file); - } - - fetch('/', { - method: 'POST', - body: data, - }) - .then(response => response.text()) - .then(body => { // Yes, this is ugly - document.open() - document.write(body) - document.close() - }) + if (files.length != 1 ) { + return; } + const data = new FormData(); + for (const file of files) { + data.append('file', file); + } + + fetch('/', { + method: 'POST', + body: data, + }) + .then(response => response.text()) + .then(body => { // Yes, this is ugly + document.open() + document.write(body) + document.close() + }) }); })();