From 0a6f00749bb0720664409e9c4eca82928e6773c7 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Mon, 16 Jun 2025 22:15:15 +0400 Subject: feat: paste images --- public/index.php | 33 ++++++++------------------------- public/static/scripts/form.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 25 deletions(-) create mode 100644 public/static/scripts/form.js diff --git a/public/index.php b/public/index.php index b9c9857..f8b5e1e 100644 --- a/public/index.php +++ b/public/index.php @@ -267,7 +267,7 @@ $privacy_exists = is_file($_SERVER['DOCUMENT_ROOT'] . '/static/PRIVACY.txt');
@@ -412,13 +412,7 @@ $privacy_exists = is_file($_SERVER['DOCUMENT_ROOT'] . '/static/PRIVACY.txt'); formFile.style.display = 'none'; formFile.addEventListener("change", (e) => { file = e.target.files[0]; - if (file) { - fileUploadWrapper.innerHTML = `

File: ${file.name}

`; - setFormDetailsVisiblity(true); - - fileURLWrapper.style.display = 'none'; - - } + showFile(file); }); fileUploadWrapper.addEventListener("click", () => formFile.click()); @@ -428,11 +422,7 @@ $privacy_exists = is_file($_SERVER['DOCUMENT_ROOT'] . '/static/PRIVACY.txt'); for (const item of e.dataTransfer.items) { if (item.kind === "file") { file = item.getAsFile(); - fileUploadWrapper.innerHTML = `

File: ${file.name}

`; - setFormDetailsVisiblity(true); - - fileURLWrapper.style.display = 'none'; - + showFile(file); break; } } @@ -446,15 +436,7 @@ $privacy_exists = is_file($_SERVER['DOCUMENT_ROOT'] . '/static/PRIVACY.txt'); }); fileUploadWrapper.addEventListener("dragleave", (e) => { - if (file) { - fileUploadWrapper.innerHTML = `

File: ${file.name}

`; - return; - } - fileUploadWrapper.innerHTML = '

Click, or drop files here

'; - - fileURLWrapper.style.display = 'flex'; - - + showFile(file); }); setFormDetailsVisiblity(false); @@ -505,11 +487,11 @@ $privacy_exists = is_file($_SERVER['DOCUMENT_ROOT'] . '/static/PRIVACY.txt'); fileURLWrapper.style.display = 'flex'; fileUploadWrapper.style.display = 'block'; - fileUploadWrapper.innerHTML = '

Click, or drop files here

'; + fileUploadWrapper.innerHTML = '

Click, drop, or paste files here

'; }) .then((r) => r.json()) .then((json) => { - fileUploadWrapper.innerHTML = '

Click, or drop files here

'; + fileUploadWrapper.innerHTML = '

Click, drop, or paste files here

'; fileURLWrapper.style.display = 'flex'; @@ -529,7 +511,7 @@ $privacy_exists = is_file($_SERVER['DOCUMENT_ROOT'] . '/static/PRIVACY.txt'); files.unshift(json.data); localStorage.setItem('uploaded_files', JSON.stringify(files)); - formUpload.clear(); + formUpload.reset(); }); } @@ -662,6 +644,7 @@ $privacy_exists = is_file($_SERVER['DOCUMENT_ROOT'] . '/static/PRIVACY.txt'); formSubmitButton.style.display = show ? 'block' : 'none'; } + \ No newline at end of file diff --git a/public/static/scripts/form.js b/public/static/scripts/form.js new file mode 100644 index 0000000..6e4f608 --- /dev/null +++ b/public/static/scripts/form.js @@ -0,0 +1,29 @@ +document.onpaste = () => { + var items = (event.clipboardData || event.originalEvent.clipboardData).items; + + for (index in items) { + var item = items[index]; + if (item.kind === 'file') { + file = item.getAsFile(); + showFile(file); + } + } +}; + +function showFile(file) { + setFormDetailsVisiblity(file != null); + + if (file == null) { + fileUploadWrapper.innerHTML = '

Click, drop, or paste files here

'; + + if (fileURLWrapper) { + fileURLWrapper.style.display = 'flex'; + } + } else { + fileUploadWrapper.innerHTML = `

File: ${file.name}

`; + + if (fileURLWrapper) { + fileURLWrapper.style.display = 'none'; + } + } +} \ No newline at end of file -- cgit v1.2.3