Grammalecte  Check-in [360a78ac9f]

Overview
Comment:[fx] call Grammalecte via custom button and send results by event on node
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk | fx
Files: files | file ages | folders
SHA3-256: 360a78ac9fab6875a3f8b97144e5dbe117d4089a7627483a4a8f1d44e06e67a0
User & Date: olr on 2020-03-09 18:54:57
Other Links: manifest | tags
Context
2020-03-09
18:56
[fx] remove console.log() check-in: 442f6eaf7e user: olr tags: trunk, fx
18:54
[fx] call Grammalecte via custom button and send results by event on node check-in: 360a78ac9f user: olr tags: trunk, fx
18:53
[fr] ajustements check-in: 4ba1592ab7 user: olr tags: trunk, fr
Changes

Modified gc_lang/fr/webext/content_scripts/init.js from [a857d83fb7] to [646f2e9c29].

73
74
75
76
77
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
73
74
75
76
77
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







-
+


-
+


-
+


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








-
+








    clearRightClickedNode: function () {
        this.xRightClickedNode = null;
    },

    createButtons: function () {
        if (bChrome) {
            browser.storage.local.get("ui_options", this._createButtons.bind(this));
            browser.storage.local.get("ui_options", this._prepareButtons.bind(this));
            return;
        }
        browser.storage.local.get("ui_options").then(this._createButtons.bind(this), showError);
        browser.storage.local.get("ui_options").then(this._prepareButtons.bind(this), showError);
    },

    _createButtons: function (oOptions) {
    _prepareButtons: function (oOptions) {
        if (oOptions.hasOwnProperty("ui_options")) {
            this.oOptions = oOptions.ui_options;
            if (this.oOptions.textarea) {
                for (let xNode of document.getElementsByTagName("textarea")) {
                    if (xNode.style.display !== "none" && xNode.style.visibility !== "hidden" && xNode.getAttribute("spellcheck") !== "false") {
                        this.lButton.push(new GrammalecteButton(this.nButton, xNode));
                        this.nButton += 1;
                    }
                }
            }
            if (this.oOptions.editablenode) {
            // textarea
            for (let xNode of document.getElementsByTagName("textarea")) {
                if (xNode.dataset.grammalecte_callbutton && document.getElementById(xNode.dataset.grammalecte_callbutton)) {
                    let xButton = document.getElementById(xNode.dataset.grammalecte_callbutton)
                    xButton.onclick = () => {
                        oGrammalecte.startGCPanel(xNode, true, true);
                    };
                    this.lButton.push(xButton);
                    this.nButton += 1;
                }
                else if (this.oOptions.textarea  &&  xNode.style.display !== "none" && xNode.style.visibility !== "hidden" && xNode.getAttribute("spellcheck") !== "false") {
                    this.lButton.push(new GrammalecteButton(this.nButton, xNode));
                    this.nButton += 1;
                }
            }
            // editable nodes
            for (let xNode of document.querySelectorAll("[contenteditable]")) {
                if (xNode.dataset.grammalecte_callbutton && document.getElementById(xNode.dataset.grammalecte_callbutton)) {
                    let xButton = document.getElementById(xNode.dataset.grammalecte_callbutton)
                    xButton.onclick = () => {
                        oGrammalecte.startGCPanel(xNode, true, true);
                    };
                    this.lButton.push(xButton);
                    this.nButton += 1;
                }
                else if (this.oOptions.editablenode  &&  xNode.style.display !== "none" && xNode.style.visibility !== "hidden") {
                for (let xNode of document.querySelectorAll("[contenteditable]")) {
                    this.lButton.push(new GrammalecteButton(this.nButton, xNode));
                    this.nButton += 1;
                }
            }
        }
    },

    observePage: function () {
        // When a textarea is added via jascript we add the menu
        // When a textarea is added via jascript we add the buttons
        let that = this;
        this.xObserver = new MutationObserver(function (mutations) {
            mutations.forEach(function (mutation) {
                for (let i = 0;  i < mutation.addedNodes.length;  i++){
                    if (mutation.addedNodes[i].tagName == "TEXTAREA") {
                        if (that.oOptions === null || that.oOptions.textarea) {
                            oGrammalecte.lButton.push(new GrammalecteButton(oGrammalecte.nButton, mutation.addedNodes[i]));
156
157
158
159
160
161
162
163

164
165
166
167
168

169
170
171
172
173
174
175
172
173
174
175
176
177
178

179
180
181
182
183

184
185
186
187
188
189
190
191







-
+




-
+







    createMessageBox: function () {
        if (this.oMessageBox === null) {
            this.oMessageBox = new GrammalecteMessageBox("grammalecte_message_box", "Grammalecte");
            this.oMessageBox.insertIntoPage();
        }
    },

    startGCPanel: function (what, bCheckText=true) {
    startGCPanel: function (what, bCheckText=true, bResultInEvent=false) {
        this.createGCPanel();
        this.oGCPanel.clear();
        this.oGCPanel.show();
        this.oGCPanel.showEditor();
        this.oGCPanel.start(what);
        this.oGCPanel.start(what, bResultInEvent);
        this.oGCPanel.startWaitIcon();
        if (what && bCheckText) {
            let sText = this.oGCPanel.oTextControl.getText();
            xGrammalectePort.postMessage({
                sCommand: "parseAndSpellcheck",
                dParam: {sText: sText, sCountry: "FR", bDebug: false, bContext: false},
                dInfo: (what.nodeType && what.nodeType === 1) ? {sTextAreaId: what.id} : {}

Modified gc_lang/fr/webext/content_scripts/panel_gc.js from [1c3cef7a06] to [327f3b8b3c].

135
136
137
138
139
140
141
142

143
144

145
146
147
148
149
150
151
135
136
137
138
139
140
141

142
143
144
145
146
147
148
149
150
151
152







-
+


+







        this.xMenu.appendChild(this.xTFButton)
        this.xMenu.appendChild(this.xEditorButton)
        this.xMenu.appendChild(this.xLxgButton)
        this.xMenu.appendChild(this.xConjButton)
        this.xPanelBar.appendChild(this.xMenu);
    }

    start (what) {
    start (what, bResultInEvent=false) {
        this.oTooltip.hide();
        this.bWorking = false;
        this.oTextControl.bResultInEvent = bResultInEvent;
        this.clear();
        this.hideMessage();
        this.resetTimer();
        if (typeof(what) === "string") {
            // text
            this.xNode = null;
            this.oTextControl.setText(what);
950
951
952
953
954
955
956

957
958
959
960
961
962
963
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965







+








class GrammalecteTextControl {

    constructor () {
        this.xNode = null;
        this.dParagraph = new Map();
        this.bTextArea = null;
        this.bResultInEvent = false; // if true, the node content is not modified, but an event is dispatched on the node with the modified text
    }

    setNode (xNode) {
        this.clear();
        this.xNode = xNode;
        this.bTextArea = (xNode.tagName == "TEXTAREA" || xNode.tagName == "INPUT");
        if (!this.bTextArea) {
1015
1016
1017
1018
1019
1020
1021







1022

1023

1024

1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030

1031
1032
1033

1034
1035
1036
1037
1038
1039




1040
1041
1042
1043







+
+
+
+
+
+
+
-
+

+
-
+





-
-
-
-




        while (this.xNode.firstChild) {
            this.xNode.removeChild(this.xNode.firstChild);
        }
    }

    write () {
        if (this.xNode !== null) {
            if (this.bResultInEvent) {
                const xEvent = new CustomEvent("GrammalecteNodeContentUpdated", {
                    detail: { text: [...this.dParagraph.values()].join("\n").normalize("NFC") }
                });
                this.xNode.dispatchEvent(xEvent);
                console.log("event", xEvent.detail.text);
            }
            if (this.bTextArea) {
            else if (this.bTextArea) {
                this.xNode.value = this.getText();
            }
            } else {
            else {
                this.eraseNodeContent();
                this.dParagraph.forEach((val, key) => {
                    this.xNode.appendChild(document.createTextNode(val.normalize("NFC")));
                    this.xNode.appendChild(document.createElement("br"));
                });
                const xEvent = new CustomEvent("grammalecteNodeContentUpdated", {
                    detail: { text: [...this.dParagraph.values()].join("\n").normalize("NFC") }
                });
                this.xNode.dispatchEvent(xEvent);
            }
        }
    }
}