Index: doc/API_web.md ================================================================== --- doc/API_web.md +++ doc/API_web.md @@ -64,24 +64,10 @@ oGrammalecteAPI.openPanelForNode(node) The node can be a textarea, an editable node or an iframe. If the node is an iframe, the content won’t be modified by Grammalecte. - -### Open the Grammalecte panel for any text - - oGrammalecteAPI.openPanelForText("your text") - -This method won’t send back any result or modified text. - - -### Open the Grammalecte panel - - oGrammalecteAPI.openPanel(param) - -Depending of what `param` is, this method will call `openPanelForNode()` or `openPanelForText()`. - ### Prevent Grammalecte to modify the node content If you don’t want Grammalecte to modify directly the node content, add the property: `data-grammalecte_result_via_event="true"`. @@ -94,10 +80,19 @@ let sText = detail.sText; ... } } + +### Open the Grammalecte panel for any text + + oGrammalecteAPI.openPanelForText("your text") + oGrammalecteAPI.openPanelForText("your text", "node_id") + oGrammalecteAPI.openPanelForText("your text", node) + +With the second parameter, Grammalecte will send an event to the node each times the text is modified within the panel. + ### Parse a node and get errors oGrammalecteAPI.parseNode("node_id") oGrammalecteAPI.parseNode(node) Index: gc_lang/fr/webext/content_scripts/api.js ================================================================== --- gc_lang/fr/webext/content_scripts/api.js +++ gc_lang/fr/webext/content_scripts/api.js @@ -7,32 +7,10 @@ // functions callable from within pages // to be sent to the content-cript via an event “GrammalecteCall” sVersion: "1.0", - openPanel: function (arg1) { - // Parameter: a text, a node, or the identifier of a node. - let xNode = null; - if (typeof(arg1) === 'string') { - if (document.getElementById(arg1)) { - xNode = document.getElementById(arg1); - } else { - this.openPanelForText(arg1); - } - } - else if (arg1 instanceof HTMLElement) { - xNode = arg1; - } - if (xNode) { - if (xNode.tagName == "INPUT" || xNode.tagName == "TEXTAREA" || xNode.tagName == "IFRAME" || xNode.isContentEditable) { - this.openPanelForNode(xNode); - } else { - this.openPanelForText(xNode.innerText); - } - } - }, - openPanelForNode: function (vNode) { // Parameter: a HTML node or the identifier of a HTML node if (vNode instanceof HTMLElement) { let xEvent = new CustomEvent("GrammalecteCall", { detail: {sCommand: "openPanelForNode", xNode: vNode} }); document.dispatchEvent(xEvent); @@ -44,14 +22,21 @@ else { console.log("[Grammalecte API] Error: parameter is not a HTML node."); } }, - openPanelForText: function (sText) { - // Parameter: text to analyze + openPanelForText: function (sText, vNode=null) { + // Parameter: text to analyze, and optionaly a node to send results to. if (typeof(sText) === "string") { - let xEvent = new CustomEvent("GrammalecteCall", { detail: {sCommand: "openPanelForText", sText: sText} }); + let xNode = null; + if (vNode instanceof HTMLElement) { + xNode = vNode; + } + else if (typeof(vNode) === "string" && document.getElementById(vNode)) { + xNode = document.getElementById(vNode); + } + let xEvent = new CustomEvent("GrammalecteCall", { detail: {sCommand: "openPanelForText", sText: sText, xNode: xNode} }); document.dispatchEvent(xEvent); } else { console.log("[Grammalecte API] Error: parameter is not a text."); } }, @@ -70,10 +55,29 @@ } else { console.log("[Grammalecte API] Error: parameter is not a HTML node or doesn’t have an identifier."); } }, + + parseText: function (sText, vNode) { + // Parameter: text to analyze, and a node to send results to. + if (typeof(sText) === "string") { + if (vNode instanceof HTMLElement && vNode.id) { + let xEvent = new CustomEvent("GrammalecteCall", { detail: {sCommand: "parseText", sText: sText, xNode: vNode} }); + document.dispatchEvent(xEvent); + } + else if (typeof(vNode) === "string" && document.getElementById(vNode)) { + let xEvent = new CustomEvent("GrammalecteCall", { detail: {sCommand: "parseText", sText: sText, xNode: document.getElementById(vNode)} }); + document.dispatchEvent(xEvent); + } + else { + console.log("[Grammalecte API] Error: parameter is not a HTML node or doesn’t have an identifier."); + } + } else { + console.log("[Grammalecte API] Error: parameter is not a text."); + } + }, getSpellSuggestions: function (sWord, sDestination, sRequestId="") { /* parameters: - sWord (string) - sDestination: HTML identifier (string) -> the result will be sent as an event “GrammalecteResult” to destination node Index: gc_lang/fr/webext/content_scripts/init.js ================================================================== --- gc_lang/fr/webext/content_scripts/init.js +++ gc_lang/fr/webext/content_scripts/init.js @@ -184,16 +184,14 @@ startGCPanel: function (what, xResultNode=null) { this.createGCPanel(); this.oGCPanel.clear(); this.oGCPanel.show(); this.oGCPanel.showEditor(); - this.oGCPanel.start(what); + this.oGCPanel.start(what, xResultNode); this.oGCPanel.startWaitIcon(); - if (what) { - let sText = this.oGCPanel.oTextControl.getText(); - oGrammalecteBackgroundPort.parseAndSpellcheck(sText, "__GrammalectePanel__"); - } + let sText = this.oGCPanel.oTextControl.getText(); + oGrammalecteBackgroundPort.parseAndSpellcheck(sText, "__GrammalectePanel__"); }, showMessage: function (sMessage) { this.createMessageBox(); this.oMessageBox.show(); @@ -532,11 +530,11 @@ oGrammalecte.startGCPanel(oCommand.xNode); } break; case "openPanelForText": if (oCommand.sText) { - oGrammalecte.startGCPanel(oCommand.sText); + oGrammalecte.startGCPanel(oCommand.sText, oCommand.xNode); } break; case "parseNode": if (oCommand.xNode && oCommand.xNode.id) { if (oCommand.xNode.tagName == "TEXTAREA" || oCommand.xNode.tagName == "INPUT") { @@ -547,10 +545,15 @@ } else { oGrammalecteBackgroundPort.parseAndSpellcheck(oCommand.xNode.innerText, oCommand.xNode.id); } } + break; + case "parseText": + if (oCommand.sText && oCommand.xNode) { + oGrammalecteBackgroundPort.parseAndSpellcheck(oCommand.sText, oCommand.xNode.id); + } break; case "getSpellSuggestions": if (oCommand.sWord) { oGrammalecteBackgroundPort.getSpellSuggestions(oCommand.sWord, oCommand.sDestination, oCommand.sErrorId); } Index: gc_lang/fr/webext/content_scripts/panel_gc.js ================================================================== --- gc_lang/fr/webext/content_scripts/panel_gc.js +++ gc_lang/fr/webext/content_scripts/panel_gc.js @@ -129,25 +129,28 @@ this.xMenu.appendChild(this.xLxgButton) this.xMenu.appendChild(this.xConjButton) this.xPanelBar.appendChild(this.xMenu); } - start (what) { + start (what, xResultNode=null) { this.oTooltip.hide(); this.bWorking = false; this.clear(); this.hideMessage(); this.resetTimer(); if (typeof(what) === "string") { // text this.xNode = null; + this.oTextControl.setResultNode(xResultNode); this.oTextControl.setText(what); - } else if (what.nodeType && what.nodeType === 1) { // 1 = Node.ELEMENT_NODE + } + else if (what.nodeType && what.nodeType === 1) { // 1 = Node.ELEMENT_NODE // node this.xNode = what; this.oTextControl.setNode(this.xNode); - } else { + } + else { // error oGrammalecte.oMessageBox.showMessage("[BUG] Analyse d’un élément inconnu…"); console.log("[Grammalecte] Unknown element:", what); } } @@ -930,10 +933,11 @@ this.xNode = null; this.dParagraph = new Map(); this.bTextArea = false; this.bIframe = false; this.bResultInEvent = false; // if true, the node content is not modified, but an event is dispatched on the node with the modified text + this.xResultNode = null; // only useful if for text analysed without node } setNode (xNode) { this.clear(); this.xNode = xNode; @@ -955,14 +959,23 @@ // editable node oGrammalecte.oGCPanel.addMessageToGCPanel("❗ La zone de texte analysée est un champ textuel enrichi susceptible de contenir des éléments non textuels qui seront effacés lors de la correction."); this.loadText(this.xNode.innerText); } } + + setResultNode (xNode) { + if (xNode instanceof HTMLElement) { + this.xResultNode = xNode; + this.bResultInEvent = true; + } + } setText (sText) { this.clear(); - oGrammalecte.oGCPanel.addMessageToGCPanel("⛔ Aucun champ textuel défini. Les changements ne seront pas répercutés sur la zone d’où le texte a été extrait."); + if (!this.xResultNode) { + oGrammalecte.oGCPanel.addMessageToGCPanel("⛔ Aucun champ textuel défini. Les changements ne seront pas répercutés sur la zone d’où le texte a été extrait."); + } this.loadText(sText); } loadText (sText) { // function also used by the text formatter @@ -987,10 +1000,11 @@ this.xNode.disabled = false; this.bTextArea = false; this.bIframe = false; this.bResultInEvent = false; this.xNode = null; + this.xResultNode = null; } this.dParagraph.clear(); } getText () { @@ -1009,11 +1023,16 @@ write () { if (this.xNode !== null) { if (this.bResultInEvent) { const xEvent = new CustomEvent("GrammalecteResult", { detail: JSON.stringify({ sType: "text", sText: this.getText() }) }); - this.xNode.dispatchEvent(xEvent); + if (this.xNode) { + this.xNode.dispatchEvent(xEvent); + } + else if (this.xResultNode) { + this.xResultNode.dispatchEvent(xEvent); + } //console.log("Text sent via an event :", xEvent.detail); } else if (this.bTextArea) { this.xNode.value = this.getText(); } Index: grammalecte-cli.py ================================================================== --- grammalecte-cli.py +++ grammalecte-cli.py @@ -323,15 +323,14 @@ break elif sText.startswith("/rl"): # reload (todo) pass elif sText.startswith("$"): - for sParagraph in txt.getParagraph(sText): - sText = sText[1:] + for sParagraph in txt.getParagraph(sText[1:]): if xArgs.textformatter: - sText = oTextFormatter.formatText(sParagraph) - lParagraphErrors, lSentences = oGrammarChecker.gce.parse(sText, bDebug=xArgs.debug, bFullInfo=True) + sParagraph = oTextFormatter.formatText(sParagraph) + lParagraphErrors, lSentences = oGrammarChecker.gce.parse(sParagraph, bDebug=xArgs.debug, bFullInfo=True) echo(txt.getReadableErrors(lParagraphErrors, xArgs.width)) for dSentence in lSentences: echo("{nStart}:{nEnd}".format(**dSentence)) echo(" <" + dSentence["sSentence"]+">") for dToken in dSentence["lToken"]: @@ -342,11 +341,11 @@ "·".join(dToken.get("aTags", "")) ) ) echo(txt.getReadableErrors(dSentence["lGrammarErrors"], xArgs.width)) else: for sParagraph in txt.getParagraph(sText): if xArgs.textformatter: - sText = oTextFormatter.formatText(sParagraph) + sParagraph = oTextFormatter.formatText(sParagraph) sRes, _ = oGrammarChecker.getParagraphWithErrors(sParagraph, bEmptyIfNoErrors=xArgs.only_when_errors, nWidth=xArgs.width, bDebug=xArgs.debug) if sRes: echo("\n" + sRes) else: echo("\nNo error found.")