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';
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();
});

View File

@@ -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();