diff --git a/src/js/spell-checker.js b/src/js/spell-checker.js index b5e990e..e7c9dc6 100644 --- a/src/js/spell-checker.js +++ b/src/js/spell-checker.js @@ -1,43 +1,52 @@ +// Initialize data globally to reduce memory consumption +var num_loaded = 0; +var aff_loading = false; +var dic_loading = false; +var aff_data = ""; +var dic_data = ""; +var typo; + + CodeMirror.defineMode("spell-checker", function(config, parserConfig) { - // Initialize data - var num_loaded = 0; - var aff_data = ""; - var dic_data = ""; - var typo; - - // Load AFF/DIC data - 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 (e) { - if (xhr_aff.readyState === 4 && xhr_aff.status === 200) { - aff_data = xhr_aff.responseText; - num_loaded++; - - if(num_loaded == 2){ - typo = new Typo("en_US", aff_data, dic_data, { - platform: 'any' - }); + if(!aff_loading){ + 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 (e) { + if (xhr_aff.readyState === 4 && xhr_aff.status === 200) { + aff_data = xhr_aff.responseText; + num_loaded++; + + if(num_loaded == 2){ + typo = new Typo("en_US", aff_data, dic_data, { + platform: 'any' + }); + } } - } - }; - xhr_aff.send(null); + }; + xhr_aff.send(null); + } - 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 (e) { - if (xhr_dic.readyState === 4 && xhr_dic.status === 200) { - dic_data = xhr_dic.responseText; - num_loaded++; - - if(num_loaded == 2){ - typo = new Typo("en_US", aff_data, dic_data, { - platform: 'any' - }); + if(!dic_loading){ + 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 (e) { + if (xhr_dic.readyState === 4 && xhr_dic.status === 200) { + dic_data = xhr_dic.responseText; + num_loaded++; + + if(num_loaded == 2){ + typo = new Typo("en_US", aff_data, dic_data, { + platform: 'any' + }); + } } - } - }; - xhr_dic.send(null); + }; + xhr_dic.send(null); + } + // Define what separates a word