Grammalecte  Diff

Differences From Artifact [2282aa660c]:

To Artifact [505aead2d8]:


55
56
57
58
59
60
61
62
63

64
65
66
67
68
69
70
55
56
57
58
59
60
61


62
63
64
65
66
67
68
69







-
-
+







        // Editor
        this.xGCPanelContent = oGrammalecte.createNode("div", {id: "grammalecte_gc_panel_content"});
        this.xParagraphList = oGrammalecte.createNode("div", {id: "grammalecte_paragraph_list"});
        this.xGCPanelContent.appendChild(this.xParagraphList);
        this.xPanelContent.addEventListener("click", onGrammalecteGCPanelClick, false);
        this.oTooltip = new GrammalecteTooltip(this.xParent, this.xGCPanelContent);
        this.xPanelContent.appendChild(this.xGCPanelContent);
        this.xNode = null;
        this.oTextControl = new GrammalecteTextControl();
        this.oTextControl = null;
        this.nLastResult = 0;
        this.iLastEditedParagraph = -1;
        // Lexicographer
        this.nLxgCount = 0;
        this.xLxgPanelContent = oGrammalecte.createNode("div", {id: "grammalecte_lxg_panel_content"});
        this.xPanelContent.appendChild(this.xLxgPanelContent);
        // Conjugueur
98
99
100
101
102
103
104




105
106
107
108
109
110
111
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114







