amine_dubs commited on
Commit
0148163
·
1 Parent(s): abc6641
Files changed (2) hide show
  1. static/script.js +39 -11
  2. static/style.css +21 -9
static/script.js CHANGED
@@ -85,15 +85,22 @@ window.onload = function() {
85
 
86
  // Quick phrases implementation
87
  if (phraseButtons && phraseButtons.length > 0) {
 
88
  phraseButtons.forEach(button => {
89
  button.addEventListener('click', function(e) {
90
  e.preventDefault();
 
91
  const phrase = this.getAttribute('data-text') || this.getAttribute('data-phrase');
92
 
93
  if (phrase && textInput) {
94
- // If not on the text tab, switch to it
 
95
  if (textSection.classList.contains('hidden')) {
96
- textTabLink.click();
 
 
 
 
97
  }
98
 
99
  // Insert the phrase at cursor position, or append to end
@@ -565,13 +572,14 @@ window.onload = function() {
565
 
566
  // Function to download translated document
567
  function downloadTranslatedDocument(content, fileName, fileType) {
 
568
  // Determine the file extension
569
  let extension = '';
570
- if (fileName.endsWith('.pdf')) {
571
  extension = '.pdf';
572
- } else if (fileName.endsWith('.docx')) {
573
  extension = '.docx';
574
- } else if (fileName.endsWith('.txt')) {
575
  extension = '.txt';
576
  } else {
577
  extension = '.txt'; // Default to txt
@@ -581,8 +589,10 @@ window.onload = function() {
581
  const baseName = fileName.substring(0, fileName.lastIndexOf('.'));
582
  const translatedFileName = `${baseName}_translated${extension}`;
583
 
584
- // For simplicity, we'll handle text downloads here
585
- // For PDF and DOCX, we would need server-side processing
 
 
586
  if (extension === '.txt') {
587
  const blob = new Blob([content], { type: 'text/plain' });
588
  const url = URL.createObjectURL(blob);
@@ -594,8 +604,10 @@ window.onload = function() {
594
  a.click();
595
  document.body.removeChild(a);
596
  URL.revokeObjectURL(url);
 
 
597
  } else {
598
- // For non-text files, we need to request a download from the server
599
  fetch('/download/translated-document', {
600
  method: 'POST',
601
  headers: {
@@ -604,17 +616,31 @@ window.onload = function() {
604
  body: JSON.stringify({
605
  content: content,
606
  filename: translatedFileName,
607
- original_type: fileType
608
  }),
609
  })
610
  .then(response => {
611
  if (!response.ok) {
612
- throw new Error('Failed to generate document for download');
613
  }
614
  return response.blob();
615
  })
616
  .then(blob => {
617
- const url = URL.createObjectURL(blob);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
618
  const a = document.createElement('a');
619
  a.href = url;
620
  a.download = translatedFileName;
@@ -622,6 +648,8 @@ window.onload = function() {
622
  a.click();
623
  document.body.removeChild(a);
624
  URL.revokeObjectURL(url);
 
 
625
  })
626
  .catch(error => {
627
  console.error('Error downloading document:', error);
 
85
 
86
  // Quick phrases implementation
87
  if (phraseButtons && phraseButtons.length > 0) {
88
+ console.log('Found phrase buttons:', phraseButtons.length);
89
  phraseButtons.forEach(button => {
90
  button.addEventListener('click', function(e) {
91
  e.preventDefault();
92
+ console.log('Phrase button clicked');
93
  const phrase = this.getAttribute('data-text') || this.getAttribute('data-phrase');
94
 
95
  if (phrase && textInput) {
96
+ console.log('Using phrase:', phrase);
97
+ // First ensure text section is visible
98
  if (textSection.classList.contains('hidden')) {
99
+ console.log('Switching to text tab');
100
+ docSection.classList.add('hidden');
101
+ textSection.classList.remove('hidden');
102
+ textTabLink.parentElement.classList.add('active');
103
+ docTabLink.parentElement.classList.remove('active');
104
  }
105
 
106
  // Insert the phrase at cursor position, or append to end
 
572
 
573
  // Function to download translated document
574
  function downloadTranslatedDocument(content, fileName, fileType) {
575
+ console.log('Downloading translated document:', fileName, fileType);
576
  // Determine the file extension
577
  let extension = '';
578
+ if (fileName.toLowerCase().endsWith('.pdf')) {
579
  extension = '.pdf';
580
+ } else if (fileName.toLowerCase().endsWith('.docx')) {
581
  extension = '.docx';
582
+ } else if (fileName.toLowerCase().endsWith('.txt')) {
583
  extension = '.txt';
584
  } else {
585
  extension = '.txt'; // Default to txt
 
589
  const baseName = fileName.substring(0, fileName.lastIndexOf('.'));
590
  const translatedFileName = `${baseName}_translated${extension}`;
591
 
592
+ // Show notification that download is starting
593
+ showNotification('Preparing document for download...');
594
+
595
+ // For text files, we can download directly from the browser
596
  if (extension === '.txt') {
597
  const blob = new Blob([content], { type: 'text/plain' });
598
  const url = URL.createObjectURL(blob);
 
604
  a.click();
605
  document.body.removeChild(a);
606
  URL.revokeObjectURL(url);
607
+
608
+ showNotification('Document downloaded successfully!');
609
  } else {
610
+ // For PDF and DOCX files, we need the server to create them
611
  fetch('/download/translated-document', {
612
  method: 'POST',
613
  headers: {
 
616
  body: JSON.stringify({
617
  content: content,
618
  filename: translatedFileName,
619
+ original_type: fileType || 'text/plain'
620
  }),
621
  })
622
  .then(response => {
623
  if (!response.ok) {
624
+ throw new Error(`Server error: ${response.status} ${response.statusText}`);
625
  }
626
  return response.blob();
627
  })
628
  .then(blob => {
629
+ // Create appropriate MIME type based on extension
630
+ let mimeType;
631
+ if (extension === '.pdf') {
632
+ mimeType = 'application/pdf';
633
+ } else if (extension === '.docx') {
634
+ mimeType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
635
+ } else {
636
+ mimeType = 'text/plain';
637
+ }
638
+
639
+ // Create a blob with the correct MIME type
640
+ const fileBlob = new Blob([blob], { type: mimeType });
641
+ const url = URL.createObjectURL(fileBlob);
642
+
643
+ // Create and trigger download link
644
  const a = document.createElement('a');
645
  a.href = url;
646
  a.download = translatedFileName;
 
648
  a.click();
649
  document.body.removeChild(a);
650
  URL.revokeObjectURL(url);
651
+
652
+ showNotification('Document downloaded successfully!');
653
  })
654
  .catch(error => {
655
  console.error('Error downloading document:', error);
static/style.css CHANGED
@@ -326,14 +326,17 @@ textarea#text-input:focus {
326
 
327
  /* Translate button for documents - default transparent until file is selected */
328
  #doc-translation-form .translate-button {
329
- background-color: rgba(66, 133, 244, 0.5);
330
- opacity: 0.7;
331
  transition: all 0.3s ease;
 
332
  }
333
 
334
  #doc-translation-form .translate-button.active-button {
335
  background-color: #4285f4;
336
  opacity: 1;
 
 
337
  }
338
 
339
  /* Document upload area */
@@ -344,6 +347,7 @@ textarea#text-input:focus {
344
  text-align: center;
345
  margin: 2rem 0;
346
  transition: border-color 0.3s, background-color 0.3s;
 
347
  }
348
 
349
  .file-upload-area:hover {
@@ -374,15 +378,17 @@ input.file-input {
374
  display: none;
375
  }
376
 
 
377
  #file-name-display {
378
  margin-top: 1rem;
379
- font-size: 0.9rem;
380
- color: #4285f4;
381
- font-weight: 500;
382
- padding: 0.5rem;
383
  background-color: #e8f0fe;
384
  border-radius: 4px;
 
 
385
  display: none;
 
 
386
  }
387
 
388
  .document-result-area {
@@ -404,7 +410,7 @@ input.file-input {
404
  color: #777;
405
  }
406
 
407
- /* Download button */
408
  .download-button {
409
  background-color: #34A853;
410
  color: white;
@@ -413,15 +419,21 @@ input.file-input {
413
  padding: 0.8rem 1.5rem;
414
  font-size: 1rem;
415
  cursor: pointer;
416
- transition: background-color 0.3s;
417
  display: flex;
418
  align-items: center;
 
419
  gap: 0.5rem;
420
  margin: 1.5rem auto;
 
 
 
421
  }
422
 
423
  .download-button:hover {
424
- background-color: #2d8e47;
 
 
425
  }
426
 
427
  .download-button i {
 
326
 
327
  /* Translate button for documents - default transparent until file is selected */
328
  #doc-translation-form .translate-button {
329
+ opacity: 0.6;
330
+ background-color: rgba(66, 133, 244, 0.8);
331
  transition: all 0.3s ease;
332
+ cursor: not-allowed;
333
  }
334
 
335
  #doc-translation-form .translate-button.active-button {
336
  background-color: #4285f4;
337
  opacity: 1;
338
+ cursor: pointer;
339
+ box-shadow: 0 2px 5px rgba(0,0,0,0.2);
340
  }
341
 
342
  /* Document upload area */
 
347
  text-align: center;
348
  margin: 2rem 0;
349
  transition: border-color 0.3s, background-color 0.3s;
350
+ position: relative;
351
  }
352
 
353
  .file-upload-area:hover {
 
378
  display: none;
379
  }
380
 
381
+ /* File name display - improved styling */
382
  #file-name-display {
383
  margin-top: 1rem;
384
+ padding: 0.7rem 1rem;
 
 
 
385
  background-color: #e8f0fe;
386
  border-radius: 4px;
387
+ color: #1a73e8;
388
+ font-weight: 500;
389
  display: none;
390
+ margin-bottom: 1rem;
391
+ border-left: 3px solid #4285f4;
392
  }
393
 
394
  .document-result-area {
 
410
  color: #777;
411
  }
412
 
413
+ /* Download button - enhanced styling */
414
  .download-button {
415
  background-color: #34A853;
416
  color: white;
 
419
  padding: 0.8rem 1.5rem;
420
  font-size: 1rem;
421
  cursor: pointer;
422
+ transition: all 0.3s ease;
423
  display: flex;
424
  align-items: center;
425
+ justify-content: center;
426
  gap: 0.5rem;
427
  margin: 1.5rem auto;
428
+ width: fit-content;
429
+ min-width: 200px;
430
+ box-shadow: 0 2px 5px rgba(0,0,0,0.15);
431
  }
432
 
433
  .download-button:hover {
434
+ background-color: #2a8a46;
435
+ box-shadow: 0 3px 8px rgba(0,0,0,0.2);
436
+ transform: translateY(-1px);
437
  }
438
 
439
  .download-button i {