Stackedit/public/res/workers/spellCheckWorker.js

40 lines
1.0 KiB
JavaScript
Raw Normal View History

2013-11-05 23:03:38 +00:00
/*jshint worker:true */
var dictionary;
2013-10-05 01:30:54 +00:00
2013-11-05 23:03:38 +00:00
/*jshint evil:true, unused:false */
self.init = function(typoJS, LZString, lang, aff, dic) {
2013-10-05 01:30:54 +00:00
eval([
typoJS,
LZString,
'aff = LZString.decompressFromUTF16(aff);',
'dic = LZString.decompressFromUTF16(dic);',
2013-10-05 01:30:54 +00:00
'dictionary = new Typo(lang, aff, dic);'
2013-11-05 23:03:38 +00:00
].join('\n'));
2013-10-05 01:30:54 +00:00
};
2013-11-05 23:03:38 +00:00
/*jshint evil:false, unused:true */
2013-10-05 01:30:54 +00:00
2013-11-05 23:03:38 +00:00
var timeoutId;
2013-10-05 01:30:54 +00:00
self.check = function(words) {
// Check function has priority over Suggest function
// This prevents Suggest to run if called just before Check
timeoutId && clearTimeout(timeoutId);
for (var i = 0; i < words.length; i++) {
var word = words[i];
word.check = dictionary.check(word.value);
}
postMessage(JSON.stringify(['check', words]));
};
2013-11-05 23:03:38 +00:00
var word;
2013-10-05 01:30:54 +00:00
function delayedSuggest() {
timeoutId = undefined;
var suggestions = dictionary.suggest(word);
postMessage(JSON.stringify(['suggest', suggestions]));
}
self.suggest = function(wordParam) {
word = wordParam;
timeoutId = setTimeout(delayedSuggest, 50);
};