Grammalecte  Diff

Differences From Artifact [da6d34e941]:

To Artifact [ed054ff2cf]:


57
58
59
60
61
62
63
64

65
66
67
68
69
70
71
57
58
59
60
61
62
63

64
65
66
67
68
69
70
71







-
+







        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.bAutoRefresh = false;
        this.nLastResult = 0
        // Lexicographer
        this.nLxgCount = 0;
        this.xLxgPanelContent = oGrammalecte.createNode("div", {id: "grammalecte_lxg_panel_content"});
        this.xPanelContent.appendChild(this.xLxgPanelContent);
        // Conjugueur
        this.xConjPanelContent = oGrammalecte.createNode("div", {id: "grammalecte_conj_panel_content"});
        this.xConjPanelContent.innerHTML = sGrammalecteConjugueurHTML;  // @Reviewers: sGrammalecteConjugueurHTML is a const value defined in <content_scripts/html_src.js>
79
80
81
82
83
84
85


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102



103
104
105
106
107
108
109
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101



102
103
104
105
106
107
108
109
110
111







+
+














-
-
-
+
+
+







        this.xTFButton = oGrammalecte.createNode("div", {className: "grammalecte_menu_button", textContent: "Formateur de texte"});
        this.xEditorButton = oGrammalecte.createNode("div", {className: "grammalecte_menu_button", textContent: "Éditeur"});
        this.xLxgButton = oGrammalecte.createNode("div", {className: "grammalecte_menu_button", textContent: "Lexicographe"});
        this.xConjButton = oGrammalecte.createNode("div", {className: "grammalecte_menu_button", textContent: "Conjugueur"});
        this.xLEButton = oGrammalecte.createNode("div", {className: "grammalecte_menu_button", textContent: "•Éditeur lexical•"});
        this.xAutoRefresh = oGrammalecte.createNode("div", {className: "grammalecte_autorefresh_button", textContent: "AR", title: "Auto-rafraîchissement de la correction grammaticale (3 s après la dernière frappe)"})
        this.xEditorButton.appendChild(this.xAutoRefresh);
        this.bAutoRefresh = oGrammalecte.bAutoRefresh;
        this.setAutoRefreshButton();
        this.xTFButton.onclick = () => {
            if (!this.bWorking) {
                oGrammalecte.createTFPanel();
                oGrammalecte.oTFPanel.start();
                oGrammalecte.oTFPanel.show();
            }
        };
        this.xEditorButton.onclick = () => {
            if (!this.bWorking) {
                this.showEditor();
            }
        };
        this.xAutoRefresh.onclick = () => {
            this.bAutoRefresh = !this.bAutoRefresh;
            this.xAutoRefresh.style.backgroundColor = (this.bAutoRefresh) ? "hsl(150, 50%, 50%)" : "";
            this.xAutoRefresh.style.color = (this.bAutoRefresh) ? "hsl(150, 50%, 96%)" : "";
            this.xAutoRefresh.style.opacity = (this.bAutoRefresh) ? "1" : "";
            oGrammalecte.bAutoRefresh = this.bAutoRefresh;
            browser.storage.local.set({"autorefresh_option": this.bAutoRefresh});
            this.setAutoRefreshButton();
        }
        this.xLxgButton.onclick = () => {
            if (!this.bWorking) {
                this.showLexicographer();
                this.clearLexicographer();
                this.startWaitIcon();
                xGrammalectePort.postMessage({
122
123
124
125
126
127
128

129
130
131
132
133
134
135
136
137
138
139
140
141

142
143
144
145
146
147
148
149
150
151
152
153
154
155






156
157
158
159
160
161

162
163
164
165
166
167
168
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179







+













+














+
+
+
+
+
+






+







            if (!this.bWorking) {
                this.showConjugueur();
            }
        };
        this.xLEButton.onclick = () => {
            xGrammalectePort.postMessage({sCommand: "openLexiconEditor", dParam: null, dInfo: null});
        };
        // Menu, tabs
        this.xMenu.appendChild(this.xTFButton)
        this.xMenu.appendChild(this.xEditorButton)
        this.xMenu.appendChild(this.xLxgButton)
        this.xMenu.appendChild(this.xConjButton)
        this.xMenu.appendChild(this.xLEButton)
        this.xPanelBar.appendChild(this.xMenu);
    }

    start (what) {
        this.oTooltip.hide();
        this.bWorking = false;
        this.clear();
        this.hideMessage();
        this.resetTimer();
        if (typeof(what) === "string") {
            // text
            this.xNode = null;
            this.oTextControl.setText(what);
        } else if (what.nodeType && what.nodeType === 1) {
            // node
            this.xNode = what;
            this.oTextControl.setNode(this.xNode);
        } else {
            // error
            oGrammalecte.oMessageBox.showMessage("[BUG] Analyse d’un élément inconnu…");
            console.log("Grammalecte [bug]:", what);
        }
    }

    setAutoRefreshButton () {
        this.xAutoRefresh.style.backgroundColor = (this.bAutoRefresh) ? "hsl(150, 50%, 50%)" : "";
        this.xAutoRefresh.style.color = (this.bAutoRefresh) ? "hsl(150, 50%, 96%)" : "";
        this.xAutoRefresh.style.opacity = (this.bAutoRefresh) ? "1" : "";
    }

    recheckAll () {
        this.oTooltip.hide();
        this.showEditor();
        this.clear();
        this.startWaitIcon();
        this.resetTimer();
        xGrammalectePort.postMessage({
            sCommand: "parseAndSpellcheck",
            dParam: {sText: this.oTextControl.getText(), sCountry: "FR", bDebug: false, bContext: false},
            dInfo: ((this.xNode) ? {sTextAreaId: this.xNode.id} : {})
        });
    }

216
217
218
219
220
221
222

223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
















269
270
271
272
273

274
275
276
277
278
279
280
281
282
283
284
285
286
287


288



289

290
291

292
293
294
295
296
297
298
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249




250
251
252
253
254
255










256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286

287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303

304
305
306
307
308
309

310
311
312
313
314
315
316
317







+















-
-
-
-






-
-
-
-
-
-
-
-
-
-











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




-
+














+
+
-
+
+
+

+

-
+







        oGrammalecte.clearRightClickedNode();
        this.xPanel.style.display = "none";
        this.oTextControl.clear();
    }

    addParagraphResult (oResult) {
        try {
            this.resetTimer();
            if (oResult && (oResult.sParagraph.trim() !== "" || oResult.aGrammErr.length > 0 || oResult.aSpellErr.length > 0)) {
                let xNodeDiv = oGrammalecte.createNode("div", {className: "grammalecte_paragraph_block"});
                // actions
                let xActionsBar = oGrammalecte.createNode("div", {className: "grammalecte_paragraph_actions"});
                xActionsBar.appendChild(oGrammalecte.createNode("div", {id: "grammalecte_check" + oResult.iParaNum, className: "grammalecte_paragraph_button grammalecte_green", textContent: "↻", title: "Réanalyser…"}, {para_num: oResult.iParaNum}));
                xActionsBar.appendChild(oGrammalecte.createNode("div", {id: "grammalecte_hide" + oResult.iParaNum, className: "grammalecte_paragraph_button grammalecte_red", textContent: "×", title: "Cacher", style: "font-weight: bold;"}));
                // paragraph
                let xParagraph = oGrammalecte.createNode("p", {id: "grammalecte_paragraph"+oResult.iParaNum, className: "grammalecte_paragraph", lang: "fr", contentEditable: "true"}, {para_num: oResult.iParaNum});
                xParagraph.setAttribute("spellcheck", "false"); // doesn’t seem possible to use “spellcheck” as a common attribute.
                xParagraph.dataset.timer_id = "0";
                xParagraph.addEventListener("input", function (xEvent) {
                    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);
                        // save caret position
                        let [nStart, nEnd] = oGrammalecte.getCaretPosition(xParagraph);
                        xParagraph.dataset.caret_position_start = nStart;
                        xParagraph.dataset.caret_position_end = nEnd;
                    }
                    // write text
                    this.oTextControl.setParagraph(parseInt(xEvent.target.dataset.para_num, 10), this.purgeText(xEvent.target.textContent));
                    this.oTextControl.write();
                }.bind(this)
                , true);
                /*xParagraph.addEventListener("blur", function (xEvent) {
                    // remove timer for refreshing analysis
                    window.clearTimeout(parseInt(xParagraph.dataset.timer_id));
                    // unset caret position
                    xParagraph.dataset.caret_position_start = "-1";
                    xParagraph.dataset.caret_position_end = "-1";
                    // recheck
                    this.recheckParagraph(oResult.iParaNum);
                }.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);
            }
        }
        catch (e) {
            showError(e);
        }
    }

    resetTimer () {
        this.nLastResult = Date.now();
        window.clearTimeout(this.nTimer);
        this.nTimer = window.setTimeout(
            oGrammalecte.oGCPanel.showMessage.bind(this),
            5000,
            "Le serveur grammatical semble ne plus répondre.",
            "Arrêter et relancer le serveur grammatical.",
            "restartWorker"
        );
    }

    endTimer () {
        window.clearTimeout(this.nTimer);
    }

    recheckParagraph (iParaNum) {
        let sParagraphId = "grammalecte_paragraph" + iParaNum;
        let xParagraph = this.xParent.getElementById(sParagraphId);
        this.blockParagraph(xParagraph);
        this._blockParagraph(xParagraph);
        let sText = this.purgeText(xParagraph.textContent);
        xGrammalectePort.postMessage({
            sCommand: "parseAndSpellcheck1",
            dParam: {sText: sText, sCountry: "FR", bDebug: false, bContext: false},
            dInfo: {sParagraphId: sParagraphId}
        });
        this.oTextControl.setParagraph(iParaNum, sText);
        this.oTextControl.write();
    }

    refreshParagraph (sParagraphId, oResult) {
        // function called when results are sent by the Worker
        try {
            let xParagraph = this.xParent.getElementById(sParagraphId);
            // save caret position
            let [nStart, nEnd] = oGrammalecte.getCaretPosition(xParagraph);
            xParagraph.className = (oResult.aGrammErr.length || oResult.aSpellErr.length) ? "grammalecte_paragraph softred" : "grammalecte_paragraph";
            xParagraph.dataset.caret_position_start = nStart;
            xParagraph.dataset.caret_position_end = nEnd;
            // erase texte
            xParagraph.textContent = "";
            // recreate and retag
            this._tagParagraph(xParagraph, oResult.sParagraph, sParagraphId.slice(21), oResult.aGrammErr, oResult.aSpellErr);
            this.freeParagraph(xParagraph);
            this._freeParagraph(xParagraph);
        }
        catch (e) {
            showError(e);
        }
    }

    _tagParagraph (xParagraph, sParagraph, iParaNum, aSpellErr, aGrammErr) {
351
352
353
354
355
356
357
358

359
360
361
362
363
364
365
366

367
368
369
370
371
372
373
370
371
372
373
374
375
376

377
378
379
380
381
382
383
384

385
386
387
388
389
390
391
392







-
+







-
+







            xNodeErr.dataset.suggestions = oErr["aSuggestions"].join("|");
        }
        xNodeErr.className = (this.aIgnoredErrors.has(xNodeErr.dataset.ignored_key)) ? "grammalecte_error_ignored" : "grammalecte_error";
        xNodeErr.style.backgroundColor = (oErr['sType'] === "WORD") ? "hsl(0, 50%, 50%)" : oErr["aColor"];
        return xNodeErr;
    }

    blockParagraph (xParagraph) {
    _blockParagraph (xParagraph) {
        xParagraph.contentEditable = "false";
        this.xParent.getElementById("grammalecte_check"+xParagraph.dataset.para_num).textContent = "!!";
        this.xParent.getElementById("grammalecte_check"+xParagraph.dataset.para_num).style.backgroundColor = "hsl(0, 50%, 50%)";
        this.xParent.getElementById("grammalecte_check"+xParagraph.dataset.para_num).style.boxShadow = "0 0 0 3px hsla(0, 0%, 50%, .2)";
        this.xParent.getElementById("grammalecte_check"+xParagraph.dataset.para_num).style.animation = "grammalecte-pulse 1s linear infinite";
    }

    freeParagraph (xParagraph) {
    _freeParagraph (xParagraph) {
        xParagraph.contentEditable = "true";
        if (xParagraph.dataset.caret_position_start !== "-1") {
            let nStart = parseInt(xParagraph.dataset.caret_position_start, 10);
            let nEnd = parseInt(xParagraph.dataset.caret_position_end, 10);
            oGrammalecte.setCaretPosition(xParagraph, nStart, nEnd);
        }
        this.xParent.getElementById("grammalecte_check"+xParagraph.dataset.para_num).textContent = "↻";