Index: gc_lang/fr/webext/content_scripts/content_modifier.js ================================================================== --- gc_lang/fr/webext/content_scripts/content_modifier.js +++ gc_lang/fr/webext/content_scripts/content_modifier.js @@ -51,13 +51,17 @@ 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({ @@ -68,10 +72,11 @@ }; 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} }); Index: gc_lang/fr/webext/content_scripts/gc_content.js ================================================================== --- gc_lang/fr/webext/content_scripts/gc_content.js +++ gc_lang/fr/webext/content_scripts/gc_content.js @@ -14,20 +14,20 @@ 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); } @@ -54,15 +54,22 @@ 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); } @@ -95,15 +102,18 @@ 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 { @@ -185,11 +195,11 @@ 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); } }, @@ -200,11 +210,11 @@ 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); } }, @@ -242,11 +252,11 @@ } } -const oGrammalecteTooltip = { +oGCPanelContent.oTooltip = { xTooltip: null, xTooltipArrow: createNode("img", { id: "grammalecte_tooltip_arrow", @@ -370,63 +380,58 @@ } } } -const oGrammalecteTextareaControl = { - - xTextarea: null, - - dParagraph: new Map(), - - setTextarea: function (xNode) { - this.xTextarea = xNode; - this.xTextarea.disabled = true; +oGCPanelContent.oTAC = { + // Text area control + + _xTextArea: null, + + _dParagraph: new Map(), + + setTextArea: function (xNode) { + this.clear(); + this._xTextArea = xNode; + this._xTextArea.disabled = true; + this._loadText(); }, clear: function () { - this.xTextarea.disabled = false; - this.xTextarea = null; - this.dParagraph.clear(); + if (this._xTextArea !== null) { + this._xTextArea.disabled = false; + this._xTextArea = null; + } + this._dParagraph.clear(); }, setParagraph (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) { - return ((iEnd = sText.indexOf("\n", iStart)) !== -1) ? sText.slice(iStart, iEnd) : sText.slice(iStart); - } - return "# Erreur. Paragraphe introuvable."; - } - return "# Erreur. Zone de texte introuvable."; - }, - - rewrite: function () { - try { - if (this.xTextarea !== null) { - let sText = ""; - let i = 0; - for (let sParagraph of this.xTextarea.value.split("\n")) { - sText += (dParagraphs.has(i)) ? dParagraphs.get(i) + "\n" : sParagraph + "\n"; - i += 1; - } - this.xTextarea.value = sText.slice(0,-1); - } - } catch (e) { - showError(e); + if (this._xTextArea !== null) { + this._dParagraph.set(iParagraph, sText); + } + }, + + _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)); + console.log("Paragraphs number: " + (i+1)); + }, + + write: function () { + if (this._xTextArea !== null) { + let sText = ""; + this._dParagraph.forEach(function (val, key) { + sText += val + "\n"; + }); + this._xTextArea.value = sText.slice(0,-1); } } } Index: gc_lang/fr/webext/gce_worker.js ================================================================== --- gc_lang/fr/webext/gce_worker.js +++ gc_lang/fr/webext/gce_worker.js @@ -190,12 +190,12 @@ 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={}) {