55import Typo from 'typo-js'
66import { serverurl } from '../config'
77
8- const dictionaryDownloadUrls = {
9- en_US : {
10- aff : `${ serverurl } /vendor/codemirror-spell-checker/en_US.aff` ,
11- dic : `${ serverurl } /vendor/codemirror-spell-checker/en_US.dic`
8+ export const supportLanguages = [
9+ {
10+ name : 'English (United States)' ,
11+ value : 'en_US' ,
12+ aff : {
13+ url : `${ serverurl } /vendor/codemirror-spell-checker/en_US.aff` ,
14+ cdnUrl : `${ serverurl } /vendor/codemirror-spell-checker/en_US.aff`
15+ } ,
16+ dic : {
17+ url : `${ serverurl } /vendor/codemirror-spell-checker/en_US.dic` ,
18+ cdnUrl : `${ serverurl } /vendor/codemirror-spell-checker/en_US.dic`
19+ }
1220 } ,
13- de : {
14- aff : 'https://rawcdn.githack.com/wooorm/dictionaries/143091715eebbbdfa0e8936e117f9182514eebe6/dictionaries/de/index.aff' ,
15- dic : 'https://rawcdn.githack.com/wooorm/dictionaries/143091715eebbbdfa0e8936e117f9182514eebe6/dictionaries/de/index.dic'
21+ {
22+ name : 'German' ,
23+ value : 'de' ,
24+ aff : {
25+ url : `${ serverurl } /build/dictionary-de/index.aff` ,
26+ cdnUrl : `https://cdn.jsdelivr.net/npm/dictionary-de@2.0.3/index.aff`
27+ } ,
28+ dic : {
29+ url : `${ serverurl } /build/dictionary-de/index.dic` ,
30+ cdnUrl : `https://cdn.jsdelivr.net/npm/dictionary-de@2.0.3/index.dic`
31+ }
1632 } ,
17- de_AT : {
18- aff : 'https://rawcdn.githack.com/wooorm/dictionaries/143091715eebbbdfa0e8936e117f9182514eebe6/dictionaries/de-AT/index.aff' ,
19- dic : 'https://rawcdn.githack.com/wooorm/dictionaries/143091715eebbbdfa0e8936e117f9182514eebe6/dictionaries/de-AT/index.dic'
33+ {
34+ name : 'German (Austria)' ,
35+ value : 'de_AT' ,
36+ aff : {
37+ url : `${ serverurl } /build/dictionary-de-at/index.aff` ,
38+ cdnUrl : `https://cdn.jsdelivr.net/npm/dictionary-de-at@2.0.3/index.aff`
39+ } ,
40+ dic : {
41+ url : `${ serverurl } /build/dictionary-de-at/index.dic` ,
42+ cdnUrl : `https://cdn.jsdelivr.net/npm/dictionary-de-at@2.0.3/index.dic`
43+ }
2044 } ,
21- de_CH : {
22- aff : 'https://rawcdn.githack.com/wooorm/dictionaries/143091715eebbbdfa0e8936e117f9182514eebe6/dictionaries/de-CH/index.aff' ,
23- dic : 'https://rawcdn.githack.com/wooorm/dictionaries/143091715eebbbdfa0e8936e117f9182514eebe6/dictionaries/de-CH/index.dic'
45+ {
46+ name : 'German (Switzerland)' ,
47+ value : 'de_CH' ,
48+ aff : {
49+ url : `${ serverurl } /build/dictionary-de-ch/index.aff` ,
50+ cdnUrl : `https://cdn.jsdelivr.net/npm/dictionary-de-ch@2.0.3/index.aff`
51+ } ,
52+ dic : {
53+ url : `${ serverurl } /build/dictionary-de-ch/index.dic` ,
54+ cdnUrl : `https://cdn.jsdelivr.net/npm/dictionary-de-ch@2.0.3/index.dic`
55+ }
2456 }
25- }
57+ ]
2658
27- export const supportLanguages = Object . keys ( dictionaryDownloadUrls )
59+ export const supportLanguageCodes = supportLanguages . map ( lang => lang . value )
2860
2961function request ( url ) {
3062 return new Promise ( resolve => {
@@ -59,20 +91,51 @@ function createTypo (lang, affData, dicData) {
5991
6092const typoMap = new Map ( )
6193
94+ let fetching = false
6295async function findOrCreateTypoInstance ( lang ) {
96+ if ( ! lang ) {
97+ return
98+ }
99+
63100 // find existing typo instance
64101 let typo = typoMap . get ( lang )
65102 if ( typo ) {
66103 return typo
67104 }
68105
69- const [ affData , dicData ] = await mapSeriesP ( [
70- dictionaryDownloadUrls [ lang ] . aff ,
71- dictionaryDownloadUrls [ lang ] . dic
72- ] , request )
106+ let dict = supportLanguages . find ( l => l . value === lang )
107+
108+ if ( ! dict ) {
109+ console . error ( `Dictionary not found for "${ lang } "\n Fallback to default English spellcheck` )
110+ dict = supportLanguages [ 0 ]
111+ }
73112
74- typo = createTypo ( lang , affData , dicData )
75- typoMap . set ( lang , typo )
113+ let affUrl
114+ let dicUrl
115+ if ( window . USE_CDN ) {
116+ affUrl = dict . aff . cdnUrl
117+ dicUrl = dict . dic . cdnUrl
118+ } else {
119+ affUrl = dict . aff . url
120+ dicUrl = dict . dic . url
121+ }
122+
123+ if ( fetching ) {
124+ return typo
125+ }
126+
127+ try {
128+ fetching = true
129+
130+ const [ affData , dicData ] = await mapSeriesP ( [ affUrl , dicUrl ] , request )
131+
132+ typo = createTypo ( lang , affData , dicData )
133+ typoMap . set ( lang , typo )
134+ } catch ( err ) {
135+ console . error ( err )
136+ } finally {
137+ fetching = false
138+ }
76139
77140 return typo
78141}
@@ -82,7 +145,7 @@ class CodeMirrorSpellChecker {
82145 * @param {CodeMirror } cm
83146 * @param {string } lang
84147 */
85- constructor ( cm , lang = 'en_US' ) {
148+ constructor ( cm , lang ) {
86149 // Verify
87150 if ( typeof cm !== 'function' || typeof cm . defineMode !== 'function' ) {
88151 console . log (
0 commit comments