-
Notifications
You must be signed in to change notification settings - Fork 11
/
spellcheck_ace.js
130 lines (113 loc) · 3.58 KB
/
spellcheck_ace.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// You also need to load in typo.js and jquery.js
// You should configure these classes.
var editor = "editor"; // This should be the id of your editor element.
var lang = "en_US";
var dicPath = "dictionaries/en_US/en_US.dic";
var affPath = "dictionaries/en_US/en_US.aff";
// Make red underline for gutter and words.
$("<style type='text/css'>.ace_marker-layer .misspelled { position: absolute; z-index: -2; border-bottom: 1px solid red; margin-bottom: -1px; }</style>").appendTo("head");
$("<style type='text/css'>.misspelled { border-bottom: 1px solid red; margin-bottom: -1px; }</style>").appendTo("head");
// Load the dictionary.
// We have to load the dictionary files sequentially to ensure
var dictionary = null;
$.get(dicPath, function(data) {
dicData = data;
}).done(function() {
$.get(affPath, function(data) {
affData = data;
}).done(function() {
console.log("Dictionary loaded");
dictionary = new Typo(lang, affData, dicData);
});
});
// Check the spelling of a line, and return [start, end]-pairs for misspelled words.
function misspelled(line) {
var words = line.split(/[^a-zA-Z\-']/);
var i = 0;
var bads = [];
for (var word in words) {
var x = words[word] + "";
var checkWord = x.replace(/[^a-zA-Z\-']/g, '');
if (!dictionary.check(checkWord)) {
bads[bads.length] = [i, i + words[word].length];
}
i += words[word].length + 1;
}
return bads;
}
var contents_modified = true;
var currently_spellchecking = false;
var markers_present = [];
// Spell check the Ace editor contents.
function spell_check() {
// Wait for the dictionary to be loaded.
if (dictionary == null) {
return;
}
if (currently_spellchecking) {
return;
}
if (!contents_modified) {
return;
}
currently_spellchecking = true;
var session = ace.edit(editor).getSession();
// Clear all markers and gutter
clear_spellcheck_markers();
// Populate with markers and gutter
try {
var Range = ace.require('ace/range').Range
var lines = session.getDocument().getAllLines();
for (var i in lines) {
// Check spelling of this line.
var misspellings = misspelled(lines[i]);
// Add markers and gutter markings.
if (misspellings.length > 0) {
session.addGutterDecoration(i, "misspelled");
}
for (var j in misspellings) {
var range = new Range(i, misspellings[j][0], i, misspellings[j][1]);
markers_present[markers_present.length] = session.addMarker(range, "misspelled", "typo", true);
}
}
} finally {
currently_spellchecking = false;
contents_modified = false;
}
}
var spellcheckEnabled = false;
function enable_spellcheck() {
spellcheckEnabled = true
ace.edit(editor).getSession().on('change', function(e) {
if (spellcheckEnabled) {
contents_modified = true;
spell_check();
};
})
// needed to trigger update once without input
contents_modified = true;
spell_check();
}
function disable_spellcheck() {
spellcheckEnabled = false
// Clear the markers
clear_spellcheck_markers();
}
function clear_spellcheck_markers() {
var session = ace.edit(editor).getSession();
for (var i in markers_present) {
session.removeMarker(markers_present[i]);
};
markers_present = [];
// Clear the gutter
var lines = session.getDocument().getAllLines();
for (var i in lines) {
session.removeGutterDecoration(i, "misspelled");
};
}
function suggest_word_for_misspelled(misspelledWord) {
var is_spelled_correctly = dictionary.check(misspelledWord);
var array_of_suggestions = dictionary.suggest(misspelledWord);
if (is_spelled_correctly || array_of_suggestions.length === 0) { return false }
return array_of_suggestions
}