Spaces:
Running
Running
add
Browse files- static/script.js +150 -49
static/script.js
CHANGED
@@ -117,7 +117,7 @@ fileInput.addEventListener('change', function(event) {
|
|
117 |
<div class="main-content-area">
|
118 |
<div class="document-container">
|
119 |
<h1 class="document-main-title">Opérations sur documents</h1>
|
120 |
-
<h2 class="document-subtitle">TRANSLATE
|
121 |
<div class="upload-container">
|
122 |
<div class="file-drop-zone" id="fileDropZone">
|
123 |
<label for="translateFile" class="file-upload-label">
|
@@ -237,6 +237,15 @@ fileInput.addEventListener('change', function(event) {
|
|
237 |
<div class="results-container">
|
238 |
<div class="results-placeholder">
|
239 |
<p class="placeholder-text" id="translateResult">Le résultat apparaîtra ici...</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
240 |
</div>
|
241 |
</div>
|
242 |
</div>
|
@@ -245,86 +254,178 @@ fileInput.addEventListener('change', function(event) {
|
|
245 |
document.body.innerHTML = "";
|
246 |
document.body.appendChild(appContainer);
|
247 |
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
event.preventDefault();
|
260 |
-
event.stopPropagation();
|
261 |
-
fileInput.click();
|
262 |
-
});
|
263 |
-
|
264 |
-
uploadIcon.addEventListener('click', function(event) {
|
265 |
-
event.preventDefault();
|
266 |
-
event.stopPropagation();
|
267 |
-
fileInput.click();
|
268 |
-
});
|
269 |
-
|
270 |
-
fileInput.addEventListener('change', function(event) {
|
271 |
-
let file = event.target.files[0];
|
272 |
-
if (file) {
|
273 |
-
dropText.textContent = file.name;
|
274 |
-
uploadIcon.style.display = "none";
|
275 |
}
|
276 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
277 |
|
278 |
-
|
279 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
280 |
|
281 |
async function uploadForTranslation() {
|
282 |
-
|
283 |
-
|
|
|
284 |
|
285 |
if (!fileInput.files[0]) {
|
286 |
-
|
287 |
return;
|
288 |
}
|
289 |
|
290 |
if (!targetLang) {
|
291 |
-
|
292 |
return;
|
293 |
}
|
294 |
-
|
295 |
-
|
296 |
formData.append("file", fileInput.files[0]);
|
297 |
formData.append("target_lang", targetLang);
|
298 |
|
299 |
try {
|
300 |
-
|
301 |
-
|
302 |
method: "POST",
|
303 |
body: formData,
|
304 |
headers: { "Accept": "application/json" }
|
305 |
});
|
306 |
|
307 |
-
let responseText = await response.text();
|
308 |
-
console.log("📤 Réponse brute du serveur :", responseText);
|
309 |
-
|
310 |
if (!response.ok) {
|
311 |
-
throw new Error("Erreur de requête
|
312 |
}
|
313 |
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
let resultElement = document.getElementById("translateResult");
|
318 |
if (data.translated_text) {
|
319 |
resultElement.innerText = "📝 Traduction : " + data.translated_text;
|
|
|
|
|
|
|
|
|
320 |
} else {
|
321 |
resultElement.innerText = "❌ Traduction non disponible.";
|
322 |
}
|
323 |
} catch (error) {
|
324 |
-
console.error("
|
325 |
-
|
326 |
}
|
327 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
328 |
}
|
329 |
function loadDocPage() {
|
330 |
document.body.style.setProperty('--background-image', "url('qstdoc2.webp')");
|
|
|
117 |
<div class="main-content-area">
|
118 |
<div class="document-container">
|
119 |
<h1 class="document-main-title">Opérations sur documents</h1>
|
120 |
+
<h2 class="document-subtitle">TRANSLATE</h2>
|
121 |
<div class="upload-container">
|
122 |
<div class="file-drop-zone" id="fileDropZone">
|
123 |
<label for="translateFile" class="file-upload-label">
|
|
|
237 |
<div class="results-container">
|
238 |
<div class="results-placeholder">
|
239 |
<p class="placeholder-text" id="translateResult">Le résultat apparaîtra ici...</p>
|
240 |
+
<div id="downloadSection" style="display: none; margin-top: 20px;">
|
241 |
+
<h3 style="color: white; margin-bottom: 10px;">Télécharger la traduction</h3>
|
242 |
+
<div style="display: flex; gap: 10px;">
|
243 |
+
<button id="downloadPdf" class="download-btn">PDF</button>
|
244 |
+
<button id="downloadWord" class="download-btn">Word</button>
|
245 |
+
<button id="downloadPpt" class="download-btn">PowerPoint</button>
|
246 |
+
<button id="downloadTxt" class="download-btn">Texte</button>
|
247 |
+
</div>
|
248 |
+
</div>
|
249 |
</div>
|
250 |
</div>
|
251 |
</div>
|
|
|
254 |
document.body.innerHTML = "";
|
255 |
document.body.appendChild(appContainer);
|
256 |
|
257 |
+
// Chargement des librairies externes
|
258 |
+
loadExternalLibs();
|
259 |
+
|
260 |
+
// Gestion des événements
|
261 |
+
setupEventListeners();
|
262 |
+
|
263 |
+
function loadExternalLibs() {
|
264 |
+
if (!window.jspdf) {
|
265 |
+
const script = document.createElement('script');
|
266 |
+
script.src = 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js';
|
267 |
+
document.head.appendChild(script);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
268 |
}
|
269 |
+
if (!window.docx) {
|
270 |
+
const script = document.createElement('script');
|
271 |
+
script.src = 'https://cdnjs.cloudflare.com/ajax/libs/docx/7.8.2/docx.min.js';
|
272 |
+
document.head.appendChild(script);
|
273 |
+
}
|
274 |
+
if (!window.PptxGenJS) {
|
275 |
+
const script = document.createElement('script');
|
276 |
+
script.src = 'https://cdnjs.cloudflare.com/ajax/libs/PptxGenJS/3.11.0/pptxgen.min.js';
|
277 |
+
document.head.appendChild(script);
|
278 |
+
}
|
279 |
+
}
|
280 |
|
281 |
+
function setupEventListeners() {
|
282 |
+
// Navigation
|
283 |
+
document.getElementById('resumerButton').addEventListener('click', loadResumerPage);
|
284 |
+
document.getElementById('qesdocButton').addEventListener('click', loadDocPage);
|
285 |
+
document.getElementById('visualisationButton').addEventListener('click', loadVisualisationPage);
|
286 |
+
document.getElementById('interpretationButton').addEventListener('click', loadInterpretationPage);
|
287 |
+
document.getElementById('qesimgButton').addEventListener('click', loadImagePage);
|
288 |
+
|
289 |
+
// Gestion des fichiers
|
290 |
+
const fileInput = document.getElementById('translateFile');
|
291 |
+
const dropText = document.getElementById('dropText');
|
292 |
+
const uploadIcon = document.getElementById('uploadIcon');
|
293 |
+
|
294 |
+
dropText.addEventListener('click', (e) => {
|
295 |
+
e.preventDefault();
|
296 |
+
fileInput.click();
|
297 |
+
});
|
298 |
+
|
299 |
+
uploadIcon.addEventListener('click', (e) => {
|
300 |
+
e.preventDefault();
|
301 |
+
fileInput.click();
|
302 |
+
});
|
303 |
+
|
304 |
+
fileInput.addEventListener('change', (e) => {
|
305 |
+
if (e.target.files[0]) {
|
306 |
+
dropText.textContent = e.target.files[0].name;
|
307 |
+
uploadIcon.style.display = "none";
|
308 |
+
}
|
309 |
+
});
|
310 |
+
|
311 |
+
// Traduction
|
312 |
+
document.getElementById('translateButton').addEventListener('click', uploadForTranslation);
|
313 |
+
|
314 |
+
// Téléchargements
|
315 |
+
document.getElementById('downloadPdf').addEventListener('click', downloadAsPdf);
|
316 |
+
document.getElementById('downloadWord').addEventListener('click', downloadAsWord);
|
317 |
+
document.getElementById('downloadPpt').addEventListener('click', downloadAsPowerPoint);
|
318 |
+
document.getElementById('downloadTxt').addEventListener('click', downloadAsText);
|
319 |
+
}
|
320 |
|
321 |
async function uploadForTranslation() {
|
322 |
+
const fileInput = document.getElementById("translateFile");
|
323 |
+
const targetLang = document.getElementById("targetLanguageSelect").value;
|
324 |
+
const resultElement = document.getElementById("translateResult");
|
325 |
|
326 |
if (!fileInput.files[0]) {
|
327 |
+
resultElement.innerText = "Veuillez sélectionner un fichier";
|
328 |
return;
|
329 |
}
|
330 |
|
331 |
if (!targetLang) {
|
332 |
+
resultElement.innerText = "Veuillez sélectionner une langue";
|
333 |
return;
|
334 |
}
|
335 |
+
|
336 |
+
const formData = new FormData();
|
337 |
formData.append("file", fileInput.files[0]);
|
338 |
formData.append("target_lang", targetLang);
|
339 |
|
340 |
try {
|
341 |
+
resultElement.innerText = "Traduction en cours...";
|
342 |
+
const response = await fetch("/translate/", {
|
343 |
method: "POST",
|
344 |
body: formData,
|
345 |
headers: { "Accept": "application/json" }
|
346 |
});
|
347 |
|
|
|
|
|
|
|
348 |
if (!response.ok) {
|
349 |
+
throw new Error("Erreur de requête: " + await response.text());
|
350 |
}
|
351 |
|
352 |
+
const data = await response.json();
|
353 |
+
|
|
|
|
|
354 |
if (data.translated_text) {
|
355 |
resultElement.innerText = "📝 Traduction : " + data.translated_text;
|
356 |
+
// Stocker le texte traduit pour le téléchargement
|
357 |
+
resultElement.dataset.translatedText = data.translated_text;
|
358 |
+
// Afficher les options de téléchargement
|
359 |
+
document.getElementById("downloadSection").style.display = "block";
|
360 |
} else {
|
361 |
resultElement.innerText = "❌ Traduction non disponible.";
|
362 |
}
|
363 |
} catch (error) {
|
364 |
+
console.error("Erreur:", error);
|
365 |
+
resultElement.innerText = "Échec de la traduction: " + error.message;
|
366 |
}
|
367 |
}
|
368 |
+
|
369 |
+
function downloadAsPdf() {
|
370 |
+
const text = document.getElementById("translateResult").dataset.translatedText;
|
371 |
+
if (!text) return;
|
372 |
+
|
373 |
+
const { jsPDF } = window.jspdf;
|
374 |
+
const doc = new jsPDF();
|
375 |
+
doc.text(text, 10, 10);
|
376 |
+
doc.save('traduction.pdf');
|
377 |
+
}
|
378 |
+
|
379 |
+
async function downloadAsWord() {
|
380 |
+
const text = document.getElementById("translateResult").dataset.translatedText;
|
381 |
+
if (!text) return;
|
382 |
+
|
383 |
+
const { Document, Paragraph, TextRun, Packer } = window.docx;
|
384 |
+
const doc = new Document({
|
385 |
+
sections: [{
|
386 |
+
properties: {},
|
387 |
+
children: [
|
388 |
+
new Paragraph({
|
389 |
+
children: [
|
390 |
+
new TextRun(text)
|
391 |
+
]
|
392 |
+
})
|
393 |
+
]
|
394 |
+
}]
|
395 |
+
});
|
396 |
+
|
397 |
+
Packer.toBlob(doc).then(blob => {
|
398 |
+
const url = URL.createObjectURL(blob);
|
399 |
+
const a = document.createElement('a');
|
400 |
+
a.href = url;
|
401 |
+
a.download = 'traduction.docx';
|
402 |
+
a.click();
|
403 |
+
URL.revokeObjectURL(url);
|
404 |
+
});
|
405 |
+
}
|
406 |
+
|
407 |
+
function downloadAsPowerPoint() {
|
408 |
+
const text = document.getElementById("translateResult").dataset.translatedText;
|
409 |
+
if (!text) return;
|
410 |
+
|
411 |
+
const pptx = new window.PptxGenJS();
|
412 |
+
const slide = pptx.addSlide();
|
413 |
+
slide.addText(text, { x: 1, y: 1, w: 8, h: 4 });
|
414 |
+
pptx.writeFile({ fileName: 'traduction.pptx' });
|
415 |
+
}
|
416 |
+
|
417 |
+
function downloadAsText() {
|
418 |
+
const text = document.getElementById("translateResult").dataset.translatedText;
|
419 |
+
if (!text) return;
|
420 |
+
|
421 |
+
const blob = new Blob([text], { type: 'text/plain' });
|
422 |
+
const url = URL.createObjectURL(blob);
|
423 |
+
const a = document.createElement('a');
|
424 |
+
a.href = url;
|
425 |
+
a.download = 'traduction.txt';
|
426 |
+
a.click();
|
427 |
+
URL.revokeObjectURL(url);
|
428 |
+
}
|
429 |
}
|
430 |
function loadDocPage() {
|
431 |
document.body.style.setProperty('--background-image', "url('qstdoc2.webp')");
|