Skip to content

Commit 2244387

Browse files
committed
Support multilanguage spellchecker
Signed-off-by: Yukai Huang <[email protected]>
1 parent 3c8b5af commit 2244387

File tree

3 files changed

+76
-36
lines changed

3 files changed

+76
-36
lines changed

public/js/lib/editor/index.js

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import config from './config'
77
import statusBarTemplate from './statusbar.html'
88
import toolBarTemplate from './toolbar.html'
99
import './markdown-lint'
10-
import CodeMirrorSpellChecker from './spellcheck'
10+
import CodeMirrorSpellChecker, { supportLanguages } from './spellcheck'
1111
import { initTableEditor } from './table-editor'
1212
import { 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) {

public/js/lib/editor/spellcheck.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,23 @@ const dictionaryDownloadUrls = {
99
en_US: {
1010
aff: `${serverurl}/vendor/codemirror-spell-checker/en_US.aff`,
1111
dic: `${serverurl}/vendor/codemirror-spell-checker/en_US.dic`
12+
},
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'
16+
},
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'
20+
},
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'
1224
}
1325
}
1426

27+
export const supportLanguages = Object.keys(dictionaryDownloadUrls)
28+
1529
function request (url) {
1630
return new Promise(resolve => {
1731
const req = new XMLHttpRequest()

public/js/lib/editor/statusbar.html

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,18 @@
3838
<ul class="dropdown-menu" aria-labelledby="themeLabel" style="overflow: auto;">
3939
</ul>
4040
</div>
41-
<div class="status-spellcheck">
42-
<a class="ui-spellcheck-toggle" title="Toggle spellcheck"><i class="fa fa-check fa-fw"></i></a>
41+
<div class="status-spellcheck dropup pull-right">
42+
<a class="ui-spellcheck-toggle" title="Toggle spellcheck" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
43+
<i class="fa fa-check fa-fw"></i>
44+
<span class="spellcheck-lang"></span>
45+
</a>
46+
<ul class="dropdown-menu" aria-labelledby="themeLabel">
47+
<li value="disabled"><a>Disabled</a></li>
48+
<li value="en_US"><a>English (United States)</a></li>
49+
<li value="de"><a>German</a></li>
50+
<li value="de_AT"><a>German (Austria)</a></li>
51+
<li value="de_CH"><a>German (Switzerland)</a></li>
52+
</ul>
4353
</div>
4454
<div class="status-linter">
4555
<a class="ui-linter-toggle" title="Toggle linter"><i class="fa fa-lightbulb-o fa-fw"></i></a>

0 commit comments

Comments
 (0)