Grammalecte  Check-in [40e9aa097d]

Overview
Comment:[fx] textarea control + small code refactoring
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | fx | webext2
Files: files | file ages | folders
SHA3-256: 40e9aa097d93a0e86353d91396d5e074837547b58a81dd08510631ac4f9c8c7c
User & Date: olr on 2017-08-22 08:02:47
Other Links: branch diff | manifest | tags
Context
2017-08-22
09:34
[fx] recheck paragraph after applying suggestion check-in: 5bfb5a6c67 user: olr tags: fx, webext2
08:02
[fx] textarea control + small code refactoring check-in: 40e9aa097d user: olr tags: fx, webext2
2017-08-16
18:27
[fx] object to control textarea check-in: 9002c7508c user: olr tags: fx, webext2
Changes

Modified gc_lang/fr/webext/content_scripts/content_modifier.js from [1ff5fd0533] to [4e0ba04476].

49
50
51
52
53
54
55
56



57
58



59
60
61
62
63
64
65
66
67
68
69
70
71
72

73
74
75
76
77
78
79
49
50
51
52
53
54
55

56
57
58
59

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84







-
+
+
+

-
+
+
+














+







    }
}

function createWrapperToolbar (xTextArea) {
    try {
        let xToolbar = createNode("div", {className: "grammalecte_wrapper_toolbar"});
        let xConjButton = createNode("div", {className: "grammalecte_wrapper_button", textContent: "Conjuguer"});
        xConjButton.onclick = function() { createConjPanel(); };
        xConjButton.onclick = function() {
            createConjPanel();
        };
        let xTFButton = createNode("div", {className: "grammalecte_wrapper_button", textContent: "Formater"});
        xTFButton.onclick = function() { createTFPanel(xTextArea); };
        xTFButton.onclick = function() {
            createTFPanel(xTextArea);
        };
        let xLxgButton = createNode("div", {className: "grammalecte_wrapper_button", textContent: "Analyser"});
        xLxgButton.onclick = function() {
            createLxgPanel();
            oLxgPanel.startWaitIcon();
            xPort.postMessage({
                sCommand: "getListOfTokens",
                dParam: {sText: xTextArea.value},
                dInfo: {sTextAreaId: xTextArea.id}
            });
        };
        let xGCButton = createNode("div", {className: "grammalecte_wrapper_button", textContent: "Corriger"});
        xGCButton.onclick = function() {
            createGCPanel();
            oGCPanel.startWaitIcon();
            oGCPanelContent.start(xTextArea);
            xPort.postMessage({
                sCommand: "parseAndSpellcheck",
                dParam: {sText: xTextArea.value, sCountry: "FR", bDebug: false, bContext: false},
                dInfo: {sTextAreaId: xTextArea.id}
            });
        };
        // Create

Modified gc_lang/fr/webext/content_scripts/gc_content.js from [869a446e62] to [da49634b64].

12
13
14
15
16
17
18
19

20
21
22
23

24
25
26
27
28

29
30
31
32
33
34
35
12
13
14
15
16
17
18

19
20
21
22

23
24
25
26
27

28
29
30
31
32
33
34
35







-
+



-
+




-
+







                oGCPanelContent.ignoreError(xElem.id);
            } else if (xElem.id.startsWith("grammalecte_check")) {
                oGCPanelContent.recheckParagraph(xElem.id.slice(17));
            } else if (xElem.id.startsWith("grammalecte_hide")) {
                document.getElementById(xElem.id).parentNode.parentNode.style.display = "none";
            } else if (xElem.tagName === "U" && xElem.id.startsWith("grammalecte_err")
                       && xElem.className !== "corrected" && xElem.className !== "ignored") {
                oGrammalecteTooltip.show(xElem.id);
                oGCPanelContent.oTooltip.show(xElem.id);
            } else if (xElem.id === "grammalecte_tooltip_url") {
                oGCPanelContent.openURL(xElem.getAttribute("href"));
            } else {
                oGrammalecteTooltip.hide();
                oGCPanelContent.oTooltip.hide();
            }
        } else if (xElem.tagName === "A") {
            oGCPanelContent.openURL(xElem.getAttribute("href"));
        } else {
            oGrammalecteTooltip.hide();
            oGCPanelContent.oTooltip.hide();
        }
    }
    catch (e) {
        showError(e);
    }
}

52
53
54
55
56
57
58
59
60


61
62
63







64
65
66
67
68
69
70
52
53
54
55
56
57
58


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77







-
-
+
+



