Skip to content

Commit

Permalink
feat: add session.addWordToSpellCheckerDictionary to allow custom wor…
Browse files Browse the repository at this point in the history
…ds in the dictionary (#21297)

* feat: add session.addWordToSpellCheckerDictionary to allow custom words in the dictionary

* Update session.md

Co-authored-by: Samuel Attard <samuel.r.attard@gmail.com>
  • Loading branch information
2 people authored and John Kleinschmidt committed Jan 16, 2020
1 parent 06868c9 commit 048f06c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docs/api/session.md
Expand Up @@ -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.
Expand All @@ -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`:
Expand Down
23 changes: 23 additions & 0 deletions shell/browser/api/atom_api_session.cc
Expand Up @@ -71,9 +71,16 @@
#endif

#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
#include "chrome/browser/spellchecker/spellcheck_factory.h"
#include "chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h" // nogncheck
#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;
Expand Down Expand Up @@ -708,6 +715,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
Expand Down Expand Up @@ -788,6 +809,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)
Expand Down
1 change: 1 addition & 0 deletions shell/browser/api/atom_api_session.h
Expand Up @@ -91,6 +91,7 @@ class Session : public mate::TrackableObject<Session>,
base::Value GetSpellCheckerLanguages();
void SetSpellCheckerLanguages(gin_helper::ErrorThrower thrower,
const std::vector<std::string>& languages);
bool AddWordToSpellCheckerDictionary(const std::string& word);
#endif

#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
Expand Down

0 comments on commit 048f06c

Please sign in to comment.