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
49
50
51
52
53
54
55
56
57
58
|
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
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
85
86
87
|
+
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
|
// JavaScript
/* jshint esversion:6, -W097 */
/* jslint esversion:6 */
/* global oGrammalecte, xGrammalectePort, showError, window, document */
"use strict";
class GrammalecteMenu {
constructor (nMenu, xNode) {
this.xNode = xNode;
this.sMenuId = "grammalecte_menu" + nMenu;
this.xButton = oGrammalecte.createNode("div", {className: "grammalecte_menu_main_button", textContent: " "});
this.xButton.onclick = () => { this.switchMenu(); };
this.xButton.style.zIndex = (xNode.style.zIndex.search(/^[0-9]+$/) !== -1) ? (parseInt(xNode.style.zIndex) + 1).toString() : xNode.style.zIndex;
this.xMenu = this._createMenu();
let xStyle = window.getComputedStyle(this.xNode);
let nMarginTop = -1 * (8 + parseInt(xStyle.marginBottom.replace('px', ''), 10));
let xNodeInsertAfter = this.xNode;
if (document.location.host == "twitter.com" && this.xNode.classList.contains('rich-editor')) {
xNodeInsertAfter = this.xNode.parentNode;
}
this.bShadow = document.body.createShadowRoot || document.body.attachShadow;
if (this.bShadow) {
let nMarginTop = -1 * (parseInt(xStyle.marginBottom.replace('px', ''), 10));
this.xShadowBtn = oGrammalecte.createNode("div", {style: "display:none;position:absolute;width:0;height:0;"});
this.xShadowBtnNode = this.xShadowBtn.attachShadow({mode: "open"});
oGrammalecte.createStyle("content_scripts/menu.css", null, this.xShadowBtnNode);
this.xShadowBtnNode.appendChild(this.xButton);
this._insertAfter(this.xShadowBtn, xNodeInsertAfter, nMarginTop);
this.xShadowMenu = oGrammalecte.createNode("div", {id: this.sMenuId+"_shadow", style: "display:none;position:absolute;width:0;height:0;"});
this.xShadowMenuNode = this.xShadowMenu.attachShadow({mode: "open"});
oGrammalecte.createStyle("content_scripts/menu.css", null, this.xShadowMenuNode);
this.xShadowMenuNode.appendChild(this.xMenu);
this._insertAfter(this.xShadowMenu, xNodeInsertAfter, nMarginTop + 8);
} else {
let nMarginTop = -1 * (8 + parseInt(xStyle.marginBottom.replace('px', ''), 10));
if (!document.getElementById("grammalecte_cssmenu")) {
oGrammalecte.createStyle("content_scripts/menu.css", "grammalecte_cssmenu", document.head);
}
this._insertAfter(this.xButton, xNodeInsertAfter, nMarginTop);
this._insertAfter(this.xMenu, xNodeInsertAfter, nMarginTop + 8);
this._insertAfter(this.xButton, xNodeInsertAfter, nMarginTop);
this._insertAfter(this.xMenu, xNodeInsertAfter, nMarginTop + 8);
}
this._createListeners();
}
_insertAfter (xNewNode, xReferenceNode, nMarginTop) {
xReferenceNode.parentNode.insertBefore(xNewNode, xReferenceNode.nextSibling);
xNewNode.style.marginTop = nMarginTop + "px";
}
_createListeners () {
this.xNode.addEventListener('focus', (e) => {
if (this.bShadow) {
this.xShadowBtn.style.display = "block";
}
this.xButton.style.display = "block";
});
/*this.xNode.addEventListener('blur', (e) => {
window.setTimeout(() => {this.xButton.style.display = "none";}, 300);
});*/
}
_getText () {
return (this.xNode.tagName == "TEXTAREA") ? this.xNode.value.normalize("NFC") : this.xNode.innerText.normalize("NFC");
}
_createMenu () {
try {
let xMenu = oGrammalecte.createNode("div", {id: this.sMenuId, className: "grammalecte_menu"});
let xCloseButton = oGrammalecte.createNode("div", {className: "grammalecte_menu_close_button", textContent: "×"} );
xCloseButton.onclick = () => {
if (this.bShadow){
this.xShadowBtn.style.display = "none";
}
this.xButton.style.display = "none";
this.switchMenu();
}
xMenu.appendChild(xCloseButton);
xMenu.appendChild(oGrammalecte.createNode("div", {className: "grammalecte_menu_header", textContent: "GRAMMALECTE"}));
// Text formatter
if (this.xNode.tagName == "TEXTAREA") {
|