From 149aaeba949b071324d939da3dab984a3cfbd621 Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Tue, 26 Nov 2019 13:16:43 -0800 Subject: [PATCH] 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 --- docs/api/session.md | 14 ++++++++++++++ shell/browser/api/atom_api_session.cc | 23 +++++++++++++++++++++++ shell/browser/api/atom_api_session.h | 1 + 3 files changed, 38 insertions(+) diff --git a/docs/api/session.md b/docs/api/session.md index 27c6d96091f2c..112636dc229c9 100644 --- a/docs/api/session.md +++ b/docs/api/session.md @@ -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 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()` 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 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)` * `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 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 The following properties are available on instances of `Session`: diff --git a/shell/browser/api/atom_api_session.cc b/shell/browser/api/atom_api_session.cc index 33fb72519c564..d4e8389abf9d0 100644 --- a/shell/browser/api/atom_api_session.cc +++ b/shell/browser/api/atom_api_session.cc @@ -69,9 +69,16 @@ #endif #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_service.h" #include "components/spellcheck/browser/pref_names.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 using content::BrowserThread; @@ -700,6 +707,20 @@ void SetSpellCheckerDictionaryDownloadURL(gin_helper::ErrorThrower thrower, } 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 // static @@ -780,6 +801,8 @@ void Session::BuildPrototype(v8::Isolate* isolate, &spellcheck::SpellCheckLanguages) .SetMethod("setSpellCheckerDictionaryDownloadURL", &SetSpellCheckerDictionaryDownloadURL) + .SetMethod("addWordToSpellCheckerDictionary", + &Session::AddWordToSpellCheckerDictionary) #endif .SetMethod("preconnect", &Session::Preconnect) .SetProperty("cookies", &Session::Cookies) diff --git a/shell/browser/api/atom_api_session.h b/shell/browser/api/atom_api_session.h index a7e63e4757cee..e63bfe1dab747 100644 --- a/shell/browser/api/atom_api_session.h +++ b/shell/browser/api/atom_api_session.h @@ -92,6 +92,7 @@ class Session : public gin_helper::TrackableObject, base::Value GetSpellCheckerLanguages(); void SetSpellCheckerLanguages(gin_helper::ErrorThrower thrower, const std::vector& languages); + bool AddWordToSpellCheckerDictionary(const std::string& word); #endif #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)