Skip to content
This repository was archived by the owner on Apr 8, 2021. It is now read-only.

Commit 82ad22c

Browse files
committed
Add support for Electron 5 and above
Starting with Electron 5.0.0, the function `webFrame.setSpellCheckProvider` has a different signature, in order to support asynchronous spell checkers. For more information on this change in Electron, see electron/electron#14032. This diff updates `SpellCheckHandler` to use the new interface if running on Electron 5.0.0 and above. However, it still checks the spelling synchronously like before. If running on Electron versions below 5.0.0, the `SpellCheckHandler` still works. Closes electron-userland#144.
1 parent 6d180a7 commit 82ad22c

File tree

1 file changed

+59
-17
lines changed

1 file changed

+59
-17
lines changed

src/spell-check-handler.js

+59-17
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,9 @@ module.exports = class SpellCheckHandler {
140140
this.currentSpellchecker = new Spellchecker();
141141
this.currentSpellcheckerLanguage = 'en-US';
142142

143+
d('DEBUG: Settings Spell Check Provider...');
143144
if (webFrame) {
144-
webFrame.setSpellCheckProvider(
145-
this.currentSpellcheckerLanguage,
146-
this.shouldAutoCorrect,
147-
{ spellCheck: this.handleElectronSpellCheck.bind(this) });
145+
this.setSpellCheckProvider(webFrame);
148146
}
149147
return;
150148
}
@@ -255,9 +253,9 @@ module.exports = class SpellCheckHandler {
255253
}
256254

257255
let contentToCheck = Observable.merge(
258-
this.spellingErrorOccurred,
259-
initialInputText,
260-
possiblySwitchedCharacterSets)
256+
this.spellingErrorOccurred,
257+
initialInputText,
258+
possiblySwitchedCharacterSets)
261259
.mergeMap(() => {
262260
if (lastInputText.length < 8) return Observable.empty();
263261
return Observable.of(lastInputText);
@@ -295,16 +293,13 @@ module.exports = class SpellCheckHandler {
295293
let prevSpellCheckLanguage;
296294

297295
disp.add(this.currentSpellcheckerChanged
298-
.startWith(true)
296+
.startWith(true)
299297
.filter(() => this.currentSpellcheckerLanguage)
300298
.subscribe(() => {
301299
if (prevSpellCheckLanguage === this.currentSpellcheckerLanguage) return;
302300

303301
d('Actually installing spell check provider to Electron');
304-
webFrame.setSpellCheckProvider(
305-
this.currentSpellcheckerLanguage,
306-
this.shouldAutoCorrect,
307-
{ spellCheck: this.handleElectronSpellCheck.bind(this) });
302+
this.setSpellCheckProvider(webFrame);
308303

309304
prevSpellCheckLanguage = this.currentSpellcheckerLanguage;
310305
}));
@@ -388,7 +383,7 @@ module.exports = class SpellCheckHandler {
388383
let dict = null;
389384

390385
this.isMisspelledCache.reset();
391-
386+
392387
// Set language on macOS
393388
if (isMac && this.currentSpellchecker) {
394389
d(`Setting current spellchecker to ${langCode}`);
@@ -449,7 +444,7 @@ module.exports = class SpellCheckHandler {
449444
return await Observable.of(...alternatives)
450445
.concatMap((l) => {
451446
return Observable.defer(() =>
452-
Observable.fromPromise(this.dictionarySync.loadDictionaryForLanguage(l, cacheOnly)))
447+
Observable.fromPromise(this.dictionarySync.loadDictionaryForLanguage(l, cacheOnly)))
453448
.map((d) => ({language: l, dictionary: d}))
454449
.do(({language}) => {
455450
alternatesTable[langCode] = language;
@@ -463,10 +458,34 @@ module.exports = class SpellCheckHandler {
463458
}
464459

465460
/**
466-
* The actual callout called by Electron to handle spellchecking
461+
* Sets the SpellCheckProvider on the given WebFrame. Handles API differences
462+
* in Electron.
463+
* @private
464+
* @param {*} webFrame
465+
*/
466+
setSpellCheckProvider(webFrame) {
467+
if (process.versions.electron >= '5.0.0') {
468+
d('DEBUG: Setting provider for Electron 5');
469+
// Electron 5 and above:
470+
webFrame.setSpellCheckProvider(
471+
this.currentSpellcheckerLanguage,
472+
{ spellCheck: this.handleElectronSpellCheck.bind(this) });
473+
} else {
474+
d('DEBUG: Setting provider for Electron 4');
475+
// Electron 4 and below:
476+
webFrame.setSpellCheckProvider(
477+
this.currentSpellcheckerLanguage,
478+
this.shouldAutoCorrect,
479+
{ spellCheck: this.handleElectron4SpellCheck.bind(this) });
480+
}
481+
}
482+
483+
/**
484+
* The actual callout called by Electron version 4 and below to handle
485+
* spellchecking
467486
* @private
468487
*/
469-
handleElectronSpellCheck(text) {
488+
handleElectron4SpellCheck(text) {
470489
if (!this.currentSpellchecker) return true;
471490

472491
if (isMac) {
@@ -480,6 +499,29 @@ module.exports = class SpellCheckHandler {
480499
return !result;
481500
}
482501

502+
/**
503+
* The actual callout called by Electron version 5 and above to handle
504+
* spellchecking.
505+
* @private
506+
*/
507+
handleElectronSpellCheck(words, callback) {
508+
if (!this.currentSpellchecker) {
509+
callback([]);
510+
return;
511+
}
512+
513+
let misspelled = words.filter(w => this.isMisspelled(w));
514+
515+
if (isMac) {
516+
callback(misspelled);
517+
return;
518+
}
519+
520+
this.spellCheckInvoked.next(true);
521+
522+
misspelled.forEach(w => this.spellingErrorOccurred.next(w));
523+
}
524+
483525
/**
484526
* Calculates whether a word is missspelled, using an LRU cache to memoize
485527
* the callout to the actual spell check code.
@@ -652,4 +694,4 @@ module.exports = class SpellCheckHandler {
652694
d(`Result: ${JSON.stringify(ret)}`);
653695
return ret;
654696
}
655-
}
697+
};

0 commit comments

Comments
 (0)