feat: add session.addWordToSpellCheckerDictionary to allow custom words in the dictionary (#21266)
* feat: add session.addWordToSpellCheckerDictionary to allow custom words in the dictionary * Update session.md
This commit is contained in:
parent
8a9c7c484b
commit
149aaeba94
3 changed files with 38 additions and 0 deletions
|
@ -464,12 +464,16 @@ The built in spellchecker does not automatically detect what language a user is
|
||||||
spell checker to correctly check their words you must call this API with an array of language codes. You can
|
spell checker to correctly check their words you must call this API with an array of language codes. You can
|
||||||
get the list of supported language codes with the `ses.availableSpellCheckerLanguages` property.
|
get the list of supported language codes with the `ses.availableSpellCheckerLanguages` property.
|
||||||
|
|
||||||
|
**Note:** On macOS the OS spellchecker is used and will detect your language automatically. This API is a no-op on macOS.
|
||||||
|
|
||||||
#### `ses.getSpellCheckerLanguages()`
|
#### `ses.getSpellCheckerLanguages()`
|
||||||
|
|
||||||
Returns `String[]` - An array of language codes the spellchecker is enabled for. If this list is empty the spellchecker
|
Returns `String[]` - An array of language codes the spellchecker is enabled for. If this list is empty the spellchecker
|
||||||
will fallback to using `en-US`. By default on launch if this setting is an empty list Electron will try to populate this
|
will fallback to using `en-US`. By default on launch if this setting is an empty list Electron will try to populate this
|
||||||
setting with the current OS locale. This setting is persisted across restarts.
|
setting with the current OS locale. This setting is persisted across restarts.
|
||||||
|
|
||||||
|
**Note:** On macOS the OS spellchecker is used and has it's own list of languages. This API is a no-op on macOS.
|
||||||
|
|
||||||
#### `ses.setSpellCheckerDictionaryDownloadURL(url)`
|
#### `ses.setSpellCheckerDictionaryDownloadURL(url)`
|
||||||
|
|
||||||
* `url` String - A base URL for Electron to download hunspell dictionaries from.
|
* `url` String - A base URL for Electron to download hunspell dictionaries from.
|
||||||
|
@ -479,6 +483,16 @@ behavior you can use this API to point the dictionary downloader at your own hos
|
||||||
dictionaries. We publish a `hunspell_dictionaries.zip` file with each release which contains the files you need
|
dictionaries. We publish a `hunspell_dictionaries.zip` file with each release which contains the files you need
|
||||||
to host here.
|
to host here.
|
||||||
|
|
||||||
|
**Note:** On macOS the OS spellchecker is used and therefore we do not download any dictionary files. This API is a no-op on macOS.
|
||||||
|
|
||||||
|
#### `ses.addWordToSpellCheckerDictionary(word)`
|
||||||
|
|
||||||
|
* `word` String - The word you want to add to the dictionary
|
||||||
|
|
||||||
|
Returns `Boolean` - Whether the word was successfully written to the custom dictionary.
|
||||||
|
|
||||||
|
**Note:** On macOS and Windows 10 this word will be written to the OS custom dictionary as well
|
||||||
|
|
||||||
### Instance Properties
|
### Instance Properties
|
||||||
|
|
||||||
The following properties are available on instances of `Session`:
|
The following properties are available on instances of `Session`:
|
||||||
|
|
|
@ -69,9 +69,16 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
||||||
|
#include "chrome/browser/spellchecker/spellcheck_factory.h"
|
||||||
#include "chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h"
|
#include "chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h"
|
||||||
|
#include "chrome/browser/spellchecker/spellcheck_service.h"
|
||||||
#include "components/spellcheck/browser/pref_names.h"
|
#include "components/spellcheck/browser/pref_names.h"
|
||||||
#include "components/spellcheck/common/spellcheck_common.h"
|
#include "components/spellcheck/common/spellcheck_common.h"
|
||||||
|
|
||||||
|
#if BUILDFLAG(USE_BROWSER_SPELLCHECKER)
|
||||||
|
#include "components/spellcheck/browser/spellcheck_platform.h"
|
||||||
|
#include "components/spellcheck/common/spellcheck_features.h"
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
using content::BrowserThread;
|
using content::BrowserThread;
|
||||||
|
@ -700,6 +707,20 @@ void SetSpellCheckerDictionaryDownloadURL(gin_helper::ErrorThrower thrower,
|
||||||
}
|
}
|
||||||
SpellcheckHunspellDictionary::SetDownloadURLForTesting(url);
|
SpellcheckHunspellDictionary::SetDownloadURLForTesting(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Session::AddWordToSpellCheckerDictionary(const std::string& word) {
|
||||||
|
#if BUILDFLAG(USE_BROWSER_SPELLCHECKER)
|
||||||
|
if (spellcheck::UseBrowserSpellChecker()) {
|
||||||
|
spellcheck_platform::AddWord(base::UTF8ToUTF16(word));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
SpellcheckService* spellcheck =
|
||||||
|
SpellcheckServiceFactory::GetForContext(browser_context_.get());
|
||||||
|
if (!spellcheck)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return spellcheck->GetCustomDictionary()->AddWord(word);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// static
|
// static
|
||||||
|
@ -780,6 +801,8 @@ void Session::BuildPrototype(v8::Isolate* isolate,
|
||||||
&spellcheck::SpellCheckLanguages)
|
&spellcheck::SpellCheckLanguages)
|
||||||
.SetMethod("setSpellCheckerDictionaryDownloadURL",
|
.SetMethod("setSpellCheckerDictionaryDownloadURL",
|
||||||
&SetSpellCheckerDictionaryDownloadURL)
|
&SetSpellCheckerDictionaryDownloadURL)
|
||||||
|
.SetMethod("addWordToSpellCheckerDictionary",
|
||||||
|
&Session::AddWordToSpellCheckerDictionary)
|
||||||
#endif
|
#endif
|
||||||
.SetMethod("preconnect", &Session::Preconnect)
|
.SetMethod("preconnect", &Session::Preconnect)
|
||||||
.SetProperty("cookies", &Session::Cookies)
|
.SetProperty("cookies", &Session::Cookies)
|
||||||
|
|
|
@ -92,6 +92,7 @@ class Session : public gin_helper::TrackableObject<Session>,
|
||||||
base::Value GetSpellCheckerLanguages();
|
base::Value GetSpellCheckerLanguages();
|
||||||
void SetSpellCheckerLanguages(gin_helper::ErrorThrower thrower,
|
void SetSpellCheckerLanguages(gin_helper::ErrorThrower thrower,
|
||||||
const std::vector<std::string>& languages);
|
const std::vector<std::string>& languages);
|
||||||
|
bool AddWordToSpellCheckerDictionary(const std::string& word);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
||||||
|
|
Loading…
Reference in a new issue