ELECTRON-869 Enable spellchecker for main window (#547)

This commit is contained in:
Kiran Niranjan
2019-01-14 17:28:20 +05:30
committed by Vishwas Shashidhar
parent 6de3c05223
commit a206b567c2
5 changed files with 59 additions and 3 deletions

View File

@@ -50,8 +50,6 @@ require('./mainApiMgr.js');
require('./memoryMonitor.js');
const windowMgr = require('./windowMgr.js');
const SpellChecker = require('./spellChecker').SpellCheckHelper;
const spellchecker = new SpellChecker();
const { ContextMenuBuilder } = require('electron-spellchecker');
const i18n = require('./translation/i18n');
@@ -252,6 +250,7 @@ app.on('web-contents-created', function (event, webContents) {
});
function onWebContent(webContents) {
const spellchecker = windowMgr.getSpellchecker();
spellchecker.initializeSpellChecker();
spellchecker.updateContextMenuLocale(i18n.getMessageFor('ContextMenu'));
const contextMenuBuilder = new ContextMenuBuilder(spellchecker.spellCheckHandler, webContents, false, spellchecker.processMenu.bind(spellchecker));

View File

@@ -180,6 +180,13 @@ electron.ipcMain.on(apiName, (event, arg) => {
windowMgr.handleKeyPress(arg.keyCode);
}
break;
case apiCmds.isMisspelled:
if (typeof arg.text === 'string') {
/* eslint-disable no-param-reassign */
event.returnValue = windowMgr.isMisspelled(arg.text);
/* eslint-enable no-param-reassign */
}
break;
case apiCmds.openScreenSharingIndicator:
if (typeof arg.displayId === 'string' && typeof arg.id === 'number') {
openScreenSharingIndicator(event.sender, arg.displayId, arg.id);

View File

@@ -11,7 +11,7 @@
// also to bring pieces of node.js:
// https://github.com/electron/electron/issues/2984
//
const { ipcRenderer, remote, crashReporter } = require('electron');
const { ipcRenderer, remote, crashReporter, webFrame } = require('electron');
const throttle = require('../utils/throttle.js');
const apiEnums = require('../enums/api.js');
@@ -86,6 +86,20 @@ const throttledSetIsInMeetingStatus = throttle(1000, function (isInMeeting) {
local.ipcRenderer.on('on-page-load', () => {
snackBar = new SnackBar();
// Enable spellchecker only for main window until
// the underline memory leak is fixed on electron
// https://github.com/electron/electron/issues/15459
if (window.name === 'main') {
webFrame.setSpellCheckProvider('en-US', true, {
spellCheck (text) {
return !local.ipcRenderer.sendSync(apiName, {
cmd: apiCmds.isMisspelled,
text
});
}
});
}
// only registers main window's preload
if (window.name === 'main') {
local.ipcRenderer.send(apiName, {

View File

@@ -95,6 +95,16 @@ class SpellCheckHelper {
return menu;
}
/**
* Method that checks if a text is misspelled
*
* @param text {string}
* @returns {*}
*/
isMisspelled(text) {
return this.spellCheckHandler.isMisspelled(text);
}
}
module.exports = {

View File

@@ -27,6 +27,9 @@ const { initCrashReporterMain, initCrashReporterRenderer } = require('./crashRep
const i18n = require('./translation/i18n');
const getCmdLineArg = require('./utils/getCmdLineArg');
const SpellChecker = require('./spellChecker').SpellCheckHelper;
const spellchecker = new SpellChecker();
// show dialog when certificate errors occur
require('./dialogs/showCertError.js');
require('./dialogs/showBasicAuth.js');
@@ -100,6 +103,27 @@ function getParsedUrl(appUrl) {
return nodeURL.parse(url);
}
/**
* Returns the Spellchecker instance
* @returns {SpellCheckHelper}
*/
function getSpellchecker() {
return spellchecker;
}
/**
* Method that invokes native module that
* verifies missed spelled word
* @param text {string}
* @returns {*}
*/
function isMisspelled(text) {
if (!spellchecker) {
return false;
}
return spellchecker.isMisspelled(text);
}
/**
* Creates the main window
* @param initialUrl
@@ -1270,4 +1294,6 @@ module.exports = {
cleanUpChildWindows: cleanUpChildWindows,
setLocale: setLocale,
getIsOnline: getIsOnline,
getSpellchecker: getSpellchecker,
isMisspelled: isMisspelled,
};