Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Allow users to add custom words to dictionary #244

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions app/menu-context.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
[
{
"labelKey": "menu-add-to-dictionary",
"command": "addToDict"
},
{
"type": "separator"
},
{
"labelKey": "menu-undo",
"accelerator": "CmdOrCtrl+Z",
Expand Down
36 changes: 36 additions & 0 deletions app/renderer/abr-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ function AbrDocument () {
// Start with an empty table of contents
this.toc = [];

// Custom words
this.words = [];

// Run this in a background thread
var worker = cp.fork(__dirname + "/toc-worker.js");

Expand Down Expand Up @@ -87,6 +90,18 @@ function AbrDocument () {
// Spellchecker init
if (config.spellchecker.active) {
that.setDictionary(config.spellchecker.language, config.spellchecker.src);

// load custom words
var words = config.spellchecker["custom-words"];
if (words != null) {
that.words = words;
words.forEach(function (word) {
// just for safety
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO you can remove this comment.

if (word != "") {
spellchecker.add(word)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Please beware of missing semicolon ;
  2. What do you think about trimming word? Like:
word = word.trim();
if (word) {
  spellchecker.add(word);
}

(not sure this is useful though)

}
});
}
}

// Editor font-size
Expand Down Expand Up @@ -773,6 +788,15 @@ AbrDocument.prototype = {
setDictionary: function (lang, path) {
if (lang) {
spellchecker.setDictionary(lang, path);

// reload custom words
this.words.forEach(function (word) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same remarks here.
Actually you can maybe use a function here to avoid code duplication. I let you decide.

// just for safety
if (word != "") {
spellchecker.add(word)
}
});

// Refresh CodeMirror highlight + enable spelling
this.cm.setOption("mode", "abr-spellcheck-on");
this.setConfig("spellchecker:active", true);
Expand All @@ -789,6 +813,18 @@ AbrDocument.prototype = {
return spellchecker.isMisspelled;
},

// Adds the given word to our custom spellcheck-ok words
addSpelledWord: function (word) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid duplicates in config (in case of multiple windows), maybe we can add this here:

this.words = this.getConfig("spellchecker:custom-words");

// add to in-memory spellchecker
spellchecker.add(word);

// add to config file
if (this.words.indexOf(word) == -1) {
this.words.push(word)
this.setConfig("spellchecker:custom-words", this.words)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing two ;

}
},

// Scale text
setFontSize: function (size) {
var min = 8,
Expand Down
56 changes: 56 additions & 0 deletions app/renderer/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,62 @@ var commands = {
document.execCommand("paste");
},

addToDict: function(win, abrDoc, cm) {
var pos = cm.doc.getCursor();
var line = cm.doc.getLine(pos.line);

// split all words on the currently-selected line
var wordDelimiters = "!\"#$%&()*+,-./:;<=>?@[\\]^_`{|}~ \t",
initialChar = 0,
word = "",
words = [];

for (var i = 0; i < line.length; i++) {
var ch = line[i];

if (wordDelimiters.includes(ch)) {
if (word != "") {
words.push([initialChar, word.replace(/[’ʼ]/g, "'")]);
word = "";
}
initialChar = i;
} else {
word += ch;
}
}
if (word != "") {
words.push([initialChar, word.replace(/[’ʼ]/g, "'")]);
}

// if there are no words on this line, return
if (words.length < 1) {
return
}

// find which specific word which is selected
var selectedWord = words[0][1];

for(var i = 0; i < words.length; i++) {
var newWordStart = words[i][0],
newWord = words[i][1];

if (newWordStart < pos.ch) {
selectedWord = newWord;
}
}

// confirm the selected word is misspelled
var isMisspelled = abrDoc.getSpellcheckFunc()(selectedWord)

if (isMisspelled) {
abrDoc.addSpelledWord(selectedWord);

// refresh overlays so user sees the word get marked as spelled fine
cm.setOption("mode", "abr-spellcheck-off");
cm.setOption("mode", "abr-spellcheck-on");
}
},

find: function(win, abrDoc, cm) {
$(".CodeMirror-dialog").remove(); // FIXME: error when double key
cm.execCommand("clearSearch");
Expand Down
1 change: 1 addition & 0 deletions default/lang/en.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"menu-add-to-dictionary": "Add to Dictionary",
"menu-undo": "Undo",
"menu-redo": "Redo",
"menu-cut": "Cut",
Expand Down