]> khamidou — GitHub mirrors - electron.git/commitdiff
Call JavaScript to get spell check results
authorCheng Zhao <zcbenz@gmail.com>
Fri, 19 Dec 2014 00:44:38 +0000 (16:44 -0800)
committerCheng Zhao <zcbenz@gmail.com>
Fri, 19 Dec 2014 00:44:38 +0000 (16:44 -0800)
atom/renderer/api/atom_api_spell_check_client.cc
atom/renderer/api/atom_api_spell_check_client.h
atom/renderer/api/atom_api_web_frame.cc

index aa069523ccf59c42cf6d2a50e0c01c33d04142f5..fc32e071bb1cf5ed29637ac609e54ed78a428904 100644 (file)
@@ -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
index d662f79ce70342e98c915a6e5036340c03c6500a..a6d05f86453a1d08ccf704265575e137cfa02996 100644 (file)
@@ -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);
index 36f13c7976e3e225031f50aa22a8eca0066026f8..e1b016c5b5b557b9b7cd4aaefa91b6b4fd17fccd 100644 (file)
@@ -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(