From ff2ef7413542cf10ba7a6e246763bcecd6829ec1 Mon Sep 17 00:00:00 2001 From: aidanjacobson Date: Thu, 8 May 2025 19:23:08 -0700 Subject: [PATCH] feat: add support for drag/drop of images --- public/script.js | 111 +++++++++++++++++++++++------------------------ 1 file changed, 54 insertions(+), 57 deletions(-) diff --git a/public/script.js b/public/script.js index 931fe4e..e99ad78 100644 --- a/public/script.js +++ b/public/script.js @@ -7,7 +7,8 @@ let fileType; let pendingFiles = 0; let formatSelected = false; -dropZone.addEventListener("dragover", () => { +dropZone.addEventListener("dragover", (e) => { + e.preventDefault(); dropZone.classList.add("dragover"); }); @@ -15,10 +16,59 @@ dropZone.addEventListener("dragleave", () => { dropZone.classList.remove("dragover"); }); -dropZone.addEventListener("drop", () => { - dropZone.classList.remove("dragover"); +dropZone.addEventListener("drop", (e) => { + e.preventDefault(); + dropZone.classList.remove("dragover"); + + const files = e.dataTransfer.files; + + if (files.length === 0) { + console.warn("No files dropped — likely a URL or unsupported source."); + return; + } + + for (const file of files) { + console.log("Handling dropped file:", file.name); + handleFile(file); + } }); +// Extracted handleFile function for reusability in drag-and-drop and file input +function handleFile(file) { + const fileList = document.querySelector("#file-list"); + + const row = document.createElement("tr"); + row.innerHTML = ` + ${file.name} + + ${(file.size / 1024).toFixed(2)} kB + Remove + `; + + if (!fileType) { + fileType = file.name.split(".").pop(); + fileInput.setAttribute("accept", `.${fileType}`); + setTitle(); + + fetch(`${webroot}/conversions`, { + method: "POST", + body: JSON.stringify({ fileType }), + headers: { "Content-Type": "application/json" }, + }) + .then((res) => res.text()) + .then((html) => { + selectContainer.innerHTML = html; + updateSearchBar(); + }) + .catch(console.error); + } + + fileList.appendChild(row); + file.htmlRow = row; + fileNames.push(file.name); + uploadFile(file); +} + const selectContainer = document.querySelector("form .select_container"); const updateSearchBar = () => { @@ -106,62 +156,9 @@ const updateSearchBar = () => { // Add a 'change' event listener to the file input element fileInput.addEventListener("change", (e) => { - // Get the selected files from the event target const files = e.target.files; - - // Select the file-list table - const fileList = document.querySelector("#file-list"); - - // Loop through the selected files for (const file of files) { - // Create a new table row for each file - const row = document.createElement("tr"); - row.innerHTML = ` - ${file.name} - - ${(file.size / 1024).toFixed(2)} kB - Remove - `; - - if (!fileType) { - fileType = file.name.split(".").pop(); - fileInput.setAttribute("accept", `.${fileType}`); - setTitle(); - - // choose the option that matches the file type - // for (const option of convertFromSelect.children) { - // console.log(option.value); - // if (option.value === fileType) { - // option.selected = true; - // } - // } - - fetch(`${webroot}/conversions`, { - method: "POST", - body: JSON.stringify({ fileType: fileType }), - headers: { - "Content-Type": "application/json", - }, - }) - .then((res) => res.text()) - .then((html) => { - selectContainer.innerHTML = html; - updateSearchBar(); - }) - .catch((err) => console.log(err)); - } - - // Append the row to the file-list table - fileList.appendChild(row); - - //Imbed row into the file to reference later - file.htmlRow = row; - - - // Append the file to the hidden input - fileNames.push(file.name); - - uploadFile(file); + handleFile(file); } });