+
+
+
+







        };
        this.xEditorButton.onclick = () => {
            if (!this.bWorking) {
                this.showEditor();
            }
        };
        this.xAutoRefresh.onclick = () => {
            if (bThunderbird) {
                oGrammalecte.showMessage("À cause d’une limitation de Thunberbird, l’auto-rafraîchissement est indisponible. Si vous modifiez le texte dans ce panneau, cliquez sur le bouton ↻ pour relancer l’analyse grammaticale du paragraphe.")
                return;
            }
            this.bAutoRefresh = !this.bAutoRefresh;
            oGrammalecte.bAutoRefresh = this.bAutoRefresh;
            browser.storage.local.set({"autorefresh_option": this.bAutoRefresh});
            this.setAutoRefreshButton();
        }
        this.xLxgButton.onclick = () => {
            if (!this.bWorking) {
134
135
136
137
138
139
140
141
142


143
144
145

146
147
148


149
150

151
152
153
154
155
156
157
137
138
139
140
141
142
143


144
145



146
147


148
149


150
151
152
153
154
155
156
157







-
-
+
+
-
-
-
+

-
-
+
+
-
-
+








    start (what, xResultNode=null) {
        this.oTooltip.hide();
        this.bWorking = false;
        this.clear();
        this.hideMessage();
        this.resetTimer();
        if (typeof(what) === "string") {
            // text
        if (typeof(what) === "string" && what === "__ThunderbirdComposeWindow__") {
            // Thunderbird compose window
            this.xNode = null;
            this.oTextControl.setResultNode(xResultNode);
            this.oTextControl.setText(what);
            this.oTextControl = new HTMLPageEditor(document);
        }
        else if (what.nodeType && what.nodeType === 1) { // 1 = Node.ELEMENT_NODE
            // node
        else if (typeof(what) === "string" || (what.nodeType && what.nodeType === 1)) {
            // Text or node
            this.xNode = what;
            this.oTextControl.setNode(this.xNode);
            this.oTextControl = new TextNodeEditor(what, xResultNode);
        }
        else {
            // error
            oGrammalecte.oMessageBox.showMessage("[BUG] Analyse d’un élément inconnu…");
            console.log("[Grammalecte] Unknown element:", what);
        }
    }
240
241
242
243
244
245
246
247

248
249
250
251
252
253
254
255
240
241
242
243
244
245
246

247

248
249
250
251
252
253
254







-
+
-







                    if (this.bAutoRefresh) {
                        // timer for refreshing analysis
                        window.clearTimeout(parseInt(xParagraph.dataset.timer_id, 10));
                        xParagraph.dataset.timer_id = window.setTimeout(this.recheckParagraph.bind(this), 3000, oResult.iParaNum);
                        this.iLastEditedParagraph = oResult.iParaNum;
                    }
                    // write text
                    this.oTextControl.setParagraph(parseInt(xEvent.target.dataset.para_num, 10), this.purgeText(xEvent.target.textContent));
                    this.oTextControl.setParagraph(parseInt(xEvent.target.dataset.para_num, 10), xEvent.target.textContent);
                    this.oTextControl.write();
                }.bind(this)
                , true);
                this._tagParagraph(xParagraph, oResult.sParagraph, oResult.iParaNum, oResult.aGrammErr, oResult.aSpellErr);
                // creation
                xNodeDiv.appendChild(xActionsBar);
                xNodeDiv.appendChild(xParagraph);
                this.xParagraphList.appendChild(xNodeDiv);
279
280
281
282
283
284
285


286


287
288
289
290
291
292
293
278
279
280
281
282
283
284
285
286

287
288
289
290
291
292
293
294
295







+
+
-
+
+







    recheckParagraph (iParaNum) {
        if (!this.bOpened) {
            return;
        }
        let sParagraphId = "grammalecte_paragraph" + iParaNum;
        let xParagraph = this.xParent.getElementById(sParagraphId);
        this._blockParagraph(xParagraph);
        if (bThunderbird) {
            // WORKAROUND: input event isn’t triggered by key input, so as textContent isn’t up to date, we do it now
        //let sText = this.purgeText(xParagraph.textContent);
            this.oTextControl.setParagraph(iParaNum, xParagraph.textContent);
        }
        let sText = this.oTextControl.getParagraph(iParaNum);
        oGrammalecteBackgroundPort.parseAndSpellcheck1(sText, "__GrammalectePanel__", sParagraphId);
    }

    refreshParagraph (sParagraphId, oResult) {
        // function called when results are sent by the Worker
        if (!this.bOpened) {
410
411
412
413
414
415
416
417

418
419
420
421
422
423
424
425
412
413
414
415
416
417
418

419

420
421
422
423
424
425
426







-
+
-







            let sErrorId = this.xParent.getElementById(sNodeSuggId).dataset.error_id;
            //let sParaNum = sErrorId.slice(0, sErrorId.indexOf("-"));
            let xNodeErr = this.xParent.getElementById("grammalecte_err" + sErrorId);
            xNodeErr.textContent = this.xParent.getElementById(sNodeSuggId).textContent;
            xNodeErr.className = "grammalecte_error_corrected";
            xNodeErr.removeAttribute("style");
            let iParaNum = parseInt(sErrorId.slice(0, sErrorId.indexOf("-")), 10);
            this.oTextControl.setParagraph(iParaNum, this.purgeText(this.xParent.getElementById("grammalecte_paragraph" + iParaNum).textContent));
            this.oTextControl.setParagraph(iParaNum, this.xParent.getElementById("grammalecte_paragraph" + iParaNum).textContent);
            this.oTextControl.write();
            this.oTooltip.hide();
            this.recheckParagraph(iParaNum);
            this.iLastEditedParagraph = iParaNum;
        }
        catch (e) {
            showError(e);
        }
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
436
437
438
439
440
441
442




443
444
445
446
447
448
449







-
-
-
-







            this.oTooltip.hide();
        }
        catch (e) {
            showError(e);
        }
    }

    purgeText (sText) {
        return sText.replace(/&nbsp;/g, " ").replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&amp;/g, "&");
    }

    addSummary () {
        // todo
    }

    addMessageToGCPanel (sMessage) {
        let xNode = oGrammalecte.createNode("div", {className: "grammalecte_panel_flow_message", textContent: sMessage});
        this.xParagraphList.appendChild(xNode);
475
476
477
478
479
480
481
482

483
484
485
486
487
488
489
472
473
474
475
476
477
478

479
480
481
482
483
484
485
486







-
+







            // Firefox 63+, Chrome 66+
            // Working draft: https://developer.mozilla.org/en-US/docs/Web/API/Clipboard
            navigator.clipboard.writeText(sText)
            .then(
                (res) => { window.setTimeout(() => { this.xClipboardButton.textContent = "📋"; }, 2000); }
            )
            .catch(
                (e) => { console.error(e); this._sendTextToClipboard(sText); }
                (e) => { showError(e); this._sendTextToClipboard(sText); }
            );
        } else {
            this._sendTextToClipboardFallback(sText);
        }
    }

    _sendTextToClipboardFallback (sText) {
498
499
500
501
502
503
504
505

506
507
508
509
510
511
512
495
496
497
498
499
500
501

502
503
504
505
506
507
508
509







-
+







                xEvent.clipboardData.setData("text/plain", sText);
            }
            document.addEventListener("copy", setClipboardData, true);
            document.execCommand("copy");
            window.setTimeout(() => { this.xClipboardButton.textContent = "📋"; }, 2000);
        }
        catch (e) {
            console.error(e);
            showError(e);
        }
    }

    // Lexicographer

    clearLexicographer () {
        this.nLxgCount = 0;
592
593
594
595
596
597
598
599
600
601
602
603





604
605
606
607













































608
609
610

611

612
613
614
615
616
617





618
619
620
621
622
623


624
625
626
627
628
629
630






631
632
633

634
635
636
637
638
639
640

641
642
643
644
645

646
647
648
649
650
651
652
653
654





655
656
657
658
659
660
661
662
663
664

665
666
667

668
669
670


671
672
673


674
675
676
677

678
679
680

681
682
683
684
685


686
687
688
689



690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706

707
708
709
710
711
712
713
589
590
591
592
593
594
595





596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653

654
655





656
657
658
659
660
661
662
663
664


665
666
667
668





669
670
671
672
673
674
675
676

677
678
679
680
681
682
683

684
685
686
687
688

689
690
691
692
693





694
695
696
697
698
699
700
701
702
703
704
705
706
707

708
709
710

711
712


713
714



715
716

717
718

719

720

721


722


723
724




725
726
727

728
729
730
731
732
733
734
735
736
737
738
739
740
741
742

743
744
745
746
747
748
749
750







-
-
-
-
-
+
+
+
+
+




+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+



+
-
+

-
-
-
-
-
+
+
+
+
+




-
-
+
+


-
-
-
-
-
+
+
+
+
+
+


-
+






-
+




-
+




-
-
-
-
-
+
+
+
+
+









-
+


-
+

-
-
+
+
-
-
-
+
+
-


-
+
-

-
+
-
-

-
-
+
+
-
-
-
-
+
+
+
-















-
+







    listenConj () {
        if (!this.bListenConj) {
            // button
            this.xParent.getElementById('grammalecte_conj_button').addEventListener("click", (e) => { this.conjugateVerb(); });
            // text field
            this.xParent.getElementById('grammalecte_conj_verb').addEventListener("change", (e) => { this.conjugateVerb(); });
            // options
            this.xParent.getElementById('grammalecte_conj_oneg').addEventListener("click", (e) => { this.updateConj(); });
            this.xParent.getElementById('grammalecte_conj_opro').addEventListener("click", (e) => { this.updateConj(); });
            this.xParent.getElementById('grammalecte_conj_oint').addEventListener("click", (e) => { this.updateConj(); });
            this.xParent.getElementById('grammalecte_conj_ofem').addEventListener("click", (e) => { this.updateConj(); });
            this.xParent.getElementById('grammalecte_conj_otco').addEventListener("click", (e) => { this.updateConj(); });
            this.xParent.getElementById('grammalecte_conj_oneg').addEventListener("click", (e) => { this.switchOption('grammalecte_conj_oneg'); this.updateConj(); });
            this.xParent.getElementById('grammalecte_conj_opro').addEventListener("click", (e) => { this.switchOption('grammalecte_conj_opro'); this.updateConj(); });
            this.xParent.getElementById('grammalecte_conj_oint').addEventListener("click", (e) => { this.switchOption('grammalecte_conj_oint'); this.updateConj(); });
            this.xParent.getElementById('grammalecte_conj_ofem').addEventListener("click", (e) => { this.switchOption('grammalecte_conj_ofem'); this.updateConj(); });
            this.xParent.getElementById('grammalecte_conj_otco').addEventListener("click", (e) => { this.switchOption('grammalecte_conj_otco'); this.updateConj(); });
            this.bListenConj = true;
        }
    }

    purgeInputText () {
        // Thunderbird don’t accept input fields
        // So we use editable node and we purge it
        let sVerb = this.xParent.getElementById('grammalecte_conj_verb').innerText;
        let nIndexCut = sVerb.indexOf("\n");
        if (nIndexCut != -1) {
            sVerb = sVerb.slice(0, nIndexCut);
        };
        this.xParent.getElementById('grammalecte_conj_verb').textContent = sVerb.trim();
    }

    switchOption (sOption) {
        if (this.xParent.getElementById(sOption).dataset.disabled == "off") {
            this.xParent.getElementById(sOption).dataset.selected = (this.xParent.getElementById(sOption).dataset.selected == "off") ? "on" : "off";
            this.xParent.getElementById(sOption).className = (this.xParent.getElementById(sOption).dataset.selected == "on") ? "grammalecte_conj_option_on" : "grammalecte_conj_option_off";
        }
    }

    resetOption (sOption) {
        this.xParent.getElementById(sOption).dataset.selected = "off";
        this.xParent.getElementById(sOption).dataset.disabled = "off";
        this.xParent.getElementById(sOption).style.color = "";
        this.xParent.getElementById(sOption).className = "grammalecte_conj_option_off";
    }

    selectOption (sOption) {
        this.xParent.getElementById(sOption).dataset.selected = "on";
        this.xParent.getElementById(sOption).className = "grammalecte_conj_option_on";
    }

    unselectOption (sOption) {
        this.xParent.getElementById(sOption).dataset.selected = "off";
        this.xParent.getElementById(sOption).className = "grammalecte_conj_option_off";
    }

    enableOption (sOption) {
        this.xParent.getElementById(sOption).dataset.disabled = "off";
        this.xParent.getElementById(sOption).style.color = "";
    }

    disableOption (sOption) {
        this.xParent.getElementById(sOption).dataset.disabled = "on";
        this.xParent.getElementById(sOption).style.color = "#CCC";
    }

    conjugateVerb (sVerb="") {
        try {
            if (!sVerb) {
                this.purgeInputText();
                sVerb = this.xParent.getElementById('grammalecte_conj_verb').value;
                sVerb = this.xParent.getElementById('grammalecte_conj_verb').textContent;
            }
            this.xParent.getElementById('grammalecte_conj_oneg').checked = false;
            this.xParent.getElementById('grammalecte_conj_opro').checked = false;
            this.xParent.getElementById('grammalecte_conj_oint').checked = false;
            this.xParent.getElementById('grammalecte_conj_otco').checked = false;
            this.xParent.getElementById('grammalecte_conj_ofem').checked = false;
            this.resetOption('grammalecte_conj_oneg');
            this.resetOption('grammalecte_conj_opro');
            this.resetOption('grammalecte_conj_oint');
            this.resetOption('grammalecte_conj_otco');
            this.resetOption('grammalecte_conj_ofem');
            // request analyzing
            sVerb = sVerb.trim().toLowerCase().replace(/’/g, "'").replace(/  +/g, " ");
            if (sVerb) {
                if (sVerb.startsWith("ne pas ")) {
                    this.xParent.getElementById('grammalecte_conj_oneg').checked = true;
                    sVerb = sVerb.slice(7);
                    this.selectOption('grammalecte_conj_oneg');
                    sVerb = sVerb.slice(7).trim();
                }
                if (sVerb.startsWith("se ")) {
                    this.xParent.getElementById('grammalecte_conj_opro').checked = true;
                    sVerb = sVerb.slice(3);
                } else if (sVerb.startsWith("s'")) {
                    this.xParent.getElementById('grammalecte_conj_opro').checked = true;
                    sVerb = sVerb.slice(2);
                    this.selectOption('grammalecte_conj_opro');
                    sVerb = sVerb.slice(3).trim();
                }
                else if (sVerb.startsWith("s'")) {
                    this.selectOption('grammalecte_conj_opro');
                    sVerb = sVerb.slice(2).trim();
                }
                if (sVerb.endsWith("?")) {
                    this.xParent.getElementById('grammalecte_conj_oint').checked = true;
                    this.selectOption('grammalecte_conj_oint');
                    sVerb = sVerb.slice(0,-1).trim();
                }
                if (sVerb) {
                    this.sVerb = sVerb;
                    this.updateConj(true);
                } else {
                    this.xParent.getElementById('grammalecte_conj_verb').value = "";
                    this.xParent.getElementById('grammalecte_conj_verb').textContent = "";
                }
            }
        }
        catch (e) {
            console.error(e.fileName + "\n" + e.name + "\nline: " + e.lineNumber + "\n" + e.message);
            showError(e);
        }
    }

    updateConj (bStart=false) {
        let bPro = this.xParent.getElementById('grammalecte_conj_opro').checked;
        let bNeg = this.xParent.getElementById('grammalecte_conj_oneg').checked;
        let bTpsCo = this.xParent.getElementById('grammalecte_conj_otco').checked;
        let bInt = this.xParent.getElementById('grammalecte_conj_oint').checked;
        let bFem = this.xParent.getElementById('grammalecte_conj_ofem').checked;
        let bPro = this.xParent.getElementById('grammalecte_conj_opro').dataset.selected == "on";
        let bNeg = this.xParent.getElementById('grammalecte_conj_oneg').dataset.selected == "on";
        let bTpsCo = this.xParent.getElementById('grammalecte_conj_otco').dataset.selected == "on";
        let bInt = this.xParent.getElementById('grammalecte_conj_oint').dataset.selected == "on";
        let bFem = this.xParent.getElementById('grammalecte_conj_ofem').dataset.selected == "on";
        if (this.sVerb) {
            oGrammalecteBackgroundPort.getVerb(this.sVerb, bStart, bPro, bNeg, bTpsCo, bInt, bFem);
        }
    }

    conjugateWith (oVerb, oConjTable) {
        // function called when results come from the Worker
        if (oVerb) {
            this.xParent.getElementById('grammalecte_conj_verb').style.color = "#999999";
            this.xParent.getElementById('grammalecte_conj_verb').value = "";
            this.xParent.getElementById('grammalecte_conj_verb').textContent = "";
            this.xParent.getElementById('grammalecte_conj_verb_title').textContent = oVerb.sVerb;
            this.xParent.getElementById('grammalecte_conj_verb_info').textContent = oVerb.sInfo;
            this.xParent.getElementById('grammalecte_conj_opro_lbl').textContent = oVerb.sProLabel;
            this.xParent.getElementById('grammalecte_conj_opro').textContent = oVerb.sProLabel;
            if (oVerb.bUncomplete) {
                this.xParent.getElementById('grammalecte_conj_opro').checked = false;
                this.xParent.getElementById('grammalecte_conj_opro').disabled = true;
                this.unselectOption('grammalecte_conj_opro');
                this.disableOption('grammalecte_conj_opro');
                this.xParent.getElementById('grammalecte_conj_opro_lbl').style.color = "#CCC";
                this.xParent.getElementById('grammalecte_conj_otco').checked = false;
                this.xParent.getElementById('grammalecte_conj_otco').disabled = true;
                this.unselectOption('grammalecte_conj_otco');
                this.disableOption('grammalecte_conj_otco');
                this.xParent.getElementById('grammalecte_conj_otco_lbl').style.color = "#CCC";
                this.xParent.getElementById('grammalecte_conj_note').textContent = "Ce verbe n’a pas encore été vérifié. C’est pourquoi les options “pronominal” et “temps composés” sont désactivées.";
            } else {
                this.xParent.getElementById('grammalecte_conj_otco').disabled = false;
                this.enableOption('grammalecte_conj_otco');
                this.xParent.getElementById('grammalecte_conj_otco_lbl').style.color = "#000";
                if (oVerb.nPronominable == 0) {
                    this.xParent.getElementById('grammalecte_conj_opro').checked = false;
                    this.enableOption('grammalecte_conj_opro');
                    this.xParent.getElementById('grammalecte_conj_opro').disabled = false;
                    this.xParent.getElementById('grammalecte_conj_opro_lbl').style.color = "#000";
                } else if (oVerb.nPronominable == 1) {
                    this.xParent.getElementById('grammalecte_conj_opro').checked = true;
                    this.xParent.getElementById('grammalecte_conj_opro').disabled = true;
                    this.selectOption('grammalecte_conj_opro');
                    this.disableOption('grammalecte_conj_opro');
                    this.xParent.getElementById('grammalecte_conj_opro_lbl').style.color = "#CCC";
                } else { // -1 or 1 or error
                    this.xParent.getElementById('grammalecte_conj_opro').checked = false;
                    this.xParent.getElementById('grammalecte_conj_opro').disabled = true;
                } else { // -1 or error
                    this.unselectOption('grammalecte_conj_opro');
                    this.disableOption('grammalecte_conj_opro');
                    this.xParent.getElementById('grammalecte_conj_opro_lbl').style.color = "#CCC";
                }
                this.xParent.getElementById('grammalecte_conj_note').textContent = "❦";
            }
            this.displayConj(oConjTable);
        } else {
            this.xParent.getElementById('grammalecte_conj_verb').style.color = "#BB4411";
        }
    }

    displayConj (oConjTable) {
        // function called when results come from the Worker
        if (oConjTable === null) {
            return;
        }
        try {
            this.xParent.getElementById('grammalecte_conj_verb').Text = "";
            this.xParent.getElementById('grammalecte_conj_verb').textContent = "";
            // infinitif
            this.xParent.getElementById('grammalecte_conj_infi').textContent = oConjTable["infi"] || " "; // something or nbsp
            // participe présent
            this.xParent.getElementById('grammalecte_conj_ppre').textContent = oConjTable["ppre"] || " ";
            // participes passés
            this.xParent.getElementById('grammalecte_conj_ppas1').textContent = oConjTable["ppas1"] || " ";
            this.xParent.getElementById('grammalecte_conj_ppas2').textContent = oConjTable["ppas2"] || " ";
779
780
781
782
783
784
785
786

787
788
789
790
791
792
793
816
817
818
819
820
821
822

823
824
825
826
827
828
829
830







-
+







            this.xParent.getElementById('grammalecte_conj_simp2').textContent = oConjTable["simp2"] || " ";
            this.xParent.getElementById('grammalecte_conj_simp3').textContent = oConjTable["simp3"] || " ";
            this.xParent.getElementById('grammalecte_conj_simp4').textContent = oConjTable["simp4"] || " ";
            this.xParent.getElementById('grammalecte_conj_simp5').textContent = oConjTable["simp5"] || " ";
            this.xParent.getElementById('grammalecte_conj_simp6').textContent = oConjTable["simp6"] || " ";
        }
        catch (e) {
            console.error(e.fileName + "\n" + e.name + "\nline: " + e.lineNumber + "\n" + e.message);
            showError(e);
        }
    }
}


