Spaces:
Configuration error
Configuration error
File size: 3,839 Bytes
a34906d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
document.addEventListener('DOMContentLoaded', () => {
// DOM Elements
const dropZone = document.getElementById('dropZone');
const fileInput = document.getElementById('fileInput');
const translateBtn = document.getElementById('translateBtn');
const previewSection = document.getElementById('previewSection');
const downloadBtn = document.getElementById('downloadBtn');
const previewFrame = document.getElementById('previewFrame');
const themeToggle = document.querySelector('#themeToggle');
// File handling
dropZone.addEventListener('click', () => fileInput.click());
fileInput.addEventListener('change', (e) => {
if (e.target.files.length) {
handleFileSelection(e.target.files[0]);
}
});
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
dropZone.addEventListener(eventName, preventDefaults, false);
});
function preventDefaults(e) {
e.preventDefault();
e.stopPropagation();
}
['dragenter', 'dragover'].forEach(eventName => {
dropZone.addEventListener(eventName, highlight, false);
});
['dragleave', 'drop'].forEach(eventName => {
dropZone.addEventListener(eventName, unhighlight, false);
});
function highlight() {
dropZone.classList.add('highlight');
}
function unhighlight() {
dropZone.classList.remove('highlight');
}
dropZone.addEventListener('drop', (e) => {
const dt = e.dataTransfer;
const file = dt.files[0];
if (file && file.type === 'application/pdf') {
handleFileSelection(file);
} else {
alert('Please upload a valid PDF file.');
}
});
function handleFileSelection(file) {
const fileName = file.name;
const fileSize = (file.size / (1024 * 1024)).toFixed(2); // in MB
if (fileSize > 50) {
alert('File size exceeds 50MB limit.');
return;
}
// Update UI to show selected file
dropZone.innerHTML = `
<i data-feather="file-text" class="w-12 h-12 text-primary-500 mb-4"></i>
<p class="text-gray-900 dark:text-white font-medium">${fileName}</p>
<p class="text-sm text-gray-500 dark:text-gray-400">${fileSize} MB</p>
`;
feather.replace();
}
// Translation process
translateBtn.addEventListener('click', async () => {
if (!fileInput.files.length) {
alert('Please select a PDF file first.');
return;
}
// Show loading state
translateBtn.disabled = true;
translateBtn.innerHTML = `
<i data-feather="loader" class="animate-spin mr-2"></i>
Translating...
`;
feather.replace();
// Simulate translation process
setTimeout(() => {
// In a real app, this would upload the file to a server for processing
// and then return the translated PDF
// For demo purposes, we'll just show the preview section
previewSection.classList.remove('hidden');
previewFrame.src = URL.createObjectURL(fileInput.files[0]);
// Reset button state
translateBtn.disabled = false;
translateBtn.innerHTML = `
<i data-feather="globe" class="mr-2"></i>
Translate Document
`;
feather.replace();
}, 3000);
});
// Download functionality
downloadBtn.addEventListener('click', () => {
if (previewFrame.src) {
const a = document.createElement('a');
a.href = previewFrame.src;
a.download = `translated_${fileInput.files[0].name}`;
a.click();
}
});
}); |