diff --git a/static/js/utils/drag-drop-into-text.js b/static/js/utils/drag-drop-into-text.js new file mode 100644 index 00000000..ab4b062f --- /dev/null +++ b/static/js/utils/drag-drop-into-text.js @@ -0,0 +1,28 @@ +const MAX_BYTES = 10240; // 10KiB + +function enableDragDropForPGPKeys(inputID) { + function drop(event) { + event.stopPropagation(); + event.preventDefault(); + + let files = event.dataTransfer.files; + for (let i = 0; i < files.length; i++) { + let file = files[i]; + if(file.type !== 'text/plain'){ + toastr.warning(`File ${file.name} is not a public key file`); + continue; + } + let reader = new FileReader(); + reader.onloadend = onFileLoaded; + reader.readAsBinaryString(file); + } + } + + function onFileLoaded(event) { + const initialData = event.currentTarget.result.substr(0, MAX_BYTES); + $(inputID).val(initialData); + } + + const dropArea = $(inputID).get(0); + dropArea.addEventListener("drop", drop, false); +} \ No newline at end of file diff --git a/templates/dashboard/contact_detail.html b/templates/dashboard/contact_detail.html index 25a79776..f6fc405b 100644 --- a/templates/dashboard/contact_detail.html +++ b/templates/dashboard/contact_detail.html @@ -49,8 +49,10 @@ +
You can drag and drop the pgp key file into the text area
+