New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local dic #1

Closed
wants to merge 2 commits into
base: inkdrop
from
File filter...
Filter file types
Jump to file or symbol
Failed to load files and symbols.
+63 −29
Diff settings

Always

Just for now

Copy path View file
@@ -35,6 +35,7 @@ CodeMirrorSpellChecker({
CodeMirror.fromTextArea(document.getElementById("textarea"), {
mode: "spell-checker",
backdrop: "gfm" // Your desired mode
language: "en_US" // Optionall specify a different language
});
```

@@ -48,3 +49,13 @@ You can customize the misspelled word appearance by updating the CSS. All misspe
/* Your styling here */
}
```

## Languages

The package ships with *en_US* by default. On Ubuntu systems it will check for
the dictionaries in */usr/share/hunspell/*. You can install new languages by
using

```
sudo apt-get install myspell-en-au
```
Copy path View file
@@ -4,6 +4,7 @@

// Requires
var Typo = require("typo-js");
var fs = require("fs");

// Check if the string is in alphabets
// Borrowed from: https://stackoverflow.com/a/17056233/1217590
@@ -16,6 +17,7 @@ function isAlpha(string) {
function CodeMirrorSpellChecker(options) {
// Initialize
options = options || {};
var language = options.language || "en_US";


// Verify
@@ -33,48 +35,58 @@ function CodeMirrorSpellChecker(options) {
};
}

var loadRemoteDic = function(type) {
return function() {
if(this.readyState === 4 && this.status === 200) {
CodeMirrorSpellChecker[type] = this.responseText;
CodeMirrorSpellChecker.num_loaded++;

// Define the new mode
options.codeMirrorInstance.defineMode("spell-checker", function(config) {
if(CodeMirrorSpellChecker.num_loaded == 2) {
CodeMirrorSpellChecker.typo = new Typo(language, CodeMirrorSpellChecker.aff_data, CodeMirrorSpellChecker.dic_data, {
platform: "any"
});
}
}
};
};

var loadRemote = function() {
// Load AFF/DIC data
if(!CodeMirrorSpellChecker.aff_loading) {
CodeMirrorSpellChecker.aff_loading = true;
var xhr_aff = new XMLHttpRequest();
xhr_aff.open("GET", "https://cdn.jsdelivr.net/codemirror.spell-checker/latest/en_US.aff", true);
xhr_aff.onload = function() {
if(xhr_aff.readyState === 4 && xhr_aff.status === 200) {
CodeMirrorSpellChecker.aff_data = xhr_aff.responseText;
CodeMirrorSpellChecker.num_loaded++;

if(CodeMirrorSpellChecker.num_loaded == 2) {
CodeMirrorSpellChecker.typo = new Typo("en_US", CodeMirrorSpellChecker.aff_data, CodeMirrorSpellChecker.dic_data, {
platform: "any"
});
}
}
};
xhr_aff.open("GET", "https://cdn.jsdelivr.net/codemirror.spell-checker/latest/" + language + ".aff", true);
xhr_aff.onload = loadRemoteDic("aff_data");
xhr_aff.send(null);
}

if(!CodeMirrorSpellChecker.dic_loading) {
CodeMirrorSpellChecker.dic_loading = true;
var xhr_dic = new XMLHttpRequest();
xhr_dic.open("GET", "https://cdn.jsdelivr.net/codemirror.spell-checker/latest/en_US.dic", true);
xhr_dic.onload = function() {
if(xhr_dic.readyState === 4 && xhr_dic.status === 200) {
CodeMirrorSpellChecker.dic_data = xhr_dic.responseText;
CodeMirrorSpellChecker.num_loaded++;

if(CodeMirrorSpellChecker.num_loaded == 2) {
CodeMirrorSpellChecker.typo = new Typo("en_US", CodeMirrorSpellChecker.aff_data, CodeMirrorSpellChecker.dic_data, {
platform: "any"
});
}
}
};
xhr_dic.open("GET", "https://cdn.jsdelivr.net/codemirror.spell-checker/latest/" + language + ".dic", true);
xhr_dic.onload = loadRemoteDic("dic_data");
xhr_dic.send(null);
}
};

var loadLocal = function() {
CodeMirrorSpellChecker.aff_data = fs.readFileSync(spellPath + "/" + language + ".aff", { encoding: "utf8" });
CodeMirrorSpellChecker.dic_data = fs.readFileSync(spellPath + "/" + language + ".dic", { encoding: "utf8" });

CodeMirrorSpellChecker.typo = new Typo(language, CodeMirrorSpellChecker.aff_data, CodeMirrorSpellChecker.dic_data, {
platform: "any"
});
};

// Define the new mode
options.codeMirrorInstance.defineMode("spell-checker", function(config) {

if (CodeMirrorSpellChecker.languages.includes(language)) {
loadLocal();
}
else {
loadRemote();
}

// Create the overlay and such
var overlay = {
@@ -121,7 +133,18 @@ CodeMirrorSpellChecker.dic_loading = false;
CodeMirrorSpellChecker.aff_data = "";
CodeMirrorSpellChecker.dic_data = "";
CodeMirrorSpellChecker.typo;
CodeMirrorSpellChecker.languages = [];
var spellPath = "/usr/share/hunspell";
if (fs.existsSync(spellPath)) {
var items = fs.readdirSync(spellPath);
for (var i=0; i < items.length; i++) {
var md = items[i].match(/^(.*).aff$/);
if (md) {
CodeMirrorSpellChecker.languages.push(md[1]);
}
}
}


// Export
module.exports = CodeMirrorSpellChecker;
module.exports = CodeMirrorSpellChecker;
ProTip! Use n and p to navigate between commits in a pull request.