From 22443879e70581709bf8c334043da7360e9c6d6e Mon Sep 17 00:00:00 2001 From: Yukai Huang Date: Tue, 26 Nov 2019 20:29:21 +0800 Subject: [PATCH] Support multilanguage spellchecker Signed-off-by: Yukai Huang --- public/js/lib/editor/index.js | 88 +++++++++++++++++------------ public/js/lib/editor/spellcheck.js | 14 +++++ public/js/lib/editor/statusbar.html | 14 ++++- 3 files changed, 78 insertions(+), 38 deletions(-) diff --git a/public/js/lib/editor/index.js b/public/js/lib/editor/index.js index f8b39023..7d796da2 100644 --- a/public/js/lib/editor/index.js +++ b/public/js/lib/editor/index.js @@ -7,7 +7,7 @@ import config from './config' import statusBarTemplate from './statusbar.html' import toolBarTemplate from './toolbar.html' import './markdown-lint' -import CodeMirrorSpellChecker from './spellcheck' +import CodeMirrorSpellChecker, { supportLanguages } from './spellcheck' import { initTableEditor } from './table-editor' import { availableThemes } from './constants' @@ -542,21 +542,44 @@ export default class Editor { }) } + setSpellcheckLang (lang) { + if (lang === 'disabled') { + this.statusIndicators.find('.spellcheck-lang').text('') + return + } + + if (!supportLanguages.includes(lang)) { + return + } + + const langName = this.statusIndicators.find(`.status-spellcheck li[value="${lang}"]`).text() + this.statusIndicators.find('.spellcheck-lang').text(langName) + + this.spellchecker.setDictLang(lang) + } + setSpellcheck () { - var cookieSpellcheck = Cookies.get('spellcheck') + const cookieSpellcheck = Cookies.get('spellcheck') if (cookieSpellcheck) { - var mode = null - if (cookieSpellcheck === 'true' || cookieSpellcheck === true) { - mode = 'spell-checker' - } else { + let mode = null + let lang = 'en_US' + if (cookieSpellcheck === 'false' || !cookieSpellcheck) { mode = defaultEditorMode + } else { + mode = 'spell-checker' + if (supportLanguages.includes(cookieSpellcheck)) { + lang = cookieSpellcheck + } } + if (mode && mode !== this.editor.getOption('mode')) { this.editor.setOption('mode', mode) + + this.setSpellcheckLang(lang) } } - var spellcheckToggle = this.statusSpellcheck.find('.ui-spellcheck-toggle') + const spellcheckToggle = this.statusSpellcheck.find('.ui-spellcheck-toggle') const checkSpellcheck = () => { var mode = this.editor.getOption('mode') @@ -567,39 +590,32 @@ export default class Editor { } } - spellcheckToggle.click(() => { - var mode = this.editor.getOption('mode') - if (mode === defaultEditorMode) { - mode = 'spell-checker' - } else { - mode = defaultEditorMode - } - if (mode && mode !== this.editor.getOption('mode')) { - this.editor.setOption('mode', mode) - } - Cookies.set('spellcheck', mode === 'spell-checker', { - expires: 365 - }) + const self = this + this.statusIndicators.find(`.status-spellcheck li`).click(function () { + const lang = $(this).attr('value') - checkSpellcheck() + if (lang === 'disabled') { + spellcheckToggle.removeClass('active') + + Cookies.set('spellcheck', false, { + expires: 365 + }) + + self.editor.setOption('mode', defaultEditorMode) + } else { + spellcheckToggle.addClass('active') + + Cookies.set('spellcheck', lang, { + expires: 365 + }) + + self.editor.setOption('mode', 'spell-checker') + } + + self.setSpellcheckLang(lang) }) checkSpellcheck() - - // workaround spellcheck might not activate beacuse the ajax loading - if (window.num_loaded < 2) { - var spellcheckTimer = setInterval( - () => { - if (window.num_loaded >= 2) { - if (this.editor.getOption('mode') === 'spell-checker') { - this.editor.setOption('mode', 'spell-checker') - } - clearInterval(spellcheckTimer) - } - }, - 100 - ) - } } toggleLinter (enable) { diff --git a/public/js/lib/editor/spellcheck.js b/public/js/lib/editor/spellcheck.js index 6c094056..24fb9c49 100644 --- a/public/js/lib/editor/spellcheck.js +++ b/public/js/lib/editor/spellcheck.js @@ -9,9 +9,23 @@ const dictionaryDownloadUrls = { en_US: { aff: `${serverurl}/vendor/codemirror-spell-checker/en_US.aff`, dic: `${serverurl}/vendor/codemirror-spell-checker/en_US.dic` + }, + de: { + aff: 'https://rawcdn.githack.com/wooorm/dictionaries/143091715eebbbdfa0e8936e117f9182514eebe6/dictionaries/de/index.aff', + dic: 'https://rawcdn.githack.com/wooorm/dictionaries/143091715eebbbdfa0e8936e117f9182514eebe6/dictionaries/de/index.dic' + }, + de_AT: { + aff: 'https://rawcdn.githack.com/wooorm/dictionaries/143091715eebbbdfa0e8936e117f9182514eebe6/dictionaries/de-AT/index.aff', + dic: 'https://rawcdn.githack.com/wooorm/dictionaries/143091715eebbbdfa0e8936e117f9182514eebe6/dictionaries/de-AT/index.dic' + }, + de_CH: { + aff: 'https://rawcdn.githack.com/wooorm/dictionaries/143091715eebbbdfa0e8936e117f9182514eebe6/dictionaries/de-CH/index.aff', + dic: 'https://rawcdn.githack.com/wooorm/dictionaries/143091715eebbbdfa0e8936e117f9182514eebe6/dictionaries/de-CH/index.dic' } } +export const supportLanguages = Object.keys(dictionaryDownloadUrls) + function request (url) { return new Promise(resolve => { const req = new XMLHttpRequest() diff --git a/public/js/lib/editor/statusbar.html b/public/js/lib/editor/statusbar.html index 5cee8998..ba718108 100644 --- a/public/js/lib/editor/statusbar.html +++ b/public/js/lib/editor/statusbar.html @@ -38,8 +38,18 @@ -