Call JavaScript to get spell check results

This commit is contained in:
Cheng Zhao 2014-12-18 16:44:38 -08:00
Родитель f1fbc5c701
Коммит a61331a083
3 изменённых файлов: 51 добавлений и 3 удалений

Просмотреть файл

@ -4,13 +4,21 @@
#include "atom/renderer/api/atom_api_spell_check_client.h"
#include <vector>
#include "atom/common/native_mate_converters/string16_converter.h"
#include "native_mate/converter.h"
#include "third_party/WebKit/public/web/WebTextCheckingCompletion.h"
#include "atom/common/node_includes.h"
namespace atom {
namespace api {
SpellCheckClient::SpellCheckClient(v8::Isolate* isolate,
v8::Handle<v8::Object> provider)
: provider_(isolate, provider) {}
: isolate_(isolate), provider_(isolate, provider) {}
SpellCheckClient::~SpellCheckClient() {}
@ -19,10 +27,19 @@ void SpellCheckClient::spellCheck(
int& misspelledOffset,
int& misspelledLength,
blink::WebVector<blink::WebString>* optionalSuggestions) {
std::vector<int> result;
if (!CallProviderMethod("spellCheck", text, &result))
return;
if (result.size() != 2)
return;
misspelledOffset = result[0];
misspelledLength = result[1];
}
void SpellCheckClient::checkTextOfParagraph(
const blink::WebString&,
const blink::WebString& text,
blink::WebTextCheckingTypeMask mask,
blink::WebVector<blink::WebTextCheckingResult>* results) {
}
@ -32,11 +49,17 @@ void SpellCheckClient::requestCheckingOfText(
const blink::WebVector<uint32_t>& markersInText,
const blink::WebVector<unsigned>& markerOffsets,
blink::WebTextCheckingCompletion* completionCallback) {
if (completionCallback)
completionCallback->didCancelCheckingText();
}
blink::WebString SpellCheckClient::autoCorrectWord(
const blink::WebString& misspelledWord) {
return blink::WebString();
base::string16 result;
if (!CallProviderMethod("autoCorrectWord", misspelledWord, &result))
return blink::WebString();
return result;
}
void SpellCheckClient::showSpellingUI(bool show) {
@ -50,6 +73,24 @@ void SpellCheckClient::updateSpellingUIWithMisspelledWord(
const blink::WebString& word) {
}
template<class T>
bool SpellCheckClient::CallProviderMethod(const char* method,
const blink::WebString& text,
T* result) {
v8::HandleScope handle_scope(isolate_);
v8::Handle<v8::Object> provider = provider_.NewHandle();
if (!provider->Has(mate::StringToV8(isolate_, method)))
return false;
v8::Handle<v8::Value> v8_str =
mate::ConvertToV8(isolate_, base::string16(text));
v8::Handle<v8::Value> v8_result =
node::MakeCallback(isolate_, provider, method, 1, &v8_str);
return mate::ConvertFromV8(isolate_, v8_result, result);;
}
} // namespace api
} // namespace atom

Просмотреть файл

@ -40,6 +40,12 @@ class SpellCheckClient : public blink::WebSpellCheckClient {
void updateSpellingUIWithMisspelledWord(
const blink::WebString& word) override;
template<class T>
bool CallProviderMethod(const char* method,
const blink::WebString& text,
T* result);
v8::Isolate* isolate_;
mate::ScopedPersistent<v8::Object> provider_;
DISALLOW_COPY_AND_ASSIGN(SpellCheckClient);

Просмотреть файл

@ -60,6 +60,7 @@ void WebFrame::AttachGuest(int id) {
void WebFrame::SetSpellCheckProvider(v8::Isolate* isolate,
v8::Handle<v8::Object> provider) {
spell_check_client_.reset(new SpellCheckClient(isolate, provider));
web_frame_->view()->setSpellCheckClient(spell_check_client_.get());
}
mate::ObjectTemplateBuilder WebFrame::GetObjectTemplateBuilder(