class FileUpload extends HTMLElement { connectedCallback() { this.attachShadow({ mode: 'open' }); this.shadowRoot.innerHTML = `
No file selected
`; const dropArea = this.shadowRoot.getElementById('drop-area'); const fileInput = this.shadowRoot.getElementById('file-upload'); const fileInfo = this.shadowRoot.getElementById('file-info'); ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => { dropArea.addEventListener(eventName, preventDefaults, false); }); ['dragenter', 'dragover'].forEach(eventName => { dropArea.addEventListener(eventName, highlight, false); }); ['dragleave', 'drop'].forEach(eventName => { dropArea.addEventListener(eventName, unhighlight, false); }); dropArea.addEventListener('drop', handleDrop, false); fileInput.addEventListener('change', handleFiles, false); function preventDefaults(e) { e.preventDefault(); e.stopPropagation(); } function highlight() { dropArea.classList.add('highlight'); } function unhighlight() { dropArea.classList.remove('highlight'); } function handleDrop(e) { const dt = e.dataTransfer; const files = dt.files; fileInput.files = files; handleFiles({ target: fileInput }); } function handleFiles(e) { const file = e.target.files[0]; if (file) { fileInfo.textContent = `Selected: ${file.name} (${(file.size / 1024).toFixed(1)} KB)`; const reader = new FileReader(); reader.onload = function(e) { const textarea = document.getElementById('dataset'); textarea.value = e.target.result; }; reader.readAsText(file); } else { fileInfo.textContent = 'No file selected'; } } } } customElements.define('file-upload', FileUpload);