2019-11-24 12:40:49 +00:00
|
|
|
/* eslint-env browser */
|
|
|
|
|
|
|
|
// Modified from https://github.com/sparksuite/codemirror-spell-checker
|
|
|
|
|
|
|
|
import Typo from 'typo-js'
|
|
|
|
import { serverurl } from '../config'
|
|
|
|
|
2020-01-31 09:51:14 +00:00
|
|
|
export const supportLanguages = [
|
|
|
|
{
|
|
|
|
name: 'English (United States)',
|
|
|
|
value: 'en_US',
|
|
|
|
aff: {
|
|
|
|
url: `${serverurl}/vendor/codemirror-spell-checker/en_US.aff`,
|
|
|
|
cdnUrl: `${serverurl}/vendor/codemirror-spell-checker/en_US.aff`
|
|
|
|
},
|
|
|
|
dic: {
|
|
|
|
url: `${serverurl}/vendor/codemirror-spell-checker/en_US.dic`,
|
|
|
|
cdnUrl: `${serverurl}/vendor/codemirror-spell-checker/en_US.dic`
|
|
|
|
}
|
2019-11-26 12:29:21 +00:00
|
|
|
},
|
2020-01-31 09:51:14 +00:00
|
|
|
{
|
|
|
|
name: 'German',
|
|
|
|
value: 'de',
|
|
|
|
aff: {
|
|
|
|
url: `${serverurl}/build/dictionary-de/index.aff`,
|
|
|
|
cdnUrl: `https://cdn.jsdelivr.net/npm/dictionary-de@2.0.3/index.aff`
|
|
|
|
},
|
|
|
|
dic: {
|
|
|
|
url: `${serverurl}/build/dictionary-de/index.dic`,
|
|
|
|
cdnUrl: `https://cdn.jsdelivr.net/npm/dictionary-de@2.0.3/index.dic`
|
|
|
|
}
|
2019-11-26 12:29:21 +00:00
|
|
|
},
|
2020-01-31 09:51:14 +00:00
|
|
|
{
|
|
|
|
name: 'German (Austria)',
|
|
|
|
value: 'de_AT',
|
|
|
|
aff: {
|
|
|
|
url: `${serverurl}/build/dictionary-de-at/index.aff`,
|
|
|
|
cdnUrl: `https://cdn.jsdelivr.net/npm/dictionary-de-at@2.0.3/index.aff`
|
|
|
|
},
|
|
|
|
dic: {
|
|
|
|
url: `${serverurl}/build/dictionary-de-at/index.dic`,
|
|
|
|
cdnUrl: `https://cdn.jsdelivr.net/npm/dictionary-de-at@2.0.3/index.dic`
|
|
|
|
}
|
2019-11-26 12:29:21 +00:00
|
|
|
},
|
2020-01-31 09:51:14 +00:00
|
|
|
{
|
|
|
|
name: 'German (Switzerland)',
|
|
|
|
value: 'de_CH',
|
|
|
|
aff: {
|
|
|
|
url: `${serverurl}/build/dictionary-de-ch/index.aff`,
|
|
|
|
cdnUrl: `https://cdn.jsdelivr.net/npm/dictionary-de-ch@2.0.3/index.aff`
|
|
|
|
},
|
|
|
|
dic: {
|
|
|
|
url: `${serverurl}/build/dictionary-de-ch/index.dic`,
|
|
|
|
cdnUrl: `https://cdn.jsdelivr.net/npm/dictionary-de-ch@2.0.3/index.dic`
|
|
|
|
}
|
2019-11-24 12:40:49 +00:00
|
|
|
}
|
2020-01-31 09:51:14 +00:00
|
|
|
]
|
2019-11-24 12:40:49 +00:00
|
|
|
|
2020-01-31 09:51:14 +00:00
|
|
|
export const supportLanguageCodes = supportLanguages.map(lang => lang.value)
|
2019-11-26 12:29:21 +00:00
|
|
|
|
2019-11-25 13:04:44 +00:00
|
|
|
function request (url) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
const req = new XMLHttpRequest()
|
|
|
|
req.open('GET', url, true)
|
|
|
|
req.onload = () => {
|
|
|
|
if (req.readyState === 4 && req.status === 200) {
|
|
|
|
resolve(req.responseText)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
req.send(null)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function runSeriesP (iterables, fn) {
|
|
|
|
const results = []
|
|
|
|
for (const iterable of iterables) {
|
|
|
|
results.push(await fn(iterable))
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapSeriesP (iterables, fn) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
resolve(runSeriesP(iterables, fn))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-25 13:12:38 +00:00
|
|
|
function createTypo (lang, affData, dicData) {
|
|
|
|
return new Typo(lang, affData, dicData, { platform: 'any' })
|
|
|
|
}
|
|
|
|
|
|
|
|
const typoMap = new Map()
|
|
|
|
|
2020-01-31 09:51:14 +00:00
|
|
|
let fetching = false
|
2019-11-25 13:04:44 +00:00
|
|
|
async function findOrCreateTypoInstance (lang) {
|
2020-01-31 09:51:14 +00:00
|
|
|
if (!lang) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-25 13:04:44 +00:00
|
|
|
// find existing typo instance
|
2019-11-25 13:12:38 +00:00
|
|
|
let typo = typoMap.get(lang)
|
2019-11-25 13:04:44 +00:00
|
|
|
if (typo) {
|
|
|
|
return typo
|
|
|
|
}
|
|
|
|
|
2020-01-31 09:51:14 +00:00
|
|
|
let dict = supportLanguages.find(l => l.value === lang)
|
|
|
|
|
|
|
|
if (!dict) {
|
|
|
|
console.error(`Dictionary not found for "${lang}"\n Fallback to default English spellcheck`)
|
|
|
|
dict = supportLanguages[0]
|
|
|
|
}
|
2019-11-25 13:04:44 +00:00
|
|
|
|
2020-01-31 09:51:14 +00:00
|
|
|
let affUrl
|
|
|
|
let dicUrl
|
|
|
|
if (window.USE_CDN) {
|
|
|
|
affUrl = dict.aff.cdnUrl
|
|
|
|
dicUrl = dict.dic.cdnUrl
|
|
|
|
} else {
|
|
|
|
affUrl = dict.aff.url
|
|
|
|
dicUrl = dict.dic.url
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fetching) {
|
|
|
|
return typo
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
fetching = true
|
|
|
|
|
|
|
|
const [affData, dicData] = await mapSeriesP([affUrl, dicUrl], request)
|
|
|
|
|
|
|
|
typo = createTypo(lang, affData, dicData)
|
|
|
|
typoMap.set(lang, typo)
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
} finally {
|
|
|
|
fetching = false
|
|
|
|
}
|
2019-11-25 13:12:38 +00:00
|
|
|
|
|
|
|
return typo
|
2019-11-25 13:04:44 +00:00
|
|
|
}
|
|
|
|
|
2019-11-24 12:40:49 +00:00
|
|
|
class CodeMirrorSpellChecker {
|
|
|
|
/**
|
|
|
|
* @param {CodeMirror} cm
|
|
|
|
* @param {string} lang
|
|
|
|
*/
|
2020-01-31 09:51:14 +00:00
|
|
|
constructor (cm, lang) {
|
2019-11-24 12:40:49 +00:00
|
|
|
// Verify
|
|
|
|
if (typeof cm !== 'function' || typeof cm.defineMode !== 'function') {
|
|
|
|
console.log(
|
|
|
|
'CodeMirror Spell Checker: You must provide an instance of CodeMirror via the option `codeMirrorInstance`'
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this.typo = undefined
|
2019-11-25 13:08:14 +00:00
|
|
|
this.defineSpellCheckerMode(cm, lang)
|
2019-11-24 12:40:49 +00:00
|
|
|
}
|
|
|
|
|
2019-11-25 13:08:14 +00:00
|
|
|
setDictLang (lang) {
|
|
|
|
findOrCreateTypoInstance(lang).then(typo => { this.typo = typo })
|
|
|
|
}
|
|
|
|
|
|
|
|
defineSpellCheckerMode (cm, lang) {
|
2019-11-24 12:40:49 +00:00
|
|
|
cm.defineMode('spell-checker', config => {
|
2019-11-25 13:04:44 +00:00
|
|
|
// Load AFF/DIC data async
|
2019-11-25 13:08:14 +00:00
|
|
|
this.setDictLang(lang)
|
2019-11-24 12:40:49 +00:00
|
|
|
|
|
|
|
// Define what separates a word
|
|
|
|
const regexWord = '!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~ '
|
|
|
|
|
|
|
|
// Create the overlay and such
|
|
|
|
const overlay = {
|
|
|
|
token: (stream) => {
|
|
|
|
let ch = stream.peek()
|
|
|
|
let word = ''
|
|
|
|
|
|
|
|
if (regexWord.includes(ch)) {
|
|
|
|
stream.next()
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((ch = stream.peek()) != null && !regexWord.includes(ch)) {
|
|
|
|
word += ch
|
|
|
|
stream.next()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.typo && !this.typo.check(word)) {
|
|
|
|
return 'spell-error' // CSS class: cm-spell-error
|
|
|
|
}
|
|
|
|
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const mode = cm.getMode(config, config.backdrop || 'text/plain')
|
|
|
|
|
|
|
|
return cm.overlayMode(mode, overlay, true)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default CodeMirrorSpellChecker
|