-
Notifications
You must be signed in to change notification settings - Fork 973
Commit
This implements highlighting of misspelled words. It also implements a context menu when right clicking on a misspelled word for up to the top 3 suggestions. You can also learn the word, in which case it will be suggested in future similar lookups, or ignore it so it won't be included in future lookups but will not be underlined Fix #859 Auditors: @diracdeltas
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// @flow | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
'use strict' | ||
|
||
const spellchecker = require('spellchecker') | ||
const messages = require('../js/constants/messages') | ||
const electron = require('electron') | ||
const ipcMain = electron.ipcMain | ||
const app = electron.app | ||
const appStore = require('../js/stores/appStore') | ||
const appActions = require('../js/actions/appActions') | ||
|
||
// Stores a reference to the last added immutable words | ||
let lastAddedWords | ||
|
||
const isMisspelled = (word) => | ||
!appStore.getState().getIn(['dictionary', 'ignoredWords']).includes(word) && | ||
!appStore.getState().getIn(['dictionary', 'addedWords']).includes(word) && | ||
spellchecker.isMisspelled(word) | ||
|
||
module.exports.init = () => { | ||
ipcMain.on(messages.IS_MISSPELLED, (e, word) => { | ||
e.returnValue = isMisspelled(word) | ||
}) | ||
ipcMain.on(messages.GET_MISSPELLING_INFO, (e, word) => { | ||
const misspelled = isMisspelled(word) | ||
e.returnValue = { | ||
isMisspelled: misspelled, | ||
suggestions: !misspelled ? [] : spellchecker.getCorrectionsForMisspelling(word) | ||
} | ||
}) | ||
|
||
appStore.addChangeListener(() => { | ||
let addedWords = appStore.getState().getIn(['dictionary', 'addedWords']) | ||
if (lastAddedWords !== addedWords) { | ||
if (lastAddedWords) { | ||
addedWords = addedWords.splice(lastAddedWords.size) | ||
} | ||
addedWords.forEach(spellchecker.add.bind(spellchecker)) | ||
This comment has been minimized.
Sorry, something went wrong.
diracdeltas
Member
|
||
lastAddedWords = appStore.getState().getIn(['dictionary', 'addedWords']) | ||
} | ||
}) | ||
|
||
const availableDictionaries = spellchecker.getAvailableDictionaries() | ||
let dict = app.getLocale().replace('-', '_') | ||
let dictionaryLocale | ||
if (availableDictionaries.includes(dict)) { | ||
dictionaryLocale = dict | ||
} else { | ||
dict = app.getLocale().split('-')[0] | ||
if (availableDictionaries.includes(dict)) { | ||
dictionaryLocale = dict | ||
} | ||
} | ||
|
||
if (dictionaryLocale) { | ||
appActions.setDictionary(dictionaryLocale) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
'use strict' | ||
|
||
const getWebview = () => | ||
document.querySelector('.frameWrapper.isActive webview') | ||
|
||
const webviewActions = { | ||
/** | ||
* Puts the webview in focus | ||
*/ | ||
setWebviewFocused: function () { | ||
const webview = getWebview() | ||
if (webview) { | ||
webview.focus() | ||
} | ||
}, | ||
|
||
/** | ||
* Inspect the element for the active webview at the x, y content position | ||
* @param {number} x - horizontal position of the element to inspect | ||
* @param {number} y - vertical position of the element to inspect | ||
*/ | ||
inspectElement: function (x, y) { | ||
const webview = getWebview() | ||
if (webview) { | ||
webview.inspectElement(x, y) | ||
} | ||
}, | ||
|
||
/** | ||
* Repalces the selected text in an editable | ||
* @param {string} text - The text to replace with | ||
*/ | ||
replaceMisspelling: function (text) { | ||
const webview = getWebview() | ||
if (webview) { | ||
webview.replaceMisspelling(text) | ||
This comment has been minimized.
Sorry, something went wrong.
diracdeltas
Member
|
||
} | ||
} | ||
} | ||
|
||
module.exports = webviewActions |
1 comment
on commit 9343742
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm other than comments above
not a blocker, but this commit causes Flow errors in
brave-default.js