Grammalecte  Check-in [a2a19392b6]

Overview
Comment:[fx] modify textarea content while typing
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | fx | webext2
Files: files | file ages | folders
SHA3-256: a2a19392b600359908cc21e74383cd1f1d6d31ee253b37ff58b3896de3d75270
User & Date: olr on 2017-08-22 12:27:59
Other Links: branch diff | manifest | tags
Context
2017-08-22
12:29
[fx] useless code check-in: 9e03da4287 user: olr tags: fx, webext2
12:27
[fx] modify textarea content while typing check-in: a2a19392b6 user: olr tags: fx, webext2
09:34
[fx] recheck paragraph after applying suggestion check-in: 5bfb5a6c67 user: olr tags: fx, webext2
Changes

Modified gc_lang/fr/webext/content_scripts/gc_content.js from [12bc7392df] to [7deea34453].

1
2
3
4
5
6
7
8
9
10
11
12
13
14

15
16

17
18
19
20
21
22
23
24
25
26
27
28
29












30
31
32
33
34
35
36
1
2
3
4
5
6
7
8
9
10
11
12
13

14
15

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48













-
+

-
+













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







// JavaScript

"use strict";

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

function onGrammalecteGCPanelKeyUp (xEvent) {
    try {
        let xElem = xEvent.target;
        if (xElem.id) {
            console.log(xElem.id);
        }
    }
    catch (e) {
        showError(e);
    }
}


78
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
112
113

114
115
116
117
118
119
120
121
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
90
91
92
93
94
95
96


97
98
99

100
101
102
103
104
105
106
107

108
109
110
111
112
113
114
115
116
117
118
119


120
121
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
169
170
171
172
173
174
175







-
-
+
+

-
+


+
+
+
+
+
-
+











-
-
+
+


-
+





-
+

-















-
+



















-
-
+
+








    addParagraphResult: function (oResult) {
        try {
            if (oResult) {
                let xNodeDiv = createNode("div", {className: "grammalecte_paragraph_block"});
                // actions
                let xActionsBar = createNode("div", {className: "grammalecte_paragraph_actions"});
                xActionsBar.appendChild(createNode("div", {id: "grammalecte_check" + oResult.sParaNum, className: "button green", textContent: "Réanalyser"}));
                xActionsBar.appendChild(createNode("div", {id: "grammalecte_hide" + oResult.sParaNum, className: "button red bold", textContent: "×"}));
                xActionsBar.appendChild(createNode("div", {id: "grammalecte_check" + oResult.iParaNum, className: "button green", textContent: "Réanalyser"}));
                xActionsBar.appendChild(createNode("div", {id: "grammalecte_hide" + oResult.iParaNum, className: "button red bold", textContent: "×"}));
                // paragraph
                let xParagraph = createNode("p", {id: "grammalecte_paragraph"+oResult.sParaNum, lang: "fr", contentEditable: "true"});
                let xParagraph = createNode("p", {id: "grammalecte_paragraph"+oResult.iParaNum, lang: "fr", contentEditable: "true"}, {para_num: oResult.iParaNum});
                xParagraph.setAttribute("spellcheck", "false"); // doesn’t seem possible to use “spellcheck” as a common attribute.
                xParagraph.className = (oResult.aGrammErr.length || oResult.aSpellErr.length) ? "grammalecte_paragraph softred" : "grammalecte_paragraph";
                xParagraph.addEventListener("keyup", function (xEvent) {
                    this.oTAC.setParagraph(parseInt(xEvent.target.dataset.para_num), this.purgeText(xEvent.target.textContent));
                    this.oTAC.write();
                }.bind(this)
                , true);
                this._tagParagraph(xParagraph, oResult.sParagraph, oResult.sParaNum, oResult.aGrammErr, oResult.aSpellErr);
                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);
        }
    },

    recheckParagraph: function (sParagraphNum) {
        let sParagraphId = "grammalecte_paragraph" + sParagraphNum;
    recheckParagraph: function (iParaNum) {
        let sParagraphId = "grammalecte_paragraph" + iParaNum;
        let xParagraph = document.getElementById(sParagraphId);
        this.blockParagraph(xParagraph);
        let sText = this.getPurgedTextOfParagraph(xParagraph.textContent);
        let sText = this.purgeText(xParagraph.textContent);
        xPort.postMessage({
            sCommand: "parseAndSpellcheck1",
            dParam: {sText: sText, sCountry: "FR", bDebug: false, bContext: false},
            dInfo: {sParagraphId: sParagraphId}
        });
        this.oTAC.setParagraph(parseInt(sParagraphNum), sText);
        this.oTAC.setParagraph(iParaNum, 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";
            xParagraph.textContent = "";
            this._tagParagraph(xParagraph, oResult.sParagraph, sParagraphId.slice(21), oResult.aGrammErr, oResult.aSpellErr);
            this.freeParagraph(xParagraph);
        }
        catch (e) {
            showError(e);
        }
    },

    _tagParagraph: function (xParagraph, sParagraph, sParaNum, aSpellErr, aGrammErr) {
    _tagParagraph: function (xParagraph, sParagraph, iParaNum, aSpellErr, aGrammErr) {
        try {
            if (aGrammErr.length === 0  &&  aSpellErr.length === 0) {
                xParagraph.textContent = sParagraph;
                return
            }
            aGrammErr.push(...aSpellErr);
            aGrammErr.sort(function (a, b) {
                if (a["nStart"] < b["nStart"])
                    return -1;
                if (a["nStart"] > b["nStart"])
                    return 1;
                return 0;
            });
            let nErr = 0; // we count errors to give them an identifier
            let nEndLastErr = 0;
            for (let oErr of aGrammErr) {
                let nStart = oErr["nStart"];
                let nEnd = oErr["nEnd"];
                if (nStart >= nEndLastErr) {
                    oErr['sErrorId'] = sParaNum + "-" + nErr.toString(); // error identifier
                    oErr['sIgnoredKey'] = sParaNum + ":" + nStart.toString() + ":" + sParagraph.slice(nStart, nEnd);
                    oErr['sErrorId'] = iParaNum + "-" + nErr.toString(); // error identifier
                    oErr['sIgnoredKey'] = iParaNum + ":" + nStart.toString() + ":" + sParagraph.slice(nStart, nEnd);
                    if (nEndLastErr < nStart) {
                        xParagraph.appendChild(document.createTextNode(sParagraph.slice(nEndLastErr, nStart)));
                    }
                    xParagraph.appendChild(this._createError(sParagraph.slice(nStart, nEnd), oErr));
                    xParagraph.insertAdjacentHTML("beforeend", "<!-- err_end -->");
                    nEndLastErr = nEnd;
                }
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
213

214
215
216
217
218
219
220
201
202
203
204
205
206
207


208
209
210
211

212
213
214
215
216
217
218
219
220
221
222
223
224
225

226
227
228
229
230
231
232
233







-
-




-














-
+







            xNodeErr.dataset.suggestions = oErr["aSuggestions"].join("|");
        }
        xNodeErr.className = (this.aIgnoredErrors.has(xNodeErr.dataset.ignored_key)) ? "ignored" : "error " + oErr['sType'];
        return xNodeErr;
    },

    blockParagraph: function (xParagraph) {
        xParagraph.style = "background-color: hsl(30, 100%, 80%)";
        xParagraph.disabled = true;
        xParagraph.contentEditable = "false";
    },

    freeParagraph: function (xParagraph) {
        xParagraph.style = "";
        xParagraph.contentEditable = "true";
    },

    applySuggestion: function (sNodeSuggId) { // sugg
        try {
            console.log(sNodeSuggId);
            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");
            this.oTooltip.hide();
            this.recheckParagraph(sErrorId.slice(0, sErrorId.indexOf("-")));
            this.recheckParagraph(parseInt(sErrorId.slice(0, sErrorId.indexOf("-"))));
        }
        catch (e) {
            showError(e);
        }
    },

    ignoreError: function (sIgnoreButtonId) {  // ignore
228
229
230
231
232
233
234
235

236
237
238
239
240
241
242
241
242
243
244
245
246
247

248
249
250
251
252
253
254
255







-
+







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

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

    addSummary: function () {
        // todo
    },

328
329
330
331
332
333
334

335
336
337
338
339
340
341
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355







+







                        iSugg += 1;
                    }
                } else {
                    xGCSugg.textContent = "Aucune.";
                }
            }
            this.xTooltipArrow.style.display = "block";
            this.xTooltipArrow.style.border = "10px solid hsl(0, 0%, 0%)";
            this.xTooltip.style.display = "block";
            if (xNodeErr.dataset.error_type === "spelling") {
                // spelling mistake
                document.getElementById("grammalecte_tooltip_message").textContent = "Mot inconnu du dictionnaire.";
                document.getElementById("grammalecte_tooltip_ignore").dataset.error_id = xNodeErr.dataset.error_id;
                while (this.xTooltipSuggBlock.firstChild) {
                    this.xTooltipSuggBlock.removeChild(this.xTooltipSuggBlock.firstChild);

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

184
185
186
187
188
189
190
191

192
193
194
195
196


197
198
199
200
201
202
203
184
185
186
187
188
189
190

191
192
193
194


195
196
197
198
199
200
201
202
203







-
+



-
-
+
+







        let aGrammErr = gc_engine.parse(sParagraph, sCountry, bDebug, bContext);
        postMessage(createResponse("parse", aGrammErr, dInfo, false));
    }
    postMessage(createResponse("parse", null, dInfo, true));
}

function parseAndSpellcheck (sText, sCountry, bDebug, bContext, dInfo={}) {
    let n = 0;
    let i = 0;
    for (let sParagraph of text.getParagraph(sText)) {
        let aGrammErr = gc_engine.parse(sParagraph, sCountry, bDebug, bContext);
        let aSpellErr = oTokenizer.getSpellingErrors(sParagraph, oDict);
        postMessage(createResponse("parseAndSpellcheck", {sParagraph: sParagraph, sParaNum: n.toString(), aGrammErr: aGrammErr, aSpellErr: aSpellErr}, dInfo, false));
        n += 1;
        postMessage(createResponse("parseAndSpellcheck", {sParagraph: sParagraph, iParaNum: i, aGrammErr: aGrammErr, aSpellErr: aSpellErr}, dInfo, false));
        i += 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);