Re-write and improve spellchecker class

Rewrote the Spellchecker class so that we can have better control
over the context menu and spellchecker.
This commit is contained in:
akashnimare
2017-10-18 04:14:02 +05:30
parent 89a292559d
commit 7afcf13401
2 changed files with 48 additions and 28 deletions

View File

@@ -1,7 +1,7 @@
'use strict'; 'use strict';
const { ipcRenderer } = require('electron'); const { ipcRenderer } = require('electron');
const { spellChecker } = require('./spellchecker'); const SetupSpellChecker = require('./spellchecker');
const ConfigUtil = require(__dirname + '/utils/config-util.js'); const ConfigUtil = require(__dirname + '/utils/config-util.js');
@@ -42,7 +42,7 @@ document.addEventListener('DOMContentLoaded', () => {
// Set spellcheker language // Set spellcheker language
ConfigUtil.setConfigItem('spellcheckerLanguage', serverLanguage); ConfigUtil.setConfigItem('spellcheckerLanguage', serverLanguage);
// Init spellchecker // Init spellchecker
spellChecker(); SetupSpellChecker.init();
} }
// redirect users to network troubleshooting page // 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();
});

View File

@@ -4,10 +4,23 @@ const { SpellCheckHandler, ContextMenuListener, ContextMenuBuilder } = require('
const ConfigUtil = require(__dirname + '/utils/config-util.js'); const ConfigUtil = require(__dirname + '/utils/config-util.js');
function spellChecker() { class SetupSpellChecker {
// Implement spellcheck using electron api init() {
window.spellCheckHandler = new SpellCheckHandler(); this.enableSpellChecker();
window.spellCheckHandler.attachToInput(); this.enableContextMenu();
}
enableSpellChecker() {
try {
this.SpellCheckHandler = new SpellCheckHandler();
} catch (err) {
console.log(err);
}
}
enableContextMenu() {
if (this.SpellCheckHandler) {
this.SpellCheckHandler.attachToInput();
const userLanguage = ConfigUtil.getConfigItem('spellcheckerLanguage'); const userLanguage = ConfigUtil.getConfigItem('spellcheckerLanguage');
@@ -15,25 +28,25 @@ function spellChecker() {
process.platform === 'darwin' ? process.platform === 'darwin' ?
// On macOS, spellchecker fails to auto-detect the lanugage user is typing in // On macOS, spellchecker fails to auto-detect the lanugage user is typing in
// that's why we need to mention it explicitly // that's why we need to mention it explicitly
window.spellCheckHandler.switchLanguage(userLanguage) : this.SpellCheckHandler.switchLanguage(userLanguage) :
// On Linux and Windows, spellchecker can automatically detects the language the user is typing in // 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 // and silently switches on the fly; thus we can start off as US English
window.spellCheckHandler.switchLanguage('en-US'); this.SpellCheckHandler.switchLanguage('en-US');
}
const contextMenuBuilder = new ContextMenuBuilder(window.spellCheckHandler); const contextMenuBuilder = new ContextMenuBuilder(this.SpellCheckHandler);
const contextMenuListener = new ContextMenuListener(info => { this.contextMenuListener = new ContextMenuListener(info => {
contextMenuBuilder.showPopupMenu(info); contextMenuBuilder.showPopupMenu(info);
}); });
// Clean up events after you navigate away from this page;
// otherwise, you may experience errors
window.addEventListener('beforeunload', () => {
// eslint-disable-next-line no-undef
spellCheckHandler.unsubscribe();
contextMenuListener.unsubscribe();
});
} }
module.exports = { unsubscribeSpellChecker() {
spellChecker // eslint-disable-next-line no-undef
}; if (this.SpellCheckHandler) {
this.SpellCheckHandler.unsubscribe();
}
this.contextMenuListener.unsubscribe();
}
}
module.exports = new SetupSpellChecker();