-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathspell-check-handler.js
656 lines (552 loc) · 21.9 KB
/
spell-check-handler.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
const {spawn} = require('spawn-rx');
const {requireTaskPool} = require('electron-remote');
const LRU = require('lru-cache');
const {Subscription} = require('rxjs/Subscription');
const {Observable} = require('rxjs/Observable');
const {Subject} = require('rxjs/Subject');
const SerialSubscription = require('rxjs-serial-subscription').default;
require('rxjs/add/observable/defer');
require('rxjs/add/observable/empty');
require('rxjs/add/observable/fromEvent');
require('rxjs/add/observable/fromPromise');
require('rxjs/add/observable/of');
require('rxjs/add/operator/catch');
require('rxjs/add/operator/concat');
require('rxjs/add/operator/concatMap');
require('rxjs/add/operator/do');
require('rxjs/add/operator/filter');
require('rxjs/add/operator/mergeMap');
require('rxjs/add/operator/merge');
require('rxjs/add/operator/observeOn');
require('rxjs/add/operator/reduce');
require('rxjs/add/operator/startWith');
require('rxjs/add/operator/take');
require('rxjs/add/operator/takeUntil');
require('rxjs/add/operator/throttle');
require('rxjs/add/operator/toPromise');
require('./custom-operators');
const DictionarySync = require('./dictionary-sync');
const {normalizeLanguageCode} = require('./utility');
let Spellchecker;
let d = require('debug')('electron-spellchecker:spell-check-handler');
const cld = requireTaskPool(require.resolve('./cld2'));
let fallbackLocaleTable = null;
let webFrame = (process.type === 'renderer' ?
require('electron').webFrame :
null);
// NB: Linux and Windows uses underscore in languages (i.e. 'en_US'), whereas
// we're trying really hard to match the Chromium way of `en-US`
const validLangCodeWindowsLinux = /[a-z]{2}[_][A-Z]{2}/;
const isMac = process.platform === 'darwin';
// NB: This is to work around electron/electron#1005, where contractions
// are incorrectly marked as spelling errors. This lets people get away with
// incorrectly spelled contracted words, but it's the best we can do for now.
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;
}, {});
const alternatesTable = {};
/**
* This method mimics Observable.fromEvent, but with capture semantics.
*/
function fromEventCapture(element, name) {
return Observable.create((subj) => {
const handler = function(...args) {
if (args.length > 1) {
subj.next(args);
} else {
subj.next(args[0] || true);
}
};
element.addEventListener(name, handler, true);
return new Subscription(() => element.removeEventListener(name, handler, true));
});
}
/**
* SpellCheckHandler is the main class of this library, and handles all of the
* different pieces of spell checking except for the context menu information.
*
* Instantiate the class, then call {{attachToInput}} to wire it up. The spell
* checker will attempt to automatically check the language that the user is
* typing in and switch on-the fly. However, giving it an explicit hint by
* calling {{switchLanguage}}, or providing it a block of sample text via
* {{provideHintText}} will result in much better results.
*
* Sample text should be text that is reasonably likely to be in the same language
* as the user typing - for example, in an Email reply box, the original Email text
* would be a great sample, or in the case of Slack, the existing channel messages
* are used as the sample text.
*/
module.exports = class SpellCheckHandler {
/**
* Constructs a SpellCheckHandler
*
* @param {DictionarySync} dictionarySync An instance of {{DictionarySync}},
* create a custom one if you want
* to override the dictionary cache
* location.
* @param {LocalStorage} localStorage Deprecated.
* @param {Scheduler} scheduler The Rx scheduler to use, for
* testing.
*/
constructor(dictionarySync=null, localStorage=null, scheduler=null) {
// NB: Require here so that consumers can handle native module exceptions.
Spellchecker = require('./node-spellchecker').Spellchecker;
this.dictionarySync = dictionarySync || new DictionarySync();
this.switchToLanguage = new Subject();
this.currentSpellchecker = null;
this.currentSpellcheckerLanguage = null;
this.currentSpellcheckerChanged = new Subject();
this.spellCheckInvoked = new Subject();
this.spellingErrorOccurred = new Subject();
this.isMisspelledCache = new LRU({
max: 512, maxAge: 4 * 1000
});
this.scheduler = scheduler;
this.shouldAutoCorrect = true;
this._automaticallyIdentifyLanguages = true;
this.disp = new SerialSubscription();
if (isMac) {
// NB: OS X does automatic language detection, we're gonna trust it
this.currentSpellchecker = new Spellchecker();
this.currentSpellcheckerLanguage = 'en-US';
if (webFrame) {
webFrame.setSpellCheckProvider(
this.currentSpellcheckerLanguage,
this.shouldAutoCorrect,
{ spellCheck: this.handleElectronSpellCheck.bind(this) });
}
return;
}
}
/**
* Is the spellchecker trying to detect the typed language automatically?
*/
get automaticallyIdentifyLanguages() {
return this._automaticallyIdentifyLanguages;
}
/**
* Is the spellchecker trying to detect the typed language automatically?
*/
set automaticallyIdentifyLanguages(value) {
this._automaticallyIdentifyLanguages = !!value;
// Calling `setDictionary` on the macOS implementation of `@nornagon/spellchecker`
// is the only way to set the `automaticallyIdentifyLanguages` property on the
// native NSSpellchecker. Calling switchLanguage with a language will set it `false`,
// while calling it with an empty language will set it to `true`
if (isMac && !!value) {
this.switchLanguage();
} else if (isMac && !!value && this.currentSpellcheckerLanguage) {
this.switchLanguage(this.currentSpellcheckerLanguage);
}
}
/**
* Disconnect the events that we connected in {{attachToInput}} or other places
* in the class.
*/
unsubscribe() {
this.disp.unsubscribe();
}
/**
* Override the default logger for this class. You probably want to use
* {{setGlobalLogger}} instead
*
* @param {Function} fn The function which will operate like console.log
*/
static setLogger(fn) {
d = fn;
}
/**
* Attach to document.body and register ourselves for Electron spell checking.
* This method will start to watch text entered by the user and automatically
* switch languages as well as enable Electron spell checking (i.e. the red
* squigglies).
*
* @param {Observable<String>} inputText Simulate the user typing text,
* for testing.
*
* @return {Disposable} A Disposable which will unregister all of the
* things that this method registered.
*/
attachToInput(inputText=null) {
// OS X has no need for any of this
if (isMac && !inputText) {
return Subscription.EMPTY;
}
let possiblySwitchedCharacterSets = new Subject();
let wordsTyped = 0;
if (!inputText && !document.body) {
throw new Error("document.body is null, if you're calling this in a preload script you need to wrap it in a setTimeout");
}
let input = inputText || (fromEventCapture(document.body, 'input')
.mergeMap((e) => {
if (!e.target || !e.target.value) return Observable.empty();
if (e.target.value.match(/\S\s$/)) {
wordsTyped++;
}
if (wordsTyped > 2) {
d(`${wordsTyped} words typed without spell checking invoked, redetecting language`);
possiblySwitchedCharacterSets.next(true);
}
return Observable.of(e.target.value);
}));
let disp = new Subscription();
// NB: When users switch character sets (i.e. we're checking in English and
// the user suddenly starts typing in Russian), the spellchecker will no
// longer invoke us, so we don't have a chance to re-detect the language.
//
// If we see too many words typed without a spelling detection, we know we
// should start rechecking the input box for a language change.
disp.add(Observable.merge(this.spellCheckInvoked, this.currentSpellcheckerChanged)
.subscribe(() => wordsTyped = 0));
let lastInputText = '';
disp.add(input.subscribe((x) => lastInputText = x));
let initialInputText = input
.guaranteedThrottle(250, this.scheduler)
.takeUntil(this.currentSpellcheckerChanged);
if (this.currentSpellcheckerLanguage) {
initialInputText = Observable.empty();
}
let contentToCheck = Observable.merge(
this.spellingErrorOccurred,
initialInputText,
possiblySwitchedCharacterSets)
.mergeMap(() => {
if (lastInputText.length < 8) return Observable.empty();
return Observable.of(lastInputText);
});
let languageDetectionMatches = contentToCheck
.filter(() => this.automaticallyIdentifyLanguages)
.mergeMap((text) => {
d(`Attempting detection, string length: ${text.length}`);
if (text.length > 256) {
text = text.substr(text.length - 256);
}
return Observable.fromPromise(this.detectLanguageForText(text))
.catch(() => Observable.empty());
});
disp.add(languageDetectionMatches
.mergeMap(async (langWithoutLocale) => {
d(`Auto-detected language as ${langWithoutLocale}`);
let lang = await this.getLikelyLocaleForLanguage(langWithoutLocale);
if (lang !== this.currentSpellcheckerLanguage) await this.switchLanguage(lang);
return lang;
})
.catch((e) => {
d(`Failed to load dictionary: ${e.message}`);
return Observable.empty();
})
.subscribe(async (lang) => {
d(`New Language is ${lang}`);
}));
if (webFrame) {
let prevSpellCheckLanguage;
disp.add(this.currentSpellcheckerChanged
.startWith(true)
.filter(() => this.currentSpellcheckerLanguage)
.subscribe(() => {
if (prevSpellCheckLanguage === this.currentSpellcheckerLanguage) return;
d('Actually installing spell check provider to Electron');
webFrame.setSpellCheckProvider(
this.currentSpellcheckerLanguage,
this.shouldAutoCorrect,
{ spellCheck: this.handleElectronSpellCheck.bind(this) });
prevSpellCheckLanguage = this.currentSpellcheckerLanguage;
}));
}
this.disp.add(disp);
return disp;
}
/**
* autoUnloadDictionariesOnBlur attempts to save memory by unloading
* dictionaries when the window loses focus.
*
* @return {Disposable} A {{Disposable}} that will unhook the events listened
* to by this method.
*/
autoUnloadDictionariesOnBlur() {
let ret = new Subscription();
let hasUnloaded = false;
if (isMac) return Subscription.EMPTY;
ret.add(Observable.fromEvent(window, 'blur').subscribe(() => {
d(`Unloading spellchecker`);
this.currentSpellchecker = null;
hasUnloaded = true;
}));
ret.add(Observable.fromEvent(window, 'focus').mergeMap(() => {
if (!hasUnloaded) return Observable.empty();
if (!this.currentSpellcheckerLanguage) return Observable.empty();
d(`Restoring spellchecker`);
return Observable.fromPromise(this.switchLanguage(this.currentSpellcheckerLanguage))
.catch((e) => {
d(`Failed to restore spellchecker: ${e.message}`);
return Observable.empty();
});
}).subscribe());
return ret;
}
/**
* Switch the dictionary language to the language of the sample text provided.
* As described in the class documentation, call this method with text most
* likely in the same language as the user is typing. The locale (i.e. *US* vs
* *UK* vs *AU*) will be inferred heuristically based on the user's computer.
*
* @param {String} inputText A language code (i.e. 'en-US')
*
* @return {Promise} Completion
*/
async provideHintText(inputText) {
let langWithoutLocale = null;
if (isMac) return;
try {
langWithoutLocale = await this.detectLanguageForText(inputText.substring(0, 512));
} catch (e) {
d(`Couldn't detect language for text of length '${inputText.length}': ${e.message}, ignoring sample`);
return;
}
let lang = await this.getLikelyLocaleForLanguage(langWithoutLocale);
await this.switchLanguage(lang);
}
/**
* Explicitly switch the language to a specific language. This method will
* automatically download the dictionary for the specific language and locale
* and on failure, will attempt to switch to dictionaries that are the same
* language but a default locale.
*
* @param {String} langCode A language code (i.e. 'en-US')
*
* @return {Promise} Completion
*/
async switchLanguage(langCode) {
let actualLang;
let dict = null;
// Set language on macOS
if (isMac && this.currentSpellchecker) {
d(`Setting current spellchecker to ${langCode}`);
this.currentSpellcheckerLanguage = langCode;
return this.currentSpellchecker.setDictionary(langCode);
}
// Set language on Linux & Windows (Hunspell)
this.isMisspelledCache.reset();
try {
const {dictionary, language} = await this.loadDictionaryForLanguageWithAlternatives(langCode);
actualLang = language; dict = dictionary;
} catch (e) {
d(`Failed to load dictionary ${langCode}: ${e.message}`);
throw e;
}
if (!dict) {
d(`dictionary for ${langCode}_${actualLang} is not available`);
this.currentSpellcheckerLanguage = actualLang;
this.currentSpellchecker = null;
this.currentSpellcheckerChanged.next(true);
return;
}
d(`Setting current spellchecker to ${actualLang}, requested language was ${langCode}`);
if (this.currentSpellcheckerLanguage !== actualLang || !this.currentSpellchecker) {
d(`Creating node-spellchecker instance`);
this.currentSpellchecker = new Spellchecker();
this.currentSpellchecker.setDictionary(actualLang, dict);
this.currentSpellcheckerLanguage = actualLang;
this.currentSpellcheckerChanged.next(true);
}
}
/**
* Loads a dictionary and attempts to use fallbacks if it fails.
* @private
*/
async loadDictionaryForLanguageWithAlternatives(langCode, cacheOnly=false) {
this.fallbackLocaleTable = this.fallbackLocaleTable || require('./fallback-locales');
let lang = langCode.split(/[-_]/)[0];
let alternatives = [langCode, await this.getLikelyLocaleForLanguage(lang), this.fallbackLocaleTable[lang]];
if (langCode in alternatesTable) {
try {
return {
language: alternatesTable[langCode],
dictionary: await this.dictionarySync.loadDictionaryForLanguage(alternatesTable[langCode])
};
} catch (e) {
d(`Failed to load language ${langCode}, altTable=${alternatesTable[langCode]}`);
delete alternatesTable[langCode];
}
}
d(`Requesting to load ${langCode}, alternatives are ${JSON.stringify(alternatives)}`);
return await Observable.of(...alternatives)
.concatMap((l) => {
return Observable.defer(() =>
Observable.fromPromise(this.dictionarySync.loadDictionaryForLanguage(l, cacheOnly)))
.map((d) => ({language: l, dictionary: d}))
.do(({language}) => {
alternatesTable[langCode] = language;
})
.catch(() => Observable.of(null));
})
.concat(Observable.of({language: langCode, dictionary: null}))
.filter((x) => x !== null)
.take(1)
.toPromise();
}
/**
* The actual callout called by Electron to handle spellchecking
* @private
*/
handleElectronSpellCheck(text) {
if (!this.currentSpellchecker) return true;
if (isMac) {
return !this.isMisspelled(text);
}
this.spellCheckInvoked.next(true);
let result = this.isMisspelled(text);
if (result) this.spellingErrorOccurred.next(text);
return !result;
}
/**
* Calculates whether a word is missspelled, using an LRU cache to memoize
* the callout to the actual spell check code.
*
* @private
*/
isMisspelled(text) {
let result = this.isMisspelledCache.get(text);
if (result !== undefined) {
return result;
}
result = (() => {
if (contractionMap[text.toLocaleLowerCase()]) {
return false;
}
if (!this.currentSpellchecker) return false;
if (isMac) {
return this.currentSpellchecker.isMisspelled(text);
}
// NB: I'm not smart enough to fix this bug in Chromium's version of
// Hunspell so I'm going to fix it here instead. Chromium Hunspell for
// whatever reason marks the first word in a sentence as mispelled if it is
// capitalized.
result = this.currentSpellchecker.checkSpelling(text);
if (result.length < 1) {
return false;
}
if (result[0].start !== 0) {
// If we're not at the beginning, we know it's not a false positive
return true;
}
// Retry with lowercase
return this.currentSpellchecker.isMisspelled(text.toLocaleLowerCase());
})();
this.isMisspelledCache.set(text, result);
return result;
}
/**
* Calls out to cld2 to detect the language of the given text
* @private
*/
detectLanguageForText(text) {
return new Promise((res,rej) => {
setTimeout(() => cld.detect(text).then(res, rej), 10);
});
}
/**
* Returns the locale for a language code based on the user's machine (i.e.
* 'en' => 'en-GB')
*/
async getLikelyLocaleForLanguage(language) {
let lang = language.toLowerCase();
if (!this.likelyLocaleTable) this.likelyLocaleTable = await this.buildLikelyLocaleTable();
if (this.likelyLocaleTable[lang]) return this.likelyLocaleTable[lang];
this.fallbackLocaleTable = this.fallbackLocaleTable || require('./fallback-locales');
return this.fallbackLocaleTable[lang];
}
/**
* A proxy for the current spellchecker's method of the same name
* @private
*/
async getCorrectionsForMisspelling(text) {
// NB: This is async even though we don't use await, to make it easy for
// ContextMenuBuilder to use this method even when it's hosted in another
// renderer process via electron-remote.
if (!this.currentSpellchecker) {
return null;
}
return this.currentSpellchecker.getCorrectionsForMisspelling(text);
}
/**
* A proxy for the current spellchecker's method of the same name
* @private
*/
async addToDictionary(text) {
// NB: Same deal as getCorrectionsForMisspelling.
if (!isMac) return;
if (!this.currentSpellchecker) return;
this.currentSpellchecker.add(text);
}
/**
* Call out to the OS to figure out what locales the user is probably
* interested in then save it off as a table.
* @private
*/
async buildLikelyLocaleTable() {
let localeList = [];
if (process.platform === 'linux') {
let locales = await spawn('locale', ['-a'])
.reduce((acc,x) => { acc.push(...x.split('\n')); return acc; }, [])
.toPromise()
.catch(() => []);
d(`Raw Locale list: ${JSON.stringify(locales)}`);
localeList = locales.reduce((acc, x) => {
let m = x.match(validLangCodeWindowsLinux);
if (!m) return acc;
acc.push(m[0]);
return acc;
}, []);
}
if (process.platform === 'win32') {
localeList = require('keyboard-layout').getInstalledKeyboardLanguages();
}
if (isMac) {
fallbackLocaleTable = fallbackLocaleTable || require('./fallback-locales');
// NB: OS X will return lists that are half just a language, half
// language + locale, like ['en', 'pt_BR', 'ko']
localeList = this.currentSpellchecker.getAvailableDictionaries()
.map((x => {
if (x.length === 2) return fallbackLocaleTable[x];
return normalizeLanguageCode(x);
}));
}
d(`Filtered Locale list: ${JSON.stringify(localeList)}`);
// Some distros like Ubuntu make locale -a useless by dumping
// every possible locale for the language into the list :-/
let counts = localeList.reduce((acc,x) => {
let k = x.split(/[-_\.]/)[0];
acc[k] = acc[k] || [];
acc[k].push(x);
return acc;
}, {});
d(`Counts: ${JSON.stringify(counts)}`);
let ret = Object.keys(counts).reduce((acc, x) => {
if (counts[x].length > 1) return acc;
d(`Setting ${x}`);
acc[x] = normalizeLanguageCode(counts[x][0]);
return acc;
}, {});
// NB: LANG has a Special Place In Our Hearts
if (process.platform === 'linux' && process.env.LANG) {
let m = process.env.LANG.match(validLangCodeWindowsLinux);
if (!m) return ret;
ret[m[0].split(/[-_\.]/)[0]] = normalizeLanguageCode(m[0]);
}
d(`Result: ${JSON.stringify(ret)}`);
return ret;
}
}