drag n drop

This commit is contained in:
Wavering Ana 2022-11-15 15:07:35 -05:00
parent e02e3d2946
commit 9358ba54cc
No known key found for this signature in database
GPG key ID: 51C992200FFA5281
4 changed files with 108 additions and 4 deletions

89
public/js/index.js Normal file
View file

@ -0,0 +1,89 @@
function copyURI(evt) {
evt.preventDefault();
navigator.clipboard.writeText(absolutePath(evt.target.getAttribute('src'))).then(() => {
/* clipboard successfully set */
console.log("copied")
}, () => {
/* clipboard write failed */
console.log("failed")
});
}
function absolutePath (href) {
let link = document.createElement("a");
link.href = href;
return link.href;
}
let dropArea = document.getElementById("dropArea");
;['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, preventDefaults, false)
})
function preventDefaults (e) {
e.preventDefault()
e.stopPropagation()
}
;['dragenter', 'dragover'].forEach(eventName => {
dropArea.addEventListener(eventName, highlight, false)
})
;['dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, unhighlight, false)
})
function highlight(e) {
dropArea.classList.add('highlight')
}
function unhighlight(e) {
dropArea.classList.remove('highlight')
}
dropArea.addEventListener('drop', handleDrop, false)
function handleDrop(e) {
let dt = e.dataTransfer
let files = dt.files
handleFiles(files)
}
function handleFiles(files) {
files = [...files]
files.forEach(uploadFile)
files.forEach(previewFile)
}
function previewFile(file) {
let reader = new FileReader()
reader.readAsDataURL(file)
reader.onloadend = function() {
let img = document.createElement('img');
img.src = reader.result
img.className = "image";
document.getElementById('gallery').appendChild(img)
console.log(document.getElementById('fileupload'))
document.getElementById('fileupload').src = img.src;
}
}
function uploadFile(file) {
let xhr = new XMLHttpRequest();
let formData = new FormData();
xhr.open('POST', '/', true);
xhr.addEventListener('readystatechange', function(e) {
if (xhr.readyState == 4 && xhr.status == 200) {
location.reload();
}
else if (xhr.readyState == 4 && xhr.status != 200) {
// Error. Inform the user
}
})
formData.append('fileupload', file);
xhr.send(formData)
}