@@ -7,6 +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 , { supportLanguages , supportLanguageCodes } from './spellcheck'
10
11
import { initTableEditor } from './table-editor'
11
12
import { availableThemes } from './constants'
12
13
@@ -541,21 +542,69 @@ export default class Editor {
541
542
} )
542
543
}
543
544
545
+ setSpellcheckLang ( lang ) {
546
+ if ( lang === 'disabled' ) {
547
+ this . statusIndicators . find ( '.spellcheck-lang' ) . text ( '' )
548
+ this . activateSpellcheckListItem ( false )
549
+ return
550
+ }
551
+
552
+ if ( ! supportLanguageCodes . includes ( lang ) ) {
553
+ return
554
+ }
555
+
556
+ const langName = this . statusIndicators . find ( `.status-spellcheck li[value="${ lang } "]` ) . text ( )
557
+ this . statusIndicators . find ( '.spellcheck-lang' ) . text ( langName )
558
+
559
+ this . spellchecker . setDictLang ( lang )
560
+ this . activateSpellcheckListItem ( lang )
561
+ }
562
+
563
+ getExistingSpellcheckLang ( ) {
564
+ const cookieSpellcheck = Cookies . get ( 'spellcheck' )
565
+
566
+ if ( cookieSpellcheck ) {
567
+ return cookieSpellcheck === 'false' ? undefined : cookieSpellcheck
568
+ } else {
569
+ return undefined
570
+ }
571
+ }
572
+
573
+ activateSpellcheckListItem ( lang ) {
574
+ this . statusIndicators . find ( '.status-spellcheck li' ) . removeClass ( 'active' )
575
+
576
+ if ( lang ) {
577
+ this . statusIndicators . find ( `.status-spellcheck li[value="${ lang } "]` ) . addClass ( 'active' )
578
+ } else {
579
+ this . statusIndicators . find ( `.status-spellcheck li[value="disabled"]` ) . addClass ( 'active' )
580
+ }
581
+ }
582
+
544
583
setSpellcheck ( ) {
545
- var cookieSpellcheck = Cookies . get ( 'spellcheck' )
584
+ this . statusSpellcheck . find ( 'ul.dropdown-menu' ) . append ( supportLanguages . map ( lang => {
585
+ return $ ( `<li value="${ lang . value } "><a>${ lang . name } </a></li>` )
586
+ } ) )
587
+
588
+ const cookieSpellcheck = Cookies . get ( 'spellcheck' )
546
589
if ( cookieSpellcheck ) {
547
- var mode = null
548
- if ( cookieSpellcheck === 'true' || cookieSpellcheck === true ) {
549
- mode = 'spell-checker'
550
- } else {
590
+ let mode = null
591
+ let lang = 'en_US'
592
+
593
+ if ( cookieSpellcheck === 'false' || ! cookieSpellcheck ) {
551
594
mode = defaultEditorMode
595
+ this . activateSpellcheckListItem ( false )
596
+ } else {
597
+ mode = 'spell-checker'
598
+ if ( supportLanguageCodes . includes ( cookieSpellcheck ) ) {
599
+ lang = cookieSpellcheck
600
+ }
601
+ this . setSpellcheckLang ( lang )
552
602
}
553
- if ( mode && mode !== this . editor . getOption ( 'mode' ) ) {
554
- this . editor . setOption ( 'mode' , mode )
555
- }
603
+
604
+ this . editor . setOption ( 'mode' , mode )
556
605
}
557
606
558
- var spellcheckToggle = this . statusSpellcheck . find ( '.ui-spellcheck-toggle' )
607
+ const spellcheckToggle = this . statusSpellcheck . find ( '.ui-spellcheck-toggle' )
559
608
560
609
const checkSpellcheck = ( ) => {
561
610
var mode = this . editor . getOption ( 'mode' )
@@ -566,39 +615,32 @@ export default class Editor {
566
615
}
567
616
}
568
617
569
- spellcheckToggle . click ( ( ) => {
570
- var mode = this . editor . getOption ( 'mode' )
571
- if ( mode === defaultEditorMode ) {
572
- mode = 'spell-checker'
618
+ const self = this
619
+ this . statusIndicators . find ( `.status-spellcheck li` ) . click ( function ( ) {
620
+ const lang = $ ( this ) . attr ( 'value' )
621
+
622
+ if ( lang === 'disabled' ) {
623
+ spellcheckToggle . removeClass ( 'active' )
624
+
625
+ Cookies . set ( 'spellcheck' , false , {
626
+ expires : 365
627
+ } )
628
+
629
+ self . editor . setOption ( 'mode' , defaultEditorMode )
573
630
} else {
574
- mode = defaultEditorMode
575
- }
576
- if ( mode && mode !== this . editor . getOption ( 'mode' ) ) {
577
- this . editor . setOption ( 'mode' , mode )
631
+ spellcheckToggle . addClass ( 'active' )
632
+
633
+ Cookies . set ( 'spellcheck' , lang , {
634
+ expires : 365
635
+ } )
636
+
637
+ self . editor . setOption ( 'mode' , 'spell-checker' )
578
638
}
579
- Cookies . set ( 'spellcheck' , mode === 'spell-checker' , {
580
- expires : 365
581
- } )
582
639
583
- checkSpellcheck ( )
640
+ self . setSpellcheckLang ( lang )
584
641
} )
585
642
586
643
checkSpellcheck ( )
587
-
588
- // workaround spellcheck might not activate beacuse the ajax loading
589
- if ( window . num_loaded < 2 ) {
590
- var spellcheckTimer = setInterval (
591
- ( ) => {
592
- if ( window . num_loaded >= 2 ) {
593
- if ( this . editor . getOption ( 'mode' ) === 'spell-checker' ) {
594
- this . editor . setOption ( 'mode' , 'spell-checker' )
595
- }
596
- clearInterval ( spellcheckTimer )
597
- }
598
- } ,
599
- 100
600
- )
601
- }
602
644
}
603
645
604
646
toggleLinter ( enable ) {
@@ -723,6 +765,7 @@ export default class Editor {
723
765
placeholder : "← Start by entering a title here\n===\nVisit /features if you don't know what to do.\nHappy hacking :)"
724
766
} )
725
767
768
+ this . spellchecker = new CodeMirrorSpellChecker ( CodeMirror , this . getExistingSpellcheckLang ( ) , this . editor )
726
769
this . tableEditor = initTableEditor ( this . editor )
727
770
728
771
return this . editor
0 commit comments