|
| 1 | +/* eslint-env browser */ |
| 2 | + |
| 3 | +// Modified from https://github.com/sparksuite/codemirror-spell-checker |
| 4 | + |
| 5 | +import Typo from 'typo-js' |
| 6 | +import { serverurl } from '../config' |
| 7 | + |
| 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` |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +class CodeMirrorSpellChecker { |
| 16 | + /** |
| 17 | + * @param {CodeMirror} cm |
| 18 | + * @param {string} lang |
| 19 | + */ |
| 20 | + constructor (cm, lang = 'en_US') { |
| 21 | + // Verify |
| 22 | + if (typeof cm !== 'function' || typeof cm.defineMode !== 'function') { |
| 23 | + console.log( |
| 24 | + 'CodeMirror Spell Checker: You must provide an instance of CodeMirror via the option `codeMirrorInstance`' |
| 25 | + ) |
| 26 | + return |
| 27 | + } |
| 28 | + |
| 29 | + this.numLoaded = 0 |
| 30 | + this.affLoading = false |
| 31 | + this.dicLoading = false |
| 32 | + this.affData = '' |
| 33 | + this.dicData = '' |
| 34 | + this.typo = undefined |
| 35 | + |
| 36 | + this.setupCM.bind(this)(cm, lang) |
| 37 | + } |
| 38 | + |
| 39 | + setupCM (cm, lang) { |
| 40 | + cm.defineMode('spell-checker', config => { |
| 41 | + // Load AFF/DIC data |
| 42 | + if (!this.affLoading) { |
| 43 | + this.affLoading = true |
| 44 | + |
| 45 | + const xhrAff = new XMLHttpRequest() |
| 46 | + xhrAff.open('GET', dictionaryDownloadUrls[lang].aff, true) |
| 47 | + xhrAff.onload = () => { |
| 48 | + if (xhrAff.readyState === 4 && xhrAff.status === 200) { |
| 49 | + this.affData = xhrAff.responseText |
| 50 | + this.numLoaded++ |
| 51 | + |
| 52 | + if (this.numLoaded === 2) { |
| 53 | + this.typo = new Typo(lang, this.affData, this.dicData, { platform: 'any' }) |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + xhrAff.send(null) |
| 58 | + } |
| 59 | + |
| 60 | + if (!this.dicLoading) { |
| 61 | + this.dicLoading = true |
| 62 | + const xhrDic = new XMLHttpRequest() |
| 63 | + xhrDic.open('GET', dictionaryDownloadUrls[lang].dic, true) |
| 64 | + xhrDic.onload = () => { |
| 65 | + if (xhrDic.readyState === 4 && xhrDic.status === 200) { |
| 66 | + this.dicData = xhrDic.responseText |
| 67 | + this.numLoaded++ |
| 68 | + |
| 69 | + if (this.numLoaded === 2) { |
| 70 | + this.typo = new Typo(lang, this.affData, this.dicData, { platform: 'any' }) |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + xhrDic.send(null) |
| 75 | + } |
| 76 | + |
| 77 | + // Define what separates a word |
| 78 | + const regexWord = '!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~ ' |
| 79 | + |
| 80 | + // Create the overlay and such |
| 81 | + const overlay = { |
| 82 | + token: (stream) => { |
| 83 | + let ch = stream.peek() |
| 84 | + let word = '' |
| 85 | + |
| 86 | + if (regexWord.includes(ch)) { |
| 87 | + stream.next() |
| 88 | + return null |
| 89 | + } |
| 90 | + |
| 91 | + while ((ch = stream.peek()) != null && !regexWord.includes(ch)) { |
| 92 | + word += ch |
| 93 | + stream.next() |
| 94 | + } |
| 95 | + |
| 96 | + if (this.typo && !this.typo.check(word)) { |
| 97 | + return 'spell-error' // CSS class: cm-spell-error |
| 98 | + } |
| 99 | + |
| 100 | + return null |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + const mode = cm.getMode(config, config.backdrop || 'text/plain') |
| 105 | + |
| 106 | + return cm.overlayMode(mode, overlay, true) |
| 107 | + }) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// Export |
| 112 | +export default CodeMirrorSpellChecker |
0 commit comments