From be14517caf5ee7e4aaa0e9ae67108305d3b6ba41 Mon Sep 17 00:00:00 2001 From: akashnimare Date: Tue, 17 Oct 2017 00:29:50 +0530 Subject: [PATCH 1/6] Set server language for spellchecker on macOS Ideally spellchecker should detect the language, but on macOS, it fails to auto-detect the lanugage user is typing in that's why we need to mention it explicitly. We set this language with the help of the default language of the server. --- app/renderer/js/preload.js | 14 ++++++++++++-- app/renderer/js/spellchecker.js | 17 ++++++++++++++--- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/app/renderer/js/preload.js b/app/renderer/js/preload.js index 6459d3545..8f832754c 100644 --- a/app/renderer/js/preload.js +++ b/app/renderer/js/preload.js @@ -2,6 +2,9 @@ const { ipcRenderer } = require('electron'); const { spellChecker } = require('./spellchecker'); + +const ConfigUtil = require(__dirname + '/utils/config-util.js'); + // eslint-disable-next-line import/no-unassigned-import require('./notification'); @@ -32,8 +35,15 @@ process.once('loaded', () => { // To prevent failing this script on linux we need to load it after the document loaded document.addEventListener('DOMContentLoaded', () => { - // Init spellchecker - spellChecker(); + // Get the default language of the server + const serverLanguage = page_params.default_language; // eslint-disable-line no-undef, camelcase + + if (serverLanguage) { + // Set spellcheker language + ConfigUtil.setConfigItem('spellcheckerLanguage', serverLanguage); + // Init spellchecker + spellChecker(); + } // redirect users to network troubleshooting page const getRestartButton = document.querySelector('.restart_get_events_button'); diff --git a/app/renderer/js/spellchecker.js b/app/renderer/js/spellchecker.js index 00accaf6c..a4dea7d48 100644 --- a/app/renderer/js/spellchecker.js +++ b/app/renderer/js/spellchecker.js @@ -1,13 +1,24 @@ 'use strict'; -const {SpellCheckHandler, ContextMenuListener, ContextMenuBuilder} = require('electron-spellchecker'); +const { SpellCheckHandler, ContextMenuListener, ContextMenuBuilder } = require('electron-spellchecker'); + +const ConfigUtil = require(__dirname + '/utils/config-util.js'); function spellChecker() { // Implement spellcheck using electron api window.spellCheckHandler = new SpellCheckHandler(); window.spellCheckHandler.attachToInput(); - // Start off as US English + const userLanguage = ConfigUtil.getConfigItem('spellcheckerLanguage'); + + // On macOS, spellchecker fails to auto-detect the lanugage user is typing in + // that's why we need to mention it explicitly + if (process.platform === 'darwin') { + 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'); const contextMenuBuilder = new ContextMenuBuilder(window.spellCheckHandler); @@ -18,7 +29,7 @@ function spellChecker() { // Clean up events after you navigate away from this page; // otherwise, you may experience errors window.addEventListener('beforeunload', () => { - // eslint-disable-next-line no-undef + // eslint-disable-next-line no-undef spellCheckHandler.unsubscribe(); contextMenuListener.unsubscribe(); }); From 89a292559dd1f9704aeb1a44f11eae70a243da69 Mon Sep 17 00:00:00 2001 From: akashnimare Date: Tue, 17 Oct 2017 21:57:51 +0530 Subject: [PATCH 2/6] Set English as default language for spellchecker on Linux/Windows --- app/renderer/js/spellchecker.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/app/renderer/js/spellchecker.js b/app/renderer/js/spellchecker.js index a4dea7d48..b9ac7cb1f 100644 --- a/app/renderer/js/spellchecker.js +++ b/app/renderer/js/spellchecker.js @@ -11,15 +11,14 @@ function spellChecker() { const userLanguage = ConfigUtil.getConfigItem('spellcheckerLanguage'); - // On macOS, spellchecker fails to auto-detect the lanugage user is typing in - // that's why we need to mention it explicitly - if (process.platform === 'darwin') { - 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'); + // 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'); const contextMenuBuilder = new ContextMenuBuilder(window.spellCheckHandler); const contextMenuListener = new ContextMenuListener(info => { From 7afcf134010a93199dc372254c3f77d1b49b55e9 Mon Sep 17 00:00:00 2001 From: akashnimare Date: Wed, 18 Oct 2017 04:14:02 +0530 Subject: [PATCH 3/6] Re-write and improve spellchecker class Rewrote the Spellchecker class so that we can have better control over the context menu and spellchecker. --- app/renderer/js/preload.js | 11 ++++- app/renderer/js/spellchecker.js | 73 +++++++++++++++++++-------------- 2 files changed, 52 insertions(+), 32 deletions(-) diff --git a/app/renderer/js/preload.js b/app/renderer/js/preload.js index 8f832754c..c554ecb0a 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 b9ac7cb1f..dc1815a0a 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(); - - const userLanguage = ConfigUtil.getConfigItem('spellcheckerLanguage'); - - // 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'); - - const contextMenuBuilder = new ContextMenuBuilder(window.spellCheckHandler); - const contextMenuListener = new ContextMenuListener(info => { - contextMenuBuilder.showPopupMenu(info); - }); - - // Clean up events after you navigate away from this page; - // otherwise, you may experience errors - window.addEventListener('beforeunload', () => { +class SetupSpellChecker { + init() { + this.enableSpellChecker(); + 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'); + + // 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(); From a498ffc7d6f818f88cdb0a5af2663d0df3b34bb6 Mon Sep 17 00:00:00 2001 From: akashnimare Date: Wed, 18 Oct 2017 04:17:51 +0530 Subject: [PATCH 4/6] Update spellchecker to v1.1.2 --- app/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/package.json b/app/package.json index 0a073675c..1b0cad54d 100644 --- a/app/package.json +++ b/app/package.json @@ -31,7 +31,7 @@ "electron-is-dev": "0.3.0", "electron-localshortcut": "2.0.2", "electron-log": "2.2.7", - "electron-spellchecker": "1.2.0", + "electron-spellchecker": "1.1.2", "electron-updater": "2.8.9", "node-json-db": "0.7.3", "request": "2.81.0", From 73603a4fd23a224f241c3d70c843bd74d2ff2430 Mon Sep 17 00:00:00 2001 From: akashnimare Date: Wed, 18 Oct 2017 21:36:01 +0530 Subject: [PATCH 5/6] Add settings to disable/enable spellchecker --- .../js/pages/preference/general-section.js | 17 +++++++++++++++++ app/renderer/js/spellchecker.js | 8 ++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/app/renderer/js/pages/preference/general-section.js b/app/renderer/js/pages/preference/general-section.js index 55c5fcfed..f5a59974a 100644 --- a/app/renderer/js/pages/preference/general-section.js +++ b/app/renderer/js/pages/preference/general-section.js @@ -57,6 +57,10 @@ class GeneralSection extends BaseSection {
Start app at login
+
+
Enable Spellchecker (requires restart)
+
+
Reset Application Data
@@ -80,6 +84,7 @@ class GeneralSection extends BaseSection { this.updateStartAtLoginOption(); this.updateResetDataOption(); this.showDesktopNotification(); + this.enableSpellchecker(); } updateTrayOption() { @@ -170,6 +175,18 @@ class GeneralSection extends BaseSection { }); } + enableSpellchecker() { + this.generateSettingOption({ + $element: document.querySelector('#enable-spellchecker-option .setting-control'), + value: ConfigUtil.getConfigItem('enableSpellchecker', true), + clickHandler: () => { + const newValue = !ConfigUtil.getConfigItem('enableSpellchecker'); + ConfigUtil.setConfigItem('enableSpellchecker', newValue); + this.enableSpellchecker(); + } + }); + } + clearAppDataDialog() { const clearAppDataMessage = 'By clicking proceed you will be removing all added accounts and preferences from Zulip. When the application restarts, it will be as if you are starting Zulip for the first time.'; const getAppPath = path.join(app.getPath('appData'), app.getName()); diff --git a/app/renderer/js/spellchecker.js b/app/renderer/js/spellchecker.js index dc1815a0a..751a62139 100644 --- a/app/renderer/js/spellchecker.js +++ b/app/renderer/js/spellchecker.js @@ -6,7 +6,9 @@ const ConfigUtil = require(__dirname + '/utils/config-util.js'); class SetupSpellChecker { init() { - this.enableSpellChecker(); + if (ConfigUtil.getConfigItem('enableSpellchecker')) { + this.enableSpellChecker(); + } this.enableContextMenu(); } @@ -45,7 +47,9 @@ class SetupSpellChecker { if (this.SpellCheckHandler) { this.SpellCheckHandler.unsubscribe(); } - this.contextMenuListener.unsubscribe(); + if (this.contextMenuListener) { + this.contextMenuListener.unsubscribe(); + } } } From fae05fc3b16302e65a822b2bef35e1c6caf9c171 Mon Sep 17 00:00:00 2001 From: akashnimare Date: Wed, 18 Oct 2017 21:38:48 +0530 Subject: [PATCH 6/6] Initialize default app settings Settings are initialized only when user clicks on General/Server/Network section settings In case, user doesn't visit these section, those values set to be null automatically. This fix makes sure the default settings are correctly set to either true or false. --- .../js/pages/preference/preference.js | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/app/renderer/js/pages/preference/preference.js b/app/renderer/js/pages/preference/preference.js index 7204308ee..3c55cb4e4 100644 --- a/app/renderer/js/pages/preference/preference.js +++ b/app/renderer/js/pages/preference/preference.js @@ -1,7 +1,9 @@ 'use strict'; const BaseComponent = require(__dirname + '/js/components/base.js'); -const {ipcRenderer} = require('electron'); +const { ipcRenderer } = require('electron'); + +const ConfigUtil = require(__dirname + '/js/utils/config-util.js'); const Nav = require(__dirname + '/js/pages/preference/nav.js'); const ServersSection = require(__dirname + '/js/pages/preference/servers-section.js'); @@ -25,6 +27,7 @@ class PreferenceView extends BaseComponent { this.setDefaultView(); this.registerIpcs(); + this.setDefaultSettings(); } setDefaultView() { @@ -36,6 +39,30 @@ class PreferenceView extends BaseComponent { this.handleNavigation(nav); } + // Settings are initialized only when user clicks on General/Server/Network section settings + // In case, user doesn't visit these section, those values set to be null automatically + // This will make sure the default settings are correctly set to either true or false + setDefaultSettings() { + // Default settings which should be respected + const settingOptions = { + trayIcon: true, + useProxy: false, + showSidebar: true, + badgeOption: true, + startAtLogin: false, + enableSpellchecker: true, + showNotification: true, + betaUpdate: false, + silent: false + }; + + for (const i in settingOptions) { + if (ConfigUtil.getConfigItem(i) === null) { + ConfigUtil.setConfigItem(i, settingOptions[i]); + } + } + } + handleNavigation(navItem) { this.nav.select(navItem); switch (navItem) {