class GrammalecteTooltip {

936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
973
974
975
976
977
978
979









































































































































-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
        catch (e) {
            let xSuggBlock = this.xParent.getElementById("grammalecte_tooltip_sugg_block");
            xSuggBlock.appendChild(document.createTextNode("# Oups. Le mécanisme de suggestion orthographique a rencontré un bug… (Ce module est encore en phase β.)"));
            showError(e);
        }
    }
}


class GrammalecteTextControl {

    constructor () {
        this.xNode = null;
        this.dParagraph = new Map();
        this.bTextArea = false;
        this.bIframe = false;
        this.bResultInEvent = false; // if true, the node content is not modified, but an event is dispatched on the node with the modified text
        this.xResultNode = null; // only useful if for text analysed without node
    }

    setNode (xNode) {
        this.clear();
        this.xNode = xNode;
        this.bResultInEvent = Boolean(xNode.dataset.grammalecte_result_via_event && xNode.dataset.grammalecte_result_via_event == "true");
        this.bTextArea = (xNode.tagName == "TEXTAREA" || xNode.tagName == "INPUT");
        this.bIframe = (xNode.tagName == "IFRAME");
        if (this.bTextArea) {
            this.xNode.disabled = true;
            this.loadText(this.xNode.value);
        }
        else if (this.bIframe) {
            // iframe
            if (!this.bResultInEvent) {
                oGrammalecte.oGCPanel.addMessageToGCPanel("⛔ La zone analysée est un cadre contenant une autre page web (“iframe”). Les changements faits ne peuvent être pas répercutés dans cette zone.");
            }
            this.loadText(this.xNode.contentWindow.document.body.innerText);
        }
        else {
            // editable node
            oGrammalecte.oGCPanel.addMessageToGCPanel("❗ La zone de texte analysée est un champ textuel enrichi susceptible de contenir des éléments non textuels qui seront effacés lors de la correction.");
            this.loadText(this.xNode.innerText);
        }
    }

