Grammalecte  Check-in [360a78ac9f]

Overview
Comment:[fx] call Grammalecte via custom button and send results by event on node
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | fx
Files: files | file ages | folders
SHA3-256: 360a78ac9fab6875a3f8b97144e5dbe117d4089a7627483a4a8f1d44e06e67a0
User & Date: olr on 2020-03-09 18:54:57
Other Links: manifest | tags
Context
2020-03-09
18:56
[fx] remove console.log() check-in: 442f6eaf7e user: olr tags: fx, trunk
18:54
[fx] call Grammalecte via custom button and send results by event on node check-in: 360a78ac9f user: olr tags: fx, trunk
18:53
[fr] ajustements check-in: 4ba1592ab7 user: olr tags: fr, trunk
Changes

Modified gc_lang/fr/webext/content_scripts/init.js from [a857d83fb7] to [646f2e9c29].

    73     73   
    74     74       clearRightClickedNode: function () {
    75     75           this.xRightClickedNode = null;
    76     76       },
    77     77   
    78     78       createButtons: function () {
    79     79           if (bChrome) {
    80         -            browser.storage.local.get("ui_options", this._createButtons.bind(this));
           80  +            browser.storage.local.get("ui_options", this._prepareButtons.bind(this));
    81     81               return;
    82     82           }
    83         -        browser.storage.local.get("ui_options").then(this._createButtons.bind(this), showError);
           83  +        browser.storage.local.get("ui_options").then(this._prepareButtons.bind(this), showError);
    84     84       },
    85     85   
    86         -    _createButtons: function (oOptions) {
           86  +    _prepareButtons: function (oOptions) {
    87     87           if (oOptions.hasOwnProperty("ui_options")) {
    88     88               this.oOptions = oOptions.ui_options;
    89         -            if (this.oOptions.textarea) {
    90         -                for (let xNode of document.getElementsByTagName("textarea")) {
    91         -                    if (xNode.style.display !== "none" && xNode.style.visibility !== "hidden" && xNode.getAttribute("spellcheck") !== "false") {
    92         -                        this.lButton.push(new GrammalecteButton(this.nButton, xNode));
    93         -                        this.nButton += 1;
    94         -                    }
           89  +            // textarea
           90  +            for (let xNode of document.getElementsByTagName("textarea")) {
           91  +                if (xNode.dataset.grammalecte_callbutton && document.getElementById(xNode.dataset.grammalecte_callbutton)) {
           92  +                    let xButton = document.getElementById(xNode.dataset.grammalecte_callbutton)
           93  +                    xButton.onclick = () => {
           94  +                        oGrammalecte.startGCPanel(xNode, true, true);
           95  +                    };
           96  +                    this.lButton.push(xButton);
           97  +                    this.nButton += 1;
           98  +                }
           99  +                else if (this.oOptions.textarea  &&  xNode.style.display !== "none" && xNode.style.visibility !== "hidden" && xNode.getAttribute("spellcheck") !== "false") {
          100  +                    this.lButton.push(new GrammalecteButton(this.nButton, xNode));
          101  +                    this.nButton += 1;
    95    102                   }
    96    103               }
    97         -            if (this.oOptions.editablenode) {
    98         -                for (let xNode of document.querySelectorAll("[contenteditable]")) {
          104  +            // editable nodes
          105  +            for (let xNode of document.querySelectorAll("[contenteditable]")) {
          106  +                if (xNode.dataset.grammalecte_callbutton && document.getElementById(xNode.dataset.grammalecte_callbutton)) {
          107  +                    let xButton = document.getElementById(xNode.dataset.grammalecte_callbutton)
          108  +                    xButton.onclick = () => {
          109  +                        oGrammalecte.startGCPanel(xNode, true, true);
          110  +                    };
          111  +                    this.lButton.push(xButton);
          112  +                    this.nButton += 1;
          113  +                }
          114  +                else if (this.oOptions.editablenode  &&  xNode.style.display !== "none" && xNode.style.visibility !== "hidden") {
    99    115                       this.lButton.push(new GrammalecteButton(this.nButton, xNode));
   100    116                       this.nButton += 1;
   101    117                   }
   102    118               }
   103    119           }
   104    120       },
   105    121   
   106    122       observePage: function () {
   107         -        // When a textarea is added via jascript we add the menu
          123  +        // When a textarea is added via jascript we add the buttons
   108    124           let that = this;
   109    125           this.xObserver = new MutationObserver(function (mutations) {
   110    126               mutations.forEach(function (mutation) {
   111    127                   for (let i = 0;  i < mutation.addedNodes.length;  i++){
   112    128                       if (mutation.addedNodes[i].tagName == "TEXTAREA") {
   113    129                           if (that.oOptions === null || that.oOptions.textarea) {
   114    130                               oGrammalecte.lButton.push(new GrammalecteButton(oGrammalecte.nButton, mutation.addedNodes[i]));
................................................................................
   156    172       createMessageBox: function () {
   157    173           if (this.oMessageBox === null) {
   158    174               this.oMessageBox = new GrammalecteMessageBox("grammalecte_message_box", "Grammalecte");
   159    175               this.oMessageBox.insertIntoPage();
   160    176           }
   161    177       },
   162    178   
   163         -    startGCPanel: function (what, bCheckText=true) {
          179  +    startGCPanel: function (what, bCheckText=true, bResultInEvent=false) {
   164    180           this.createGCPanel();
   165    181           this.oGCPanel.clear();
   166    182           this.oGCPanel.show();
   167    183           this.oGCPanel.showEditor();
   168         -        this.oGCPanel.start(what);
          184  +        this.oGCPanel.start(what, bResultInEvent);
   169    185           this.oGCPanel.startWaitIcon();
   170    186           if (what && bCheckText) {
   171    187               let sText = this.oGCPanel.oTextControl.getText();
   172    188               xGrammalectePort.postMessage({
   173    189                   sCommand: "parseAndSpellcheck",
   174    190                   dParam: {sText: sText, sCountry: "FR", bDebug: false, bContext: false},
   175    191                   dInfo: (what.nodeType && what.nodeType === 1) ? {sTextAreaId: what.id} : {}

Modified gc_lang/fr/webext/content_scripts/panel_gc.js from [1c3cef7a06] to [327f3b8b3c].

   135    135           this.xMenu.appendChild(this.xTFButton)
   136    136           this.xMenu.appendChild(this.xEditorButton)
   137    137           this.xMenu.appendChild(this.xLxgButton)
   138    138           this.xMenu.appendChild(this.xConjButton)
   139    139           this.xPanelBar.appendChild(this.xMenu);
   140    140       }
   141    141   
   142         -    start (what) {
          142  +    start (what, bResultInEvent=false) {
   143    143           this.oTooltip.hide();
   144    144           this.bWorking = false;
          145  +        this.oTextControl.bResultInEvent = bResultInEvent;
   145    146           this.clear();
   146    147           this.hideMessage();
   147    148           this.resetTimer();
   148    149           if (typeof(what) === "string") {
   149    150               // text
   150    151               this.xNode = null;
   151    152               this.oTextControl.setText(what);
................................................................................
   950    951   
   951    952   class GrammalecteTextControl {
   952    953   
   953    954       constructor () {
   954    955           this.xNode = null;
   955    956           this.dParagraph = new Map();
   956    957           this.bTextArea = null;
          958  +        this.bResultInEvent = false; // if true, the node content is not modified, but an event is dispatched on the node with the modified text
   957    959       }
   958    960   
   959    961       setNode (xNode) {
   960    962           this.clear();
   961    963           this.xNode = xNode;
   962    964           this.bTextArea = (xNode.tagName == "TEXTAREA" || xNode.tagName == "INPUT");
   963    965           if (!this.bTextArea) {
................................................................................
  1015   1017           while (this.xNode.firstChild) {
  1016   1018               this.xNode.removeChild(this.xNode.firstChild);
  1017   1019           }
  1018   1020       }
  1019   1021   
  1020   1022       write () {
  1021   1023           if (this.xNode !== null) {
  1022         -            if (this.bTextArea) {
         1024  +            if (this.bResultInEvent) {
         1025  +                const xEvent = new CustomEvent("GrammalecteNodeContentUpdated", {
         1026  +                    detail: { text: [...this.dParagraph.values()].join("\n").normalize("NFC") }
         1027  +                });
         1028  +                this.xNode.dispatchEvent(xEvent);
         1029  +                console.log("event", xEvent.detail.text);
         1030  +            }
         1031  +            else if (this.bTextArea) {
  1023   1032                   this.xNode.value = this.getText();
  1024         -            } else {
         1033  +            }
         1034  +            else {
  1025   1035                   this.eraseNodeContent();
  1026   1036                   this.dParagraph.forEach((val, key) => {
  1027   1037                       this.xNode.appendChild(document.createTextNode(val.normalize("NFC")));
  1028   1038                       this.xNode.appendChild(document.createElement("br"));
  1029   1039                   });
  1030         -                const xEvent = new CustomEvent("grammalecteNodeContentUpdated", {
  1031         -                    detail: { text: [...this.dParagraph.values()].join("\n").normalize("NFC") }
  1032         -                });
  1033         -                this.xNode.dispatchEvent(xEvent);
  1034   1040               }
  1035   1041           }
  1036   1042       }
  1037   1043   }