@@ -140,11 +140,9 @@ module.exports = class SpellCheckHandler {
140
140
this . currentSpellchecker = new Spellchecker ( ) ;
141
141
this . currentSpellcheckerLanguage = 'en-US' ;
142
142
143
+ d ( 'DEBUG: Settings Spell Check Provider...' ) ;
143
144
if ( webFrame ) {
144
- webFrame . setSpellCheckProvider (
145
- this . currentSpellcheckerLanguage ,
146
- this . shouldAutoCorrect ,
147
- { spellCheck : this . handleElectronSpellCheck . bind ( this ) } ) ;
145
+ this . setSpellCheckProvider ( webFrame ) ;
148
146
}
149
147
return ;
150
148
}
@@ -255,9 +253,9 @@ module.exports = class SpellCheckHandler {
255
253
}
256
254
257
255
let contentToCheck = Observable . merge (
258
- this . spellingErrorOccurred ,
259
- initialInputText ,
260
- possiblySwitchedCharacterSets )
256
+ this . spellingErrorOccurred ,
257
+ initialInputText ,
258
+ possiblySwitchedCharacterSets )
261
259
. mergeMap ( ( ) => {
262
260
if ( lastInputText . length < 8 ) return Observable . empty ( ) ;
263
261
return Observable . of ( lastInputText ) ;
@@ -295,16 +293,13 @@ module.exports = class SpellCheckHandler {
295
293
let prevSpellCheckLanguage ;
296
294
297
295
disp . add ( this . currentSpellcheckerChanged
298
- . startWith ( true )
296
+ . startWith ( true )
299
297
. filter ( ( ) => this . currentSpellcheckerLanguage )
300
298
. subscribe ( ( ) => {
301
299
if ( prevSpellCheckLanguage === this . currentSpellcheckerLanguage ) return ;
302
300
303
301
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 ) ;
308
303
309
304
prevSpellCheckLanguage = this . currentSpellcheckerLanguage ;
310
305
} ) ) ;
@@ -388,7 +383,7 @@ module.exports = class SpellCheckHandler {
388
383
let dict = null ;
389
384
390
385
this . isMisspelledCache . reset ( ) ;
391
-
386
+
392
387
// Set language on macOS
393
388
if ( isMac && this . currentSpellchecker ) {
394
389
d ( `Setting current spellchecker to ${ langCode } ` ) ;
@@ -449,7 +444,7 @@ module.exports = class SpellCheckHandler {
449
444
return await Observable . of ( ...alternatives )
450
445
. concatMap ( ( l ) => {
451
446
return Observable . defer ( ( ) =>
452
- Observable . fromPromise ( this . dictionarySync . loadDictionaryForLanguage ( l , cacheOnly ) ) )
447
+ Observable . fromPromise ( this . dictionarySync . loadDictionaryForLanguage ( l , cacheOnly ) ) )
453
448
. map ( ( d ) => ( { language : l , dictionary : d } ) )
454
449
. do ( ( { language} ) => {
455
450
alternatesTable [ langCode ] = language ;
@@ -463,10 +458,34 @@ module.exports = class SpellCheckHandler {
463
458
}
464
459
465
460
/**
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
467
486
* @private
468
487
*/
469
- handleElectronSpellCheck ( text ) {
488
+ handleElectron4SpellCheck ( text ) {
470
489
if ( ! this . currentSpellchecker ) return true ;
471
490
472
491
if ( isMac ) {
@@ -480,6 +499,29 @@ module.exports = class SpellCheckHandler {
480
499
return ! result ;
481
500
}
482
501
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
+
483
525
/**
484
526
* Calculates whether a word is missspelled, using an LRU cache to memoize
485
527
* the callout to the actual spell check code.
@@ -652,4 +694,4 @@ module.exports = class SpellCheckHandler {
652
694
d ( `Result: ${ JSON . stringify ( ret ) } ` ) ;
653
695
return ret ;
654
696
}
655
- }
697
+ } ;
0 commit comments