-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.js
193 lines (157 loc) · 5.37 KB
/
plugin.js
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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
tinymce.PluginManager.add('suggestions', function(editor) {
const searchFunction = editor.getParam("search_function", null);
const debounceTime = editor.getParam("debounce_time", 250); //ms
const suggestionTextColor = editor.getParam("suggestion_text_color", '#dee7f1'); //hex
const suggestionNodeId = editor.getParam("suggestion_node_id", 'suggestionNodeId');
const minLengthSearch = editor.getParam("min_search_length", 3);
const terms_list = editor.getParam("terms_list", []);
let suggestionNode;
let waiting = false;
let waitingText;
const excludedKeys = [
'Space',
'Enter',
'NumpadEnter',
'Delete',
'Escape'
]
const neutralKeys = [
'ShiftLeft',
'ShiftRight',
'AltRight',
'AltLeft',
'ControlLeft',
'ControlRight'
]
function debounce(cb, delay = debounceTime) {
let timeout
return (...args) => {
clearTimeout(timeout)
timeout = setTimeout(() => {
cb(...args)
}, delay)
}
}
async function findWord( txt ) {
if(searchFunction == null){
return terms_list.find(elm => elm.toLocaleLowerCase().startsWith(txt.toLocaleLowerCase()));
}else{
let result = await searchFunction(txt);
if(Array.isArray(result)){
return result.find(elm => elm.toLocaleLowerCase().startsWith(txt.toLocaleLowerCase()));
}else{
return result.toLocaleLowerCase().startsWith(txt.toLocaleLowerCase()) ? result : undefined
}
}
}
function cancelEvent(e){
e.stopPropagation();
e.preventDefault();
}
function removeSuggestionNode(){
suggestionNode && suggestionNode.remove();
suggestionNode = undefined;
}
function getTheWordToCheck(theText, theSub){
let re = new RegExp(`${theSub}$`, "gi")
let minus = theText.replace(re, "");
return minus.split(' ').pop();
}
function keyCheck( keyCode ){
return excludedKeys.indexOf(keyCode) == -1
}
function getCompletion(typed, word) {
let re = new RegExp(`^${typed}`, "gi")
return word.replace(re, "");
}
function insertCompletionNode(ed, txt){
removeSuggestionNode();
let rng = ed.selection.getRng(1);
suggestionNode = ed.getDoc().createElement("span");
suggestionNode.style.color = suggestionTextColor;
suggestionNode.innerHTML = txt;
suggestionNode.id = suggestionNodeId;
let offset = ed.selection.getSel().focusOffset;
rng.insertNode(suggestionNode);
let nodeSuggestion = ed.dom.select(`#${suggestionNodeId}`);
if(nodeSuggestion[0].lastChild != null){
ed.selection.setCursorLocation(nodeSuggestion[0].firstChild, 0);
}
ed.selection.collapse(0);
waiting = true;
waitingText = txt;
}
function resetSuggestion() {
waiting = false;
waitingText = undefined;
}
function completeWord(ed) {
removeSuggestionNode();
ed.execCommand('mceInsertContent', false, waitingText);
ed.selection.collapse(1);
resetSuggestion();
}
function changeSuggestionDom(ed, key){
if(waitingText && waitingText.toLocaleLowerCase().startsWith(key.toLocaleLowerCase())){
waitingText = waitingText.substring(1);
removeSuggestionNode()
insertCompletionNode(ed, waitingText)
}else{
removeSuggestionNode()
resetSuggestion();
}
}
async function startProcess(ed){
let offset = ed.selection.getSel().focusOffset;
let rng = ed.selection.getRng(1);
let theText = rng.startContainer.data;
if(theText){
let theSub = theText.substring(offset);
if(theSub.length == 0 || theSub.match(/^\s/g)){
const wordToCheck = getTheWordToCheck(theText, theSub);
if(wordToCheck.length >= minLengthSearch){
let wordFound = await findWord(wordToCheck);
if(wordFound != undefined){
let substr = getCompletion(wordToCheck, wordFound)
insertCompletionNode(ed, substr);
}
}
}
}else{
//TODO
}
}
let process = debounce(async function(event, editor) {
if(keyCheck(event.code)){
startProcess(editor)
}
var name = event.key;
var char = event.code;
});
editor.on('keyup', (event) => {
if(!waiting){
process(event, editor)
}
});
editor.on('keydown', (event) => {
if(event.code == 'Tab' && waiting){
cancelEvent(event)
completeWord(editor)
}else{
if(!keyCheck(event.code)){
removeSuggestionNode();
resetSuggestion();
}else if(waiting && neutralKeys.indexOf(event.code) == -1){
changeSuggestionDom(editor, event.key)
}
}
});
editor.on('blur', (e) => {
removeSuggestionNode();
resetSuggestion();
})
editor.on('click', (e) => {
removeSuggestionNode();
resetSuggestion();
})
});