diff --git a/.gitignore b/.gitignore index f25b6a2..01e8619 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ *.log # Caches and runtime sessions +.superpowers/ /__pycache__/ *.py[cod] .pytest_cache/ diff --git a/cmd/image-manipulator/app.js b/cmd/image-manipulator/app.js index f0df292..8ec8a54 100644 --- a/cmd/image-manipulator/app.js +++ b/cmd/image-manipulator/app.js @@ -313,10 +313,80 @@ function renderActiveImage() { }; } +// ═══════════════════════════════════════════════════════════════════ +// DOWNLOAD / EXPORT UTILITIES (Single & Batch) +// ═══════════════════════════════════════════════════════════════════ +async function downloadImageItem(item) { + if (!item || (!item.dataUrl && !item.file)) { + showToast("No image available to download", "error"); + return; + } + try { + let blob; + if (item.dataUrl) { + const res = await fetch(item.dataUrl); + blob = await res.blob(); + } else if (item.file instanceof Blob) { + blob = item.file; + } + + if (!blob) { + showToast("Failed to prepare image blob for download", "error"); + return; + } + + let filename = item.name || "processed_image"; + if (filename.includes("/")) { + filename = filename.substring(filename.lastIndexOf("/") + 1); + } + const mimeExtMap = { + "image/jpeg": ".jpg", + "image/png": ".png", + "image/webp": ".webp", + "image/avif": ".avif", + "image/bmp": ".bmp", + "image/tiff": ".tiff" + }; + if (blob.type && mimeExtMap[blob.type]) { + const expectedExt = mimeExtMap[blob.type]; + const curExt = filename.substring(filename.lastIndexOf(".")).toLowerCase(); + if (!curExt || (curExt !== expectedExt && !(expectedExt === ".jpg" && curExt === ".jpeg"))) { + filename = filename.replace(/\.[^/.]+$/, "") + expectedExt; + } + } + + const blobUrl = window.URL.createObjectURL(blob); + const a = document.createElement("a"); + a.href = blobUrl; + a.download = filename; + document.body.appendChild(a); + a.click(); + a.remove(); + setTimeout(() => window.URL.revokeObjectURL(blobUrl), 2000); + showToast(`Downloaded ${filename}`, "success"); + } catch (err) { + showToast(`Download failed: ${err.message}`, "error"); + } +} +window.downloadImageItem = downloadImageItem; + +function downloadActiveImage() { + if (!filesList || filesList.length === 0) { + showToast("No image open to download", "error"); + return; + } + const cur = filesList[activeIndex]; + if (!cur) return; + downloadImageItem(cur); +} +window.downloadActiveImage = downloadActiveImage; + function updateQueueUI() { const counterPill = document.getElementById("ps-file-counter"); const btnBatchHeader = document.getElementById("btn-batch-all"); const btnBatchFooter = document.getElementById("btn-batch-footer"); + const btnDownloadHeader = document.getElementById("btn-download-active"); + const btnDownloadFooter = document.getElementById("btn-download-footer"); const hubCount = document.getElementById("batch-hub-count"); const hubSize = document.getElementById("batch-hub-size"); @@ -330,6 +400,14 @@ function updateQueueUI() { else counterPill.classList.remove("has-files"); } + // Show Download button whenever there is at least 1 image loaded + if (btnDownloadHeader) { + btnDownloadHeader.style.display = count > 0 ? "inline-flex" : "none"; + } + if (btnDownloadFooter) { + btnDownloadFooter.style.display = count > 0 ? "inline-flex" : "none"; + } + if (btnBatchHeader) { if (count > 1) { btnBatchHeader.style.display = "inline-flex"; @@ -369,9 +447,14 @@ function renderFilmstrip() { ${item.name} ${item.naturalW}×${item.naturalH} · ${sizeKb}KB - +