feat: add session.listWordsFromSpellCheckerDictionary API (#22101)
* doesn't work yet but compiles. * works * fixup
This commit is contained in:
parent
6fe7d65c9d
commit
9942149f3c
3 changed files with 68 additions and 0 deletions
|
@ -487,6 +487,11 @@ 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.
|
**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.listWordsInSpellCheckerDictionary()`
|
||||||
|
|
||||||
|
Returns `Promise<String[]>` - An array of all words in app's custom dictionary.
|
||||||
|
Resolves when the full dictionary is loaded from disk.
|
||||||
|
|
||||||
#### `ses.addWordToSpellCheckerDictionary(word)`
|
#### `ses.addWordToSpellCheckerDictionary(word)`
|
||||||
|
|
||||||
* `word` String - The word you want to add to the dictionary
|
* `word` String - The word you want to add to the dictionary
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <set>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
@ -226,6 +227,42 @@ void DestroyGlobalHandle(v8::Isolate* isolate,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class DictionaryObserver final : public SpellcheckCustomDictionary::Observer {
|
||||||
|
private:
|
||||||
|
std::unique_ptr<gin_helper::Promise<std::set<std::string>>> promise_;
|
||||||
|
base::WeakPtr<SpellcheckService> spellcheck_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
DictionaryObserver(gin_helper::Promise<std::set<std::string>> promise,
|
||||||
|
base::WeakPtr<SpellcheckService> spellcheck)
|
||||||
|
: spellcheck_(spellcheck) {
|
||||||
|
promise_ = std::make_unique<gin_helper::Promise<std::set<std::string>>>(
|
||||||
|
std::move(promise));
|
||||||
|
if (spellcheck_)
|
||||||
|
spellcheck_->GetCustomDictionary()->AddObserver(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
~DictionaryObserver() {
|
||||||
|
if (spellcheck_)
|
||||||
|
spellcheck_->GetCustomDictionary()->RemoveObserver(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnCustomDictionaryLoaded() override {
|
||||||
|
if (spellcheck_) {
|
||||||
|
promise_->Resolve(spellcheck_->GetCustomDictionary()->GetWords());
|
||||||
|
} else {
|
||||||
|
promise_->RejectWithErrorMessage(
|
||||||
|
"Spellcheck in unexpected state: failed to load custom dictionary.");
|
||||||
|
}
|
||||||
|
delete this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnCustomDictionaryChanged(
|
||||||
|
const SpellcheckCustomDictionary::Change& dictionary_change) override {
|
||||||
|
// noop
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
Session::Session(v8::Isolate* isolate, ElectronBrowserContext* browser_context)
|
Session::Session(v8::Isolate* isolate, ElectronBrowserContext* browser_context)
|
||||||
|
@ -771,6 +808,29 @@ void SetSpellCheckerDictionaryDownloadURL(gin_helper::ErrorThrower thrower,
|
||||||
SpellcheckHunspellDictionary::SetDownloadURLForTesting(url);
|
SpellcheckHunspellDictionary::SetDownloadURLForTesting(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
v8::Local<v8::Promise> Session::ListWordsInSpellCheckerDictionary() {
|
||||||
|
gin_helper::Promise<std::set<std::string>> promise(isolate());
|
||||||
|
v8::Local<v8::Promise> handle = promise.GetHandle();
|
||||||
|
|
||||||
|
SpellcheckService* spellcheck =
|
||||||
|
SpellcheckServiceFactory::GetForContext(browser_context_.get());
|
||||||
|
|
||||||
|
if (!spellcheck)
|
||||||
|
promise.RejectWithErrorMessage(
|
||||||
|
"Spellcheck in unexpected state: failed to load custom dictionary.");
|
||||||
|
|
||||||
|
if (spellcheck->GetCustomDictionary()->IsLoaded()) {
|
||||||
|
promise.Resolve(spellcheck->GetCustomDictionary()->GetWords());
|
||||||
|
} else {
|
||||||
|
new DictionaryObserver(std::move(promise), spellcheck->GetWeakPtr());
|
||||||
|
// Dictionary loads by default asynchronously,
|
||||||
|
// call the load function anyways just to be sure.
|
||||||
|
spellcheck->GetCustomDictionary()->Load();
|
||||||
|
}
|
||||||
|
|
||||||
|
return handle;
|
||||||
|
}
|
||||||
|
|
||||||
bool Session::AddWordToSpellCheckerDictionary(const std::string& word) {
|
bool Session::AddWordToSpellCheckerDictionary(const std::string& word) {
|
||||||
SpellcheckService* service =
|
SpellcheckService* service =
|
||||||
SpellcheckServiceFactory::GetForContext(browser_context_.get());
|
SpellcheckServiceFactory::GetForContext(browser_context_.get());
|
||||||
|
@ -886,6 +946,8 @@ void Session::BuildPrototype(v8::Isolate* isolate,
|
||||||
&spellcheck::SpellCheckLanguages)
|
&spellcheck::SpellCheckLanguages)
|
||||||
.SetMethod("setSpellCheckerDictionaryDownloadURL",
|
.SetMethod("setSpellCheckerDictionaryDownloadURL",
|
||||||
&SetSpellCheckerDictionaryDownloadURL)
|
&SetSpellCheckerDictionaryDownloadURL)
|
||||||
|
.SetMethod("listWordsInSpellCheckerDictionary",
|
||||||
|
&Session::ListWordsInSpellCheckerDictionary)
|
||||||
.SetMethod("addWordToSpellCheckerDictionary",
|
.SetMethod("addWordToSpellCheckerDictionary",
|
||||||
&Session::AddWordToSpellCheckerDictionary)
|
&Session::AddWordToSpellCheckerDictionary)
|
||||||
.SetMethod("removeWordFromSpellCheckerDictionary",
|
.SetMethod("removeWordFromSpellCheckerDictionary",
|
||||||
|
|
|
@ -95,6 +95,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);
|
||||||
|
v8::Local<v8::Promise> ListWordsInSpellCheckerDictionary();
|
||||||
bool AddWordToSpellCheckerDictionary(const std::string& word);
|
bool AddWordToSpellCheckerDictionary(const std::string& word);
|
||||||
bool RemoveWordFromSpellCheckerDictionary(const std::string& word);
|
bool RemoveWordFromSpellCheckerDictionary(const std::string& word);
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue