-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.js
60 lines (52 loc) · 1.93 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
/**
* @file
* Spellchecker plugin for CKEditor.
*
* This plugin basically wraps the library provided by wishawa/thai-spell-check
* which uses LibThai to break words and find spelling mistakes.
* @author Sushil Gupta <sushil@appsynth.net>
* @see https://github.com/wishawa/thai-spell-check
*/
let checkerFunction;
let thaiSpellcheckerReady = false;
import('./checker.js').then(function (module) {
module.loadThaiSpellchecker().then(function (r) {
checkerFunction = r;
thaiSpellcheckerReady = true;
});
});
CKEDITOR.plugins.add('spellchecker', {
icons: 'spellchecker',
init: function (editor) {
editor.addContentsCss('/libraries/spellchecker/styles/style.css');
editor.addCommand('spellCheck', {
exec: function (editor) {
if (!thaiSpellcheckerReady) {
return;
}
let textToCheck = editor.document.getBody().getText();
if (!textToCheck.length) {
return;
}
let checkResult = checkerFunction(textToCheck);
if (checkResult.length > 0) {
let editor_data = editor.getData();
for (let i = 0; i < checkResult.length; i++) {
let wrongWord = textToCheck.slice(checkResult[i][0], checkResult[i][1]);
wrongWord = wrongWord.replace(/(\r\n|\n|\r)/gm, "");
editor_data = editor_data.replace(wrongWord, '<span class="wrongWord">' + wrongWord + '</span>');
}
editor.setData(editor_data);
}
}
});
if (editor.ui.addButton) {
editor.ui.addButton('spellchecker', {
label: 'In-Trend Spell Checker',
id: 'spellchecker',
command: 'spellCheck',
toolbar: 'format,100'
});
}
}
});