boffire commited on
Commit
50427e0
·
verified ·
1 Parent(s): 9874434

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -34
app.py CHANGED
@@ -554,7 +554,7 @@ HTML_TEMPLATE = """
554
  </div>
555
  <ul class="translation-list">
556
  {% for trans in marian %}
557
- <li class="translation-item" onclick="selectTranslation(this, {{ trans | tojson }})">
558
  <span class="translation-text">{{ trans }}</span>
559
  <svg class="check-icon" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3">
560
  <polyline points="20 6 9 17 4 12"></polyline>
@@ -584,7 +584,7 @@ HTML_TEMPLATE = """
584
  <div class="variant-grid">
585
  {% for variant_name, variant_data in libre.items() %}
586
  {% if variant_data.success %}
587
- <div class="variant-item" onclick="selectVariant(this, {{ variant_data.text | tojson }})">
588
  <div class="variant-header">
589
  <span class="variant-name">{{ variant_name }}</span>
590
  <span class="variant-code">{{ variant_data.code }}</span>
@@ -624,27 +624,7 @@ HTML_TEMPLATE = """
624
  <div class="toast" id="toast">Copied to clipboard!</div>
625
 
626
  <script>
627
- let selectedText = '';
628
-
629
- function selectTranslation(element, text) {
630
- element.parentElement.querySelectorAll('.translation-item').forEach(item => {
631
- item.classList.remove('selected');
632
- });
633
- element.classList.add('selected');
634
- selectedText = text;
635
- copyText(text, true);
636
- }
637
-
638
- function selectVariant(element, text) {
639
- document.querySelectorAll('.variant-item').forEach(item => {
640
- item.classList.remove('selected');
641
- });
642
- element.classList.add('selected');
643
- selectedText = text;
644
- copyText(text, true);
645
- }
646
-
647
- async function copyText(text, showToast = true) {
648
  let copied = false;
649
 
650
  if (navigator.clipboard && window.isSecureContext) {
@@ -672,12 +652,7 @@ HTML_TEMPLATE = """
672
  }
673
  }
674
 
675
- if (showToast) {
676
- showToastMessage(
677
- copied ? 'Copied to clipboard!' : 'Copy failed — please copy manually',
678
- !copied
679
- );
680
- }
681
  }
682
 
683
  function showToastMessage(message, isError = false) {
@@ -688,11 +663,44 @@ HTML_TEMPLATE = """
688
  setTimeout(() => toast.classList.remove('show'), 2000);
689
  }
690
 
691
- document.getElementById('translateForm').addEventListener('submit', function() {
692
- const btn = document.getElementById('submitBtn');
693
- btn.disabled = true;
694
- btn.innerHTML = '<span class="loading"></span> Translating...';
695
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
696
  </script>
697
  </body>
698
  </html>
 
554
  </div>
555
  <ul class="translation-list">
556
  {% for trans in marian %}
557
+ <li class="translation-item" data-text="{{ trans }}" data-kind="translation">
558
  <span class="translation-text">{{ trans }}</span>
559
  <svg class="check-icon" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="3">
560
  <polyline points="20 6 9 17 4 12"></polyline>
 
584
  <div class="variant-grid">
585
  {% for variant_name, variant_data in libre.items() %}
586
  {% if variant_data.success %}
587
+ <div class="variant-item" data-text="{{ variant_data.text }}" data-kind="variant">
588
  <div class="variant-header">
589
  <span class="variant-name">{{ variant_name }}</span>
590
  <span class="variant-code">{{ variant_data.code }}</span>
 
624
  <div class="toast" id="toast">Copied to clipboard!</div>
625
 
626
  <script>
627
+ async function copyText(text) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
628
  let copied = false;
629
 
630
  if (navigator.clipboard && window.isSecureContext) {
 
652
  }
653
  }
654
 
655
+ return copied;
 
 
 
 
 
656
  }
657
 
658
  function showToastMessage(message, isError = false) {
 
663
  setTimeout(() => toast.classList.remove('show'), 2000);
664
  }
665
 
666
+ function setupClickToCopy() {
667
+ // MarianMT translation items
668
+ document.querySelectorAll('.translation-item[data-text]').forEach(item => {
669
+ item.addEventListener('click', async function() {
670
+ document.querySelectorAll('.translation-item').forEach(el => el.classList.remove('selected'));
671
+ this.classList.add('selected');
672
+ const copied = await copyText(this.dataset.text);
673
+ showToastMessage(
674
+ copied ? 'Copied to clipboard!' : 'Copy failed — please copy manually',
675
+ !copied
676
+ );
677
+ });
678
+ });
679
+
680
+ // LibreTranslate variant items (error ones have no data-text, so they're skipped)
681
+ document.querySelectorAll('.variant-item[data-text]').forEach(item => {
682
+ item.addEventListener('click', async function() {
683
+ document.querySelectorAll('.variant-item').forEach(el => el.classList.remove('selected'));
684
+ this.classList.add('selected');
685
+ const copied = await copyText(this.dataset.text);
686
+ showToastMessage(
687
+ copied ? 'Copied to clipboard!' : 'Copy failed — please copy manually',
688
+ !copied
689
+ );
690
+ });
691
+ });
692
+ }
693
+
694
+ document.addEventListener('DOMContentLoaded', setupClickToCopy);
695
+
696
+ const translateForm = document.getElementById('translateForm');
697
+ if (translateForm) {
698
+ translateForm.addEventListener('submit', function() {
699
+ const btn = document.getElementById('submitBtn');
700
+ btn.disabled = true;
701
+ btn.innerHTML = '<span class="loading"></span> Translating...';
702
+ });
703
+ }
704
  </script>
705
  </body>
706
  </html>