add drag and drop
This commit is contained in:
+31
-14
@@ -159,24 +159,41 @@ option {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.btn:hover {
|
||||
background-color: var(--text-muted);
|
||||
}
|
||||
|
||||
.link-btn {
|
||||
border: none;
|
||||
background: none;
|
||||
padding: 0;
|
||||
text-decoration: underline;
|
||||
color: var(--text);
|
||||
cursor: pointer;
|
||||
font: inherit;
|
||||
border: none;
|
||||
background: none;
|
||||
padding: 0;
|
||||
text-decoration: underline;
|
||||
color: var(--text);
|
||||
cursor: pointer;
|
||||
font: inherit;
|
||||
}
|
||||
|
||||
.upload-links {
|
||||
margin-top: 12px;
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
gap: 12px;
|
||||
align-items: baseline;
|
||||
margin-top: 12px;
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
gap: 12px;
|
||||
align-items: baseline;
|
||||
}
|
||||
|
||||
.upload-links form {
|
||||
margin: 0;
|
||||
}
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
body.dragging::after {
|
||||
content: "drop file to select";
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
z-index: 100;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
background: rgb(0 0 0 / 30%);
|
||||
color: var(--text);
|
||||
font-size: 1.5rem;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
const fileInput = document.getElementById("upload-file");
|
||||
|
||||
let dragDepth = 0;
|
||||
|
||||
function isFile(event) {
|
||||
return event.dataTransfer.types.includes("Files");
|
||||
}
|
||||
|
||||
function resetDrag() {
|
||||
dragDepth = 0;
|
||||
document.body.classList.remove("dragging");
|
||||
}
|
||||
|
||||
document.addEventListener("dragenter", (event) => {
|
||||
if (!isFile(event)) return;
|
||||
|
||||
event.preventDefault();
|
||||
dragDepth++;
|
||||
document.body.classList.add("dragging");
|
||||
});
|
||||
|
||||
document.addEventListener("dragover", (event) => {
|
||||
if (!isFile(event)) return;
|
||||
|
||||
event.preventDefault();
|
||||
event.dataTransfer.dropEffect = "copy";
|
||||
});
|
||||
|
||||
document.addEventListener("dragleave", () => {
|
||||
dragDepth = Math.max(0, dragDepth - 1);
|
||||
|
||||
if (dragDepth === 0) {
|
||||
document.body.classList.remove("dragging");
|
||||
}
|
||||
});
|
||||
|
||||
document.addEventListener("drop", (event) => {
|
||||
resetDrag();
|
||||
if (!isFile(event)) return;
|
||||
|
||||
event.preventDefault();
|
||||
|
||||
const file = event.dataTransfer.files[0];
|
||||
if (!file) return;
|
||||
|
||||
const transfer = new DataTransfer();
|
||||
transfer.items.add(file);
|
||||
fileInput.files = transfer.files;
|
||||
|
||||
document.getElementById("type-file").checked = true;
|
||||
fileInput.dispatchEvent(new Event("change", { bubbles: true }));
|
||||
});
|
||||
|
||||
document.addEventListener("dragend", resetDrag);
|
||||
Reference in New Issue
Block a user