@@ -7,7 +7,7 @@ import config from './config'
7
7
import statusBarTemplate from './statusbar.html'
8
8
import toolBarTemplate from './toolbar.html'
9
9
import './markdown-lint'
10
- import CodeMirrorSpellChecker from './spellcheck'
10
+ import CodeMirrorSpellChecker , { supportLanguages } from './spellcheck'
11
11
import { initTableEditor } from './table-editor'
12
12
import { availableThemes } from './constants'
13
13
@@ -542,21 +542,44 @@ export default class Editor {
542
542
} )
543
543
}
544
544
545
+ setSpellcheckLang ( lang ) {
546
+ if ( lang === 'disabled' ) {
547
+ this . statusIndicators . find ( '.spellcheck-lang' ) . text ( '' )
548
+ return
549
+ }
550
+
551
+ if ( ! supportLanguages . includes ( lang ) ) {
552
+ return
553
+ }
554
+
555
+ const langName = this . statusIndicators . find ( `.status-spellcheck li[value="${ lang } "]` ) . text ( )
556
+ this . statusIndicators . find ( '.spellcheck-lang' ) . text ( langName )
557
+
558
+ this . spellchecker . setDictLang ( lang )
559
+ }
560
+
545
561
setSpellcheck ( ) {
546
- var cookieSpellcheck = Cookies . get ( 'spellcheck' )
562
+ const cookieSpellcheck = Cookies . get ( 'spellcheck' )
547
563
if ( cookieSpellcheck ) {
548
- var mode = null
549
- if ( cookieSpellcheck === 'true' || cookieSpellcheck === true ) {
550
- mode = 'spell-checker'
551
- } else {
564
+ let mode = null
565
+ let lang = 'en_US'
566
+ if ( cookieSpellcheck === 'false' || ! cookieSpellcheck ) {
552
567
mode = defaultEditorMode
568
+ } else {
569
+ mode = 'spell-checker'
570
+ if ( supportLanguages . includes ( cookieSpellcheck ) ) {
571
+ lang = cookieSpellcheck
572
+ }
553
573
}
574
+
554
575
if ( mode && mode !== this . editor . getOption ( 'mode' ) ) {
555
576
this . editor . setOption ( 'mode' , mode )
577
+
578
+ this . setSpellcheckLang ( lang )
556
579
}
557
580
}
558
581
559
- var spellcheckToggle = this . statusSpellcheck . find ( '.ui-spellcheck-toggle' )
582
+ const spellcheckToggle = this . statusSpellcheck . find ( '.ui-spellcheck-toggle' )
560
583
561
584
const checkSpellcheck = ( ) => {
562
585
var mode = this . editor . getOption ( 'mode' )
@@ -567,39 +590,32 @@ export default class Editor {
567
590
}
568
591
}
569
592
570
- spellcheckToggle . click ( ( ) => {
571
- var mode = this . editor . getOption ( 'mode' )
572
- if ( mode === defaultEditorMode ) {
573
- mode = 'spell-checker'
593
+ const self = this
594
+ this . statusIndicators . find ( `.status-spellcheck li` ) . click ( function ( ) {
595
+ const lang = $ ( this ) . attr ( 'value' )
596
+
597
+ if ( lang === 'disabled' ) {
598
+ spellcheckToggle . removeClass ( 'active' )
599
+
600
+ Cookies . set ( 'spellcheck' , false , {
601
+ expires : 365
602
+ } )
603
+
604
+ self . editor . setOption ( 'mode' , defaultEditorMode )
574
605
} else {
575
- mode = defaultEditorMode
576
- }
577
- if ( mode && mode !== this . editor . getOption ( 'mode' ) ) {
578
- this . editor . setOption ( 'mode' , mode )
606
+ spellcheckToggle . addClass ( 'active' )
607
+
608
+ Cookies . set ( 'spellcheck' , lang , {
609
+ expires : 365
610
+ } )
611
+
612
+ self . editor . setOption ( 'mode' , 'spell-checker' )
579
613
}
580
- Cookies . set ( 'spellcheck' , mode === 'spell-checker' , {
581
- expires : 365
582
- } )
583
614
584
- checkSpellcheck ( )
615
+ self . setSpellcheckLang ( lang )
585
616
} )
586
617
587
618
checkSpellcheck ( )
588
-
589
- // workaround spellcheck might not activate beacuse the ajax loading
590
- if ( window . num_loaded < 2 ) {
591
- var spellcheckTimer = setInterval (
592
- ( ) => {
593
- if ( window . num_loaded >= 2 ) {
594
- if ( this . editor . getOption ( 'mode' ) === 'spell-checker' ) {
595
- this . editor . setOption ( 'mode' , 'spell-checker' )
596
- }
597
- clearInterval ( spellcheckTimer )
598
- }
599
- } ,
600
- 100
601
- )
602
- }
603
619
}
604
620
605
621
toggleLinter ( enable ) {
0 commit comments