+
+
+
+
+
+
+







    aIgnoredErrors: new Set(),

    init: function () {
        this.xContentNode = createNode("div", {id: "grammalecte_gc_panel_content"});
        this.xParagraphList = createNode("div", {id: "grammalecte_paragraph_list"});
        this.xContentNode.appendChild(this.xParagraphList);
        this.xContentNode.addEventListener("click", onGrammalecteGCPanelClick, false);
        oGrammalecteTooltip.init();
        this.xContentNode.appendChild(oGrammalecteTooltip.xTooltip);
        this.oTooltip.init();
        this.xContentNode.appendChild(this.oTooltip.xTooltip);
        this.bInitDone = true;
        return this.xContentNode;
    },

    start: function (xTextArea=null) {
        this.clear();
        if (xTextArea) {
            this.oTAC.setTextArea(xTextArea);
        }
    },

    clear: function () {
        while (this.xParagraphList.firstChild) {
            this.xParagraphList.removeChild(this.xParagraphList.firstChild);
        }
        this.aIgnoredErrors.clear();
    },
93
94
95
96
97
98
99

100
101
102

103
104


105
106
107
108
109
110
111
100
101
102
103
104
105
106
107
108
109

110
111
112
113
114
115
116
117
118
119
120
121







+


-
+


+
+







        }
    },

    recheckParagraph: function (sParagraphNum) {
        //startWaitIcon();
        let sParagraphId = "grammalecte_paragraph" + sParagraphNum;
        let xParagraph = document.getElementById(sParagraphId);
        let sText = this.getPurgedTextOfParagraph(xParagraph.textContent);
        xPort.postMessage({
            sCommand: "parseAndSpellcheck1",
            dParam: {sText: this.getPurgedTextOfParagraph(xParagraph.textContent), sCountry: "FR", bDebug: false, bContext: false},
            dParam: {sText: sText, sCountry: "FR", bDebug: false, bContext: false},
            dInfo: {sParagraphId: sParagraphId}
        });
        this.oTAC.setParagraph(parseInt(sParagraphNum), sText);
        this.oTAC.write();
        //stopWaitIcon();
    },

    refreshParagraph: function (sParagraphId, oResult) {
        try {
            let xParagraph = document.getElementById(sParagraphId);
            xParagraph.className = (oResult.aGrammErr.length || oResult.aSpellErr.length) ? "grammalecte_paragraph softred" : "grammalecte_paragraph";
183
184
185
186
187
188
189
190

191
192
193
194
195
196
197
198
199
200
201
202
203
204
205

206
207
208
209
210
211
212
193
194
195
196
197
198
199

200
201
202
203
204
205
206
207
208
209
210
211
212
213
214

215
216
217
218
219
220
221
222







-
+














-
+







            let sErrorId = document.getElementById(sNodeSuggId).dataset.error_id;
            //let sParaNum = sErrorId.slice(0, sErrorId.indexOf("-"));
            console.log("grammalecte_err"+sErrorId);
            let xNodeErr = document.getElementById("grammalecte_err" + sErrorId);
            xNodeErr.textContent = document.getElementById(sNodeSuggId).textContent;
            xNodeErr.className = "corrected";
            xNodeErr.removeAttribute("style");
            oGrammalecteTooltip.hide();
            this.oTooltip.hide();
        }
        catch (e) {
            showError(e);
        }
    },

    ignoreError: function (sIgnoreButtonId) {  // ignore
        try {
            console.log(sIgnoreButtonId);
            let sErrorId = document.getElementById(sIgnoreButtonId).dataset.error_id;
            console.log("grammalecte_err"+sErrorId);
            let xNodeErr = document.getElementById("grammalecte_err" + sErrorId);
            this.aIgnoredErrors.add(xNodeErr.dataset.ignored_key);
            xNodeErr.className = "ignored";
            oGrammalecteTooltip.hide();
            this.oTooltip.hide();
        }
        catch (e) {
            showError(e);
        }
    },

    getPurgedTextOfParagraph: function (sText) {
240
241
242
243
244
245
246
247

248
249
250
251
252
253
254
250
251
252
253
254
255
256

257
258
259
260
261
262
263
264







-
+







        }
        stopWaitIcon();
    }
}



