-
Notifications
You must be signed in to change notification settings - Fork 715
/
SpellCheck.js
316 lines (274 loc) · 10.9 KB
/
SpellCheck.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
const os = require('os');
const checker = require('spellchecker');
const { remote, webFrame } = require('electron');
const webContents = remote.getCurrentWebContents();
let menu = new remote.Menu();
const path = remote.require('path');
const isWindows = ['win32', 'win64'].indexOf(os.platform()) !== -1;
class SpellCheck {
get userLanguage () {
const lang = localStorage.getItem('userLanguage');
if (lang) {
return lang.replace('-', '_');
}
}
get dictionaries () {
const dictionaries = localStorage.getItem('spellcheckerDictionaries');
if (dictionaries) {
const result = JSON.parse(dictionaries);
if (Array.isArray(result)) {
return result;
}
}
}
constructor () {
this.enabledDictionaries = [];
this.contractions = this.getContractions();
this.loadAvailableDictionaries();
this.setEnabledDictionaries();
this.languagesMenu = {
label: 'Spelling languages',
submenu: this.availableDictionaries.map((dictionary) => {
const menu = {
label: dictionary,
type: 'checkbox',
checked: this.enabledDictionaries.indexOf(dictionary) !== -1,
click: (menuItem) => {
menu.checked = menuItem.checked;
// If not using os dictionary then limit to only 1 language
if (!this.multiLanguage) {
this.languagesMenu.submenu.forEach((m) => {
if (m.label !== menuItem.label) {
m.checked = false;
}
});
}
if (menuItem.checked) {
this.setEnabled(dictionary);
} else {
this.disable(dictionary);
}
this.saveEnabledDictionaries();
}
};
return menu;
})
};
}
/**
* Set enabled dictionaries on load
* Either sets enabled dictionaries to saved preferences, or enables the first
* dictionary that is valid based on system (defaults to en_US)
*/
setEnabledDictionaries () {
const dictionaries = this.dictionaries;
if (dictionaries) {
// Dictionary disabled
if (dictionaries.length === 0) {
return;
}
if (this.setEnabled(dictionaries)) {
return;
}
}
if (this.userLanguage) {
if (this.setEnabled(this.userLanguage)) {
return;
}
if (this.userLanguage.includes('_') && this.setEnabled(this.userLanguage.split('_')[0])) {
return;
}
}
const navigatorLanguage = navigator.language.replace('-', '_');
if (this.setEnabled(navigatorLanguage)) {
return;
}
if (navigatorLanguage.includes('_') && this.setEnabled(navigatorLanguage.split('_')[0])) {
return;
}
if (this.setEnabled('en_US')) {
return;
}
if (!this.setEnabled('en')) {
console.info('Unable to set a language for the spell checker - Spell checker is disabled');
}
}
loadAvailableDictionaries () {
this.availableDictionaries = checker.getAvailableDictionaries().sort();
if (this.availableDictionaries.length === 0) {
this.multiLanguage = false;
// Dictionaries path is correct for build
this.dictionariesPath = path.join(remote.app.getAppPath(), '../dictionaries');
this.availableDictionaries = [
'en_GB',
'en_US',
'es_ES',
'pt_BR'
];
} else {
this.multiLanguage = !isWindows;
this.availableDictionaries = this.availableDictionaries.map((dict) => dict.replace('-', '_'));
}
}
setEnabled (dictionaries) {
dictionaries = [].concat(dictionaries);
let result = false;
for (let i = 0; i < dictionaries.length; i++) {
if (this.availableDictionaries.indexOf(dictionaries[i]) !== -1) {
result = true;
this.enabledDictionaries.push(dictionaries[i]);
// If using Hunspell or Windows then only allow 1 language for performance reasons
if (!this.multiLanguage) {
this.enabledDictionaries = [dictionaries[i]];
checker.setDictionary(dictionaries[i], this.dictionariesPath);
return true;
}
}
}
return result;
}
disable (dictionary) {
const pos = this.enabledDictionaries.indexOf(dictionary);
if (pos !== -1) {
this.enabledDictionaries.splice(pos, 1);
}
}
getContractions () {
const contractions = [
"ain't", "aren't", "can't", "could've", "couldn't", "couldn't've", "didn't", "doesn't", "don't", "hadn't",
"hadn't've", "hasn't", "haven't", "he'd", "he'd've", "he'll", "he's", "how'd", "how'll", "how's", "I'd",
"I'd've", "I'll", "I'm", "I've", "isn't", "it'd", "it'd've", "it'll", "it's", "let's", "ma'am", "mightn't",
"mightn't've", "might've", "mustn't", "must've", "needn't", "not've", "o'clock", "shan't", "she'd", "she'd've",
"she'll", "she's", "should've", "shouldn't", "shouldn't've", "that'll", "that's", "there'd", "there'd've",
"there're", "there's", "they'd", "they'd've", "they'll", "they're", "they've", "wasn't", "we'd", "we'd've",
"we'll", "we're", "we've", "weren't", "what'll", "what're", "what's", "what've", "when's", "where'd",
"where's", "where've", "who'd", "who'll", "who're", "who's", "who've", "why'll", "why're", "why's", "won't",
"would've", "wouldn't", "wouldn't've", "y'all", "y'all'd've", "you'd", "you'd've", "you'll", "you're", "you've"
];
const contractionMap = contractions.reduce((acc, word) => {
acc[word.replace(/'.*/, '')] = true;
return acc;
}, {});
return contractionMap;
}
enable () {
webFrame.setSpellCheckProvider('', false, {
spellCheck: (text) => this.isCorrect(text)
});
this.setupContextMenuListener();
}
getMenu () {
return [
{
label: 'Undo',
role: 'undo'
},
{
label: 'Redo',
role: 'redo'
},
{
type: 'separator'
},
{
label: 'Cut',
role: 'cut',
accelerator: 'CommandOrControl+X',
},
{
label: 'Copy',
role: 'copy',
accelerator: 'CommandOrControl+C',
},
{
label: 'Paste',
role: 'paste',
accelerator: 'CommandOrControl+V',
},
{
label: 'Select All',
role: 'selectall',
accelerator: 'CommandOrControl+A',
}
];
}
saveEnabledDictionaries () {
localStorage.setItem('spellcheckerDictionaries', JSON.stringify(this.enabledDictionaries));
}
isCorrect (text) {
if (!this.enabledDictionaries.length || this.contractions[text.toLocaleLowerCase()]) {
return true;
}
if (this.multiLanguage) {
for (let i = 0; i < this.enabledDictionaries.length; i++) {
checker.setDictionary(this.enabledDictionaries[i]);
if (!checker.isMisspelled(text)) {
return true;
}
}
} else {
return !checker.isMisspelled(text);
}
return false;
}
getCorrections (text) {
if (!this.multiLanguage) {
return checker.getCorrectionsForMisspelling(text);
}
const allCorrections = this.enabledDictionaries.map((dictionary) => {
checker.setDictionary(dictionary);
return checker.getCorrectionsForMisspelling(text);
}).filter((c) => c.length > 0);
const length = Math.max(...allCorrections.map((a) => a.length));
// Get the best suggestions of each language first
const corrections = [];
for (let i = 0; i < length; i++) {
corrections.push(...allCorrections.map((c) => c[i]).filter((c) => c));
}
// Remove duplicates
return [...new Set(corrections)];
}
setupContextMenuListener () {
window.addEventListener('contextmenu', (event) => {
event.preventDefault();
const template = this.getMenu();
if (this.languagesMenu) {
template.unshift({ type: 'separator' });
template.unshift(this.languagesMenu);
}
setTimeout(() => {
if (['TEXTAREA', 'INPUT'].indexOf(event.target.nodeName) > -1) {
const text = window.getSelection().toString().trim();
if (text !== '' && !this.isCorrect(text)) {
const options = this.getCorrections(text);
const maxItems = Math.min(options.length, 6);
if (maxItems > 0) {
const suggestions = [];
const onClick = function (menuItem) {
webContents.replaceMisspelling(menuItem.label);
};
for (let i = 0; i < options.length; i++) {
const item = options[i];
suggestions.push({ label: item, click: onClick });
}
template.unshift({ type: 'separator' });
if (suggestions.length > maxItems) {
const moreSuggestions = {
label: 'More spelling suggestions',
submenu: suggestions.slice(maxItems)
};
template.unshift(moreSuggestions);
}
template.unshift.apply(template, suggestions.slice(0, maxItems));
} else {
template.unshift({ label: 'No suggestions', enabled: false });
}
}
}
menu = remote.Menu.buildFromTemplate(template);
menu.popup(remote.getCurrentWindow(), undefined, undefined, 5);
}, 0);
}, false);
}
}
module.exports = SpellCheck;