@@ -7,7 +7,7 @@ import config from './config'
77import statusBarTemplate from './statusbar.html'
88import toolBarTemplate from './toolbar.html'
99import './markdown-lint'
10- import CodeMirrorSpellChecker from './spellcheck'
10+ import CodeMirrorSpellChecker , { supportLanguages } from './spellcheck'
1111import { initTableEditor } from './table-editor'
1212import { availableThemes } from './constants'
1313
@@ -542,21 +542,44 @@ export default class Editor {
542542 } )
543543 }
544544
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+
545561 setSpellcheck ( ) {
546- var cookieSpellcheck = Cookies . get ( 'spellcheck' )
562+ const cookieSpellcheck = Cookies . get ( 'spellcheck' )
547563 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 ) {
552567 mode = defaultEditorMode
568+ } else {
569+ mode = 'spell-checker'
570+ if ( supportLanguages . includes ( cookieSpellcheck ) ) {
571+ lang = cookieSpellcheck
572+ }
553573 }
574+
554575 if ( mode && mode !== this . editor . getOption ( 'mode' ) ) {
555576 this . editor . setOption ( 'mode' , mode )
577+
578+ this . setSpellcheckLang ( lang )
556579 }
557580 }
558581
559- var spellcheckToggle = this . statusSpellcheck . find ( '.ui-spellcheck-toggle' )
582+ const spellcheckToggle = this . statusSpellcheck . find ( '.ui-spellcheck-toggle' )
560583
561584 const checkSpellcheck = ( ) => {
562585 var mode = this . editor . getOption ( 'mode' )
@@ -567,39 +590,32 @@ export default class Editor {
567590 }
568591 }
569592
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 )
574605 } 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' )
579613 }
580- Cookies . set ( 'spellcheck' , mode === 'spell-checker' , {
581- expires : 365
582- } )
583614
584- checkSpellcheck ( )
615+ self . setSpellcheckLang ( lang )
585616 } )
586617
587618 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- }
603619 }
604620
605621 toggleLinter ( enable ) {
0 commit comments