const oGrammalecteTooltip = {
oGCPanelContent.oTooltip = {

    xTooltip: null,

    xTooltipArrow: createNode("img", {
        id: "grammalecte_tooltip_arrow",
        src: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAECAYAAACzzX7wAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAOwQAADsEBuJFr7QAAABl0RVh0U29mdHdhcmUAcGFpbnQubmV0IDQuMC4xNkRpr/UAAAAlSURBVBhXY/j//z8cq/kW/wdhZDEMSXRFWCVhGKwAmwQCF/8HAGUkScGH4cM8AAAAAElFTkSuQmCC",
        alt: "^"
368
369
370
371
372
373
374

375

376
377

378
379

380
381
382
383





384
385
386

387
388
389




390
391
392

393

394
395

396
397
398
399
400
401
402
403
404
405
406
407
408
409












410
411
412

413
414
415
416
417

418
419
420


421
422

423
424
425
426
427




428
429
430
431


432
378
379
380
381
382
383
384
385

386
387

388
389

390
391



392
393
394
395
396
397
398
399
400



401
402
403
404
405
406
407
408

409


410


411
412










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



425


426
427

428



429
430


431





432
433
434
435




436
437








+
-
+

-
+

-
+

-
-
-
+
+
+
+
+



+
-
-
-
+
+
+
+



+
-
+
-
-
+
-
-


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


-
+
-
-
-
+
+
-
-
+
-
-
-
-
-
+
+
+
+
-
-
-
-
+
+
-
        catch (e) {
            showError(e);
        }
    }
}


oGCPanelContent.oTAC = {
const oGrammalecteTextareaControl = {
    // Text area control

    xTextarea: null,
    _xTextArea: null,

    dParagraph: new Map(),
    _dParagraph: new Map(),

    setTextarea: function (xNode) {
        this.xTextarea = xNode;
        this.xTextarea.disabled = true;
    setTextArea: function (xNode) {
        this.clear();
        this._xTextArea = xNode;
        this._xTextArea.disabled = true;
        this._loadText();
    },

    clear: function () {
        if (this._xTextArea !== null) {
        this.xTextarea.disabled = false;
        this.xTextarea = null;
        this.dParagraph.clear();
            this._xTextArea.disabled = false;
            this._xTextArea = null;
        }
        this._dParagraph.clear();
    },

    setParagraph (iParagraph, sText) {
        if (this._xTextArea !== null) {
        this.dParagraph.set(iParagraph, sText);
            this._dParagraph.set(iParagraph, sText);
    },

        }
    getParagraph (iParagraph) {
        return dParagraphs.has(iParagraph) ? this.dParagraphs.get(iParagraph) : this.getNthParagraph(iParagraph);
    },

    getNthParagraph: function (iParagraph) {
        if (this.xTextarea !== null) {
            let sText = this.xTextarea.value;
            let i = 0;
            let iStart = 0;
            while (i < iParagraph && ((iStart = sText.indexOf("\n", iStart)) !== -1)) {
                i++;
                iStart++;
            }
            if (i === iParagraph) {
    _loadText () {
        let sText = this._xTextArea.value;
        let i = 0;
        let iStart = 0;
        let iEnd = 0;
        sText = sText.replace("\r\n", "\n").replace("\r", "\n");
        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));
                return ((iEnd = sText.indexOf("\n", iStart)) !== -1) ? sText.slice(iStart, iEnd) : sText.slice(iStart);
            }
            return "# Erreur. Paragraphe introuvable.";
        console.log("Paragraphs number: " + (i+1));
        }
        return "# Erreur. Zone de texte introuvable.";
    },

    rewrite: function () {
    write: function () {
        try {
            if (this.xTextarea !== null) {
                let sText = "";
        if (this._xTextArea !== null) {
            let sText = "";
                let i = 0;
                for (let sParagraph of this.xTextarea.value.split("\n")) {
            this._dParagraph.forEach(function (val, key) {
                    sText += (dParagraphs.has(i)) ? dParagraphs.get(i) + "\n" : sParagraph + "\n";
                    i += 1;
                }
                this.xTextarea.value = sText.slice(0,-1);
            }
                sText += val + "\n";
            });
            this._xTextArea.value = sText.slice(0,-1);
        }
        } catch (e) {
            showError(e);
        }
    }
    }
}
}

Modified gc_lang/fr/webext/gce_worker.js from [cda351c05e] to [cd9b8f9d6b].

188
189
190
191
192
193
194
195
196

197
198
199
200
201
202
203
188
189
190
191
192
193
194

195
196
197
198
199
200
201
202
203







-

+







}

function parseAndSpellcheck (sText, sCountry, bDebug, bContext, dInfo={}) {
    let n = 0;
    for (let sParagraph of text.getParagraph(sText)) {
        let aGrammErr = gc_engine.parse(sParagraph, sCountry, bDebug, bContext);
        let aSpellErr = oTokenizer.getSpellingErrors(sParagraph, oDict);
        n += 1;
        postMessage(createResponse("parseAndSpellcheck", {sParagraph: sParagraph, sParaNum: n.toString(), aGrammErr: aGrammErr, aSpellErr: aSpellErr}, dInfo, false));
        n += 1;
    }
    postMessage(createResponse("parseAndSpellcheck", null, dInfo, true));
}

function parseAndSpellcheck1 (sParagraph, sCountry, bDebug, bContext, dInfo={}) {
    let aGrammErr = gc_engine.parse(sParagraph, sCountry, bDebug, bContext);
    let aSpellErr = oTokenizer.getSpellingErrors(sParagraph, oDict);