    setResultNode (xNode) {
        if (xNode instanceof HTMLElement) {
            this.xResultNode = xNode;
            this.bResultInEvent = true;
        }
    }

    setText (sText) {
        this.clear();
        if (!this.xResultNode) {
            oGrammalecte.oGCPanel.addMessageToGCPanel("⛔ Aucun champ textuel défini. Les changements ne seront pas répercutés sur la zone d’où le texte a été extrait.");
        }
        this.loadText(sText);
    }

    loadText (sText) {
        // function also used by the text formatter
        if (typeof(sText) === "string") {
            this.dParagraph.clear();
            let i = 0;
            let iStart = 0;
            let iEnd = 0;
            sText = sText.replace(/\r\n/g, "\n").replace(/\r/g, "\n").normalize("NFC");
            while ((iEnd = sText.indexOf("\n", iStart)) !== -1) {
                this.dParagraph.set(i, sText.slice(iStart, iEnd));
                i++;
                iStart = iEnd+1;
            }
            this.dParagraph.set(i, sText.slice(iStart));
            //console.log("Paragraphs number: " + (i+1));
        }
    }

    clear () {
        if (this.xNode !== null) {
            this.xNode.disabled = false;
            this.bTextArea = false;
            this.bIframe = false;
            this.bResultInEvent = false;
            this.xNode = null;
            this.xResultNode = null;
        }
        this.dParagraph.clear();
    }

