diff --git a/app/renderer/js/preload.js b/app/renderer/js/preload.js index 8f832754..c554ecb0 100644 --- a/app/renderer/js/preload.js +++ b/app/renderer/js/preload.js @@ -1,7 +1,7 @@ 'use strict'; const { ipcRenderer } = require('electron'); -const { spellChecker } = require('./spellchecker'); +const SetupSpellChecker = require('./spellchecker'); const ConfigUtil = require(__dirname + '/utils/config-util.js'); @@ -42,7 +42,7 @@ document.addEventListener('DOMContentLoaded', () => { // Set spellcheker language ConfigUtil.setConfigItem('spellcheckerLanguage', serverLanguage); // Init spellchecker - spellChecker(); + SetupSpellChecker.init(); } // redirect users to network troubleshooting page @@ -53,3 +53,10 @@ document.addEventListener('DOMContentLoaded', () => { }); } }); + +// Clean up spellchecker events after you navigate away from this page; +// otherwise, you may experience errors +window.addEventListener('beforeunload', () => { + SetupSpellChecker.unsubscribeSpellChecker(); +}); + diff --git a/app/renderer/js/spellchecker.js b/app/renderer/js/spellchecker.js index b9ac7cb1..dc1815a0 100644 --- a/app/renderer/js/spellchecker.js +++ b/app/renderer/js/spellchecker.js @@ -4,36 +4,49 @@ const { SpellCheckHandler, ContextMenuListener, ContextMenuBuilder } = require(' const ConfigUtil = require(__dirname + '/utils/config-util.js'); -function spellChecker() { - // Implement spellcheck using electron api - window.spellCheckHandler = new SpellCheckHandler(); - window.spellCheckHandler.attachToInput(); +class SetupSpellChecker { + init() { + this.enableSpellChecker(); + this.enableContextMenu(); + } - const userLanguage = ConfigUtil.getConfigItem('spellcheckerLanguage'); + enableSpellChecker() { + try { + this.SpellCheckHandler = new SpellCheckHandler(); + } catch (err) { + console.log(err); + } + } - // eslint-disable-next-line no-unused-expressions - process.platform === 'darwin' ? - // On macOS, spellchecker fails to auto-detect the lanugage user is typing in - // that's why we need to mention it explicitly - window.spellCheckHandler.switchLanguage(userLanguage) : - // On Linux and Windows, spellchecker can automatically detects the language the user is typing in - // and silently switches on the fly; thus we can start off as US English - window.spellCheckHandler.switchLanguage('en-US'); + enableContextMenu() { + if (this.SpellCheckHandler) { + this.SpellCheckHandler.attachToInput(); - const contextMenuBuilder = new ContextMenuBuilder(window.spellCheckHandler); - const contextMenuListener = new ContextMenuListener(info => { - contextMenuBuilder.showPopupMenu(info); - }); + const userLanguage = ConfigUtil.getConfigItem('spellcheckerLanguage'); - // Clean up events after you navigate away from this page; - // otherwise, you may experience errors - window.addEventListener('beforeunload', () => { + // eslint-disable-next-line no-unused-expressions + process.platform === 'darwin' ? + // On macOS, spellchecker fails to auto-detect the lanugage user is typing in + // that's why we need to mention it explicitly + this.SpellCheckHandler.switchLanguage(userLanguage) : + // On Linux and Windows, spellchecker can automatically detects the language the user is typing in + // and silently switches on the fly; thus we can start off as US English + this.SpellCheckHandler.switchLanguage('en-US'); + } + + const contextMenuBuilder = new ContextMenuBuilder(this.SpellCheckHandler); + this.contextMenuListener = new ContextMenuListener(info => { + contextMenuBuilder.showPopupMenu(info); + }); + } + + unsubscribeSpellChecker() { // eslint-disable-next-line no-undef - spellCheckHandler.unsubscribe(); - contextMenuListener.unsubscribe(); - }); + if (this.SpellCheckHandler) { + this.SpellCheckHandler.unsubscribe(); + } + this.contextMenuListener.unsubscribe(); + } } -module.exports = { - spellChecker -}; +module.exports = new SetupSpellChecker();