Skip to content

Commit

Permalink
Media progressive enhancement: drag and drop to fill regular file inp…
Browse files Browse the repository at this point in the history
…ut (fixed as prettier targets es5)
  • Loading branch information
jbidoret committed Oct 16, 2024
1 parent 69a3f84 commit a0e055d
Showing 1 changed file with 34 additions and 21 deletions.
55 changes: 34 additions & 21 deletions src/media.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,40 @@ window.addEventListener('load', () => {
const fileInput = document.getElementById('file');
const fileMenu = fileInput.closest('details');

'drag dragstart dragend dragover dragenter dragleave drop'.split(' ').forEach(function(listener){
document.body.addEventListener(listener,function(e) {
e.preventDefault();
e.stopPropagation();
},false);
});
'drag dragstart dragend dragover dragenter dragleave drop'
.split(' ')
.forEach(function(listener) {
document.body.addEventListener(
listener,
function(e) {
e.preventDefault();
e.stopPropagation();
},
false
);
});

'dragover dragenter'.split(' ').forEach(function(listener){
document.body.addEventListener(listener,function(e) {
fileMenu.setAttribute("open", true);
},false);
'dragover dragenter'.split(' ').forEach(function(listener) {
document.body.addEventListener(
listener,
function(e) {
fileMenu.setAttribute('open', true);
},
false
);
});

document.body.addEventListener('drop',function(e) {
const files = e.dataTransfer.files;
const info = document.createElement("p");
info.className = "dropped-files-info";
for(const f of files){
info.innerHTML += `<span>${f.name }</span> `;
}
fileInput.insertAdjacentElement("afterend", info);
fileInput.files = files;
}, false);

document.body.addEventListener(
'drop',
function(e) {
const files = e.dataTransfer.files;
const info = document.createElement('p');
info.className = 'dropped-files-info';
for (const f of files) {
info.innerHTML += `<span>${f.name}</span> `;
}
fileInput.insertAdjacentElement('afterend', info);
fileInput.files = files;
},
false
);

0 comments on commit a0e055d

Please sign in to comment.