-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Electron Spellcheck (WIP PR) #7701
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
/* global require, process, _ */ | ||
|
||
/* eslint-disable strict */ | ||
const electron = require('electron'); | ||
const osLocale = require('os-locale'); | ||
const os = require('os'); | ||
const semver = require('semver'); | ||
const spellchecker = require('spellchecker'); | ||
|
||
const { remote, webFrame } = electron; | ||
|
||
// `remote.require` since `Menu` is a main-process module. | ||
const buildEditorContextMenu = remote.require('electron-editor-context-menu'); | ||
|
||
const EN_VARIANT = /^en/; | ||
|
||
// Prevent the spellchecker from showing contractions as errors. | ||
const ENGLISH_SKIP_WORDS = [ | ||
'ain', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indentation is wrong all over the shop, please review other code or see the style guide. |
||
'couldn', | ||
'didn', | ||
'doesn', | ||
'hadn', | ||
'hasn', | ||
'mightn', | ||
'mustn', | ||
'needn', | ||
'oughtn', | ||
'shan', | ||
'shouldn', | ||
'wasn', | ||
'weren', | ||
'wouldn', | ||
]; | ||
|
||
function setupLinux(locale) { | ||
if (process.env.HUNSPELL_DICTIONARIES || locale !== 'en_US') { | ||
// apt-get install hunspell-<locale> can be run for easy access | ||
// to other dictionaries | ||
const location = process.env.HUNSPELL_DICTIONARIES || '/usr/share/hunspell'; | ||
|
||
window.log.info( | ||
'Detected Linux. Setting up spell check with locale', | ||
locale, | ||
'and dictionary location', | ||
location | ||
); | ||
spellchecker.setDictionary(locale, location); | ||
} else { | ||
window.log.info( | ||
'Detected Linux. Using default en_US spell check dictionary' | ||
); | ||
} | ||
} | ||
|
||
function setupWin7AndEarlier(locale) { | ||
if (process.env.HUNSPELL_DICTIONARIES || locale !== 'en_US') { | ||
const location = process.env.HUNSPELL_DICTIONARIES; | ||
|
||
window.log.info( | ||
'Detected Windows 7 or below. Setting up spell-check with locale', | ||
locale, | ||
'and dictionary location', | ||
location | ||
); | ||
spellchecker.setDictionary(locale, location); | ||
} else { | ||
window.log.info( | ||
'Detected Windows 7 or below. Using default en_US spell check dictionary' | ||
); | ||
} | ||
} | ||
|
||
// We load locale this way and not via app.getLocale() because this call returns | ||
// 'es_ES' and not just 'es.' And hunspell requires the fully-qualified locale. | ||
const locale = osLocale.sync().replace('-', '_'); | ||
|
||
// The LANG environment variable is how node spellchecker finds its default language: | ||
// https://github.com/atom/node-spellchecker/blob/59d2d5eee5785c4b34e9669cd5d987181d17c098/lib/spellchecker.js#L29 | ||
if (!process.env.LANG) { | ||
process.env.LANG = locale; | ||
} | ||
|
||
if (process.platform === 'linux') { | ||
setupLinux(locale); | ||
} else if (process.platform === 'windows' && semver.lt(os.release(), '8.0.0')) { | ||
setupWin7AndEarlier(locale); | ||
} else { | ||
// OSX and Windows 8+ have OS-level spellcheck APIs | ||
window.log.info( | ||
'Using OS-level spell check API with locale', | ||
process.env.LANG | ||
); | ||
} | ||
|
||
const simpleChecker = { | ||
spellCheck(text) { | ||
return !this.isMisspelled(text); | ||
}, | ||
isMisspelled(text) { | ||
const misspelled = spellchecker.isMisspelled(text); | ||
|
||
// The idea is to make this as fast as possible. For the many, many calls which | ||
// don't result in the red squiggly, we minimize the number of checks. | ||
if (!misspelled) { | ||
return false; | ||
} | ||
|
||
// Only if we think we've found an error do we check the locale and skip list. | ||
if (locale.match(EN_VARIANT) && _.contains(ENGLISH_SKIP_WORDS, text)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
}, | ||
getSuggestions(text) { | ||
return spellchecker.getCorrectionsForMisspelling(text); | ||
}, | ||
add(text) { | ||
spellchecker.add(text); | ||
}, | ||
}; | ||
|
||
const dummyChecker = { | ||
spellCheck() { | ||
return true; | ||
}, | ||
isMisspelled() { | ||
return false; | ||
}, | ||
getSuggestions() { | ||
return []; | ||
}, | ||
add() { | ||
// nothing | ||
}, | ||
}; | ||
|
||
// set the languange based on the locale | ||
const osLanguage = locale.replace('_','-'); | ||
|
||
window.spellChecker = simpleChecker; | ||
window.disableSpellCheck = () => { | ||
window.removeEventListener('contextmenu', spellCheckHandler); | ||
webFrame.setSpellCheckProvider(osLanguage, dummyChecker); | ||
}; | ||
|
||
window.enableSpellCheck = () => { | ||
webFrame.setSpellCheckProvider(osLanguage,simpleChecker); | ||
window.addEventListener('contextmenu', spellCheckHandler); | ||
}; | ||
|
||
const spellCheckHandler = e => { | ||
// Only show the context menu in text editors. | ||
if (!e.target.closest('textarea, input, [contenteditable="true"]')) { | ||
return; | ||
} | ||
|
||
const selectedText = window.getSelection().toString(); | ||
const isMisspelled = selectedText && simpleChecker.isMisspelled(selectedText); | ||
const spellingSuggestions = | ||
isMisspelled && simpleChecker.getSuggestions(selectedText).slice(0, 5); | ||
const menu = buildEditorContextMenu({ | ||
isMisspelled, | ||
spellingSuggestions, | ||
}); | ||
|
||
// The 'contextmenu' event is emitted after 'selectionchange' has fired | ||
// but possibly before the visible selection has changed. Try to wait | ||
// to show the menu until after that, otherwise the visible selection | ||
// will update after the menu dismisses and look weird. | ||
setTimeout(() => { | ||
menu.popup(remote.getCurrentWindow()); | ||
}, 30); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This would result in electron getting packaged within electron unless you have electron installed globally, as it'd get installed to node_modules which LITERALLY gets copied