document.addEventListener("DOMContentLoaded", () => { const dropzone = document.getElementById("dropzone"); const photoInput = document.getElementById("photoInput"); const preview = document.getElementById("preview"); const previewImg = document.getElementById("previewImg"); const previewName = document.getElementById("previewName"); const submitBtn = document.getElementById("submitBtn"); if (!dropzone || !photoInput) return; dropzone.addEventListener("click", (e) => { if (e.target.closest("button")) return; photoInput.click(); }); ["dragenter", "dragover"].forEach((event) => { dropzone.addEventListener(event, (e) => { e.preventDefault(); dropzone.classList.add("dropzone--active"); }); }); ["dragleave", "drop"].forEach((event) => { dropzone.addEventListener(event, (e) => { e.preventDefault(); dropzone.classList.remove("dropzone--active"); }); }); dropzone.addEventListener("drop", (e) => { const files = e.dataTransfer.files; if (files.length > 0) { photoInput.files = files; showPreview(files[0]); } }); photoInput.addEventListener("change", () => { if (photoInput.files.length > 0) { showPreview(photoInput.files[0]); } }); function showPreview(file) { if (!file.type.startsWith("image/")) return; const reader = new FileReader(); reader.onload = (e) => { previewImg.src = e.target.result; previewName.textContent = file.name; preview.hidden = false; submitBtn.disabled = false; }; reader.readAsDataURL(file); } document.querySelectorAll(".copy-btn").forEach((btn) => { btn.addEventListener("click", async (e) => { e.stopPropagation(); const url = btn.dataset.url; try { await navigator.clipboard.writeText(url); showToast("Ссылка скопирована!"); } catch { const input = document.createElement("input"); input.value = url; document.body.appendChild(input); input.select(); document.execCommand("copy"); document.body.removeChild(input); showToast("Ссылка скопирована!"); } }); }); }); function showToast(message) { const existing = document.querySelector(".toast"); if (existing) existing.remove(); const toast = document.createElement("div"); toast.className = "toast"; toast.textContent = message; document.body.appendChild(toast); setTimeout(() => toast.remove(), 2500); }