    getText () {
        return [...this.dParagraph.values()].join("\n").normalize("NFC");
    }

    setParagraph (iParagraph, sText) {
        this.dParagraph.set(iParagraph, sText);
    }

    getParagraph (iParaNum) {
        return this.dParagraph.get(iParaNum);
    }

    eraseNodeContent () {
        while (this.xNode.firstChild) {
            this.xNode.removeChild(this.xNode.firstChild);
        }
    }

    write () {
        if (this.xNode !== null) {
            if (this.bResultInEvent) {
                const xEvent = new CustomEvent("GrammalecteResult", { detail: JSON.stringify({ sType: "text", sText: this.getText() }) });
                this.xNode.dispatchEvent(xEvent);
                //console.log("[Grammalecte debug] Text sent to xNode via event:", xEvent.detail);
            }
            else if (this.bTextArea) {
                this.xNode.value = this.getText();
                //console.log("[Grammalecte debug] text written in textarea:", this.getText());
            }
            else if (this.bIframe) {
                //console.log(this.getText());
            }
            else {
                this.eraseNodeContent();
                this.dParagraph.forEach((val, key) => {
                    this.xNode.appendChild(document.createTextNode(val.normalize("NFC")));
                    this.xNode.appendChild(document.createElement("br"));
                });
                //console.log("[Grammalecte debug] text written in editable node:", this.getText());
            }
        }
        else if (this.xResultNode !== null) {
            const xEvent = new CustomEvent("GrammalecteResult", { detail: JSON.stringify({ sType: "text", sText: this.getText() }) });
            this.xResultNode.dispatchEvent(xEvent);
            //console.log("[Grammalecte debug] Text sent to xResultNode via event:", xEvent.detail);
        }
    }
}