Quellcode durchsuchen

script.notify() usage removed

Tero Paananen vor 4 Jahren
Ursprung
Commit
9c8a1e46e1

+ 22
- 37
windows/ReactNativeWebView/ReactWebView.cpp Datei anzeigen

@@ -61,13 +61,6 @@ namespace winrt::ReactNativeWebView::implementation {
61 61
                     self->OnNavigationFailed(sender, args);
62 62
                 }
63 63
             });
64
-
65
-        m_scriptNotifyRevoker = m_webView.ScriptNotify(
66
-            winrt::auto_revoke, [ref = get_weak()](auto const& sender, auto const& args) {
67
-                if (auto self = ref.get()) {
68
-                    self->OnScriptNotify(sender, args);
69
-                }
70
-            });
71 64
     }
72 65
 
73 66
     void ReactWebView::WriteWebViewNavigationEventArg(winrt::WebView const& sender, winrt::IJSValueWriter const& eventDataWriter) {
@@ -118,15 +111,11 @@ namespace winrt::ReactNativeWebView::implementation {
118 111
                 eventDataWriter.WriteObjectEnd();
119 112
             });
120 113
 
121
-        winrt::hstring windowAlert = L"window.alert = function (msg) {window.external.notify(`{\"type\":\"__alert\",\"message\":\"${msg}\"}`)};";
122 114
         if (m_messagingEnabled) {
115
+          winrt::hstring windowAlert = L"window.alert = function (msg) {__REACT_WEB_VIEW_BRIDGE.postMessage(`{\"type\":\"__alert\",\"message\":\"${msg}\"}`)};";
123 116
           winrt::hstring postMessage = L"window.ReactNativeWebView = {postMessage: function (data) {__REACT_WEB_VIEW_BRIDGE.postMessage(String(data))}};";
124 117
           webView.InvokeScriptAsync(L"eval", { windowAlert + postMessage });
125 118
         }
126
-        else {
127
-          winrt::hstring postMessage = L"window.ReactNativeWebView = {postMessage: function (data) {window.external.notify(String(data))}};";
128
-          webView.InvokeScriptAsync(L"eval", { windowAlert + postMessage });
129
-        }
130 119
     }
131 120
 
132 121
     void ReactWebView::OnNavigationFailed(winrt::IInspectable const& /*sender*/, winrt::WebViewNavigationFailedEventArgs const& args) {
@@ -144,36 +133,32 @@ namespace winrt::ReactNativeWebView::implementation {
144 133
             });
145 134
     }
146 135
 
147
-    void ReactWebView::OnScriptNotify(winrt::IInspectable const& /*sender*/, winrt::Windows::UI::Xaml::Controls::NotifyEventArgs const& args) {
148
-        winrt::JsonObject jsonObject;
149
-        if (winrt::JsonObject::TryParse(args.Value(), jsonObject) && jsonObject.HasKey(L"type")) {
150
-            auto type = jsonObject.GetNamedString(L"type");
151
-            if (type == L"__alert") {
152
-                auto dialog = winrt::MessageDialog(jsonObject.GetNamedString(L"message"));
153
-                dialog.Commands().Append(winrt::UICommand(L"OK"));
154
-                dialog.ShowAsync();
155
-                return;
156
-            }
157
-        }
158
-        
159
-        PostMessage(winrt::hstring(args.Value()));
160
-    }
161
-
162 136
     void ReactWebView::PostMessage(winrt::hstring const& message) {
137
+      winrt::JsonObject jsonObject;
138
+      if (winrt::JsonObject::TryParse(message, jsonObject) && jsonObject.HasKey(L"type")) {
139
+          auto type = jsonObject.GetNamedString(L"type");
140
+          if (type == L"__alert") {
141
+            auto dialog = winrt::MessageDialog(jsonObject.GetNamedString(L"message"));
142
+            dialog.Commands().Append(winrt::UICommand(L"OK"));
143
+            dialog.ShowAsync();
144
+            return;
145
+          }
146
+        }
147
+      
163 148
         m_reactContext.DispatchEvent(
164
-            *this,
165
-            L"topMessage",
166
-            [&](winrt::Microsoft::ReactNative::IJSValueWriter const& eventDataWriter) noexcept {
167
-                eventDataWriter.WriteObjectBegin();
168
-                {
169
-                    WriteProperty(eventDataWriter, L"data", message);
170
-                }
171
-                eventDataWriter.WriteObjectEnd();
172
-            });
149
+              *this,
150
+              L"topMessage",
151
+              [&](winrt::Microsoft::ReactNative::IJSValueWriter const& eventDataWriter) noexcept {
152
+                  eventDataWriter.WriteObjectBegin();
153
+                  {
154
+                      WriteProperty(eventDataWriter, L"data", message);
155
+                  }
156
+                  eventDataWriter.WriteObjectEnd();
157
+              });
173 158
     }
174 159
 
175 160
     void ReactWebView::SetMessagingEnabled(bool enabled) {
176
-      this->m_messagingEnabled = enabled;
161
+      m_messagingEnabled = enabled;
177 162
     }
178 163
 
179 164
 } // namespace winrt::ReactNativeWebView::implementation

+ 1
- 3
windows/ReactNativeWebView/ReactWebView.h Datei anzeigen

@@ -19,7 +19,7 @@ namespace winrt::ReactNativeWebView::implementation {
19 19
         ~ReactWebView();
20 20
 
21 21
     private:
22
-        bool m_messagingEnabled{ false };
22
+        bool m_messagingEnabled{ true };
23 23
         winrt::Windows::UI::Xaml::Controls::WebView m_webView{ nullptr };
24 24
         Microsoft::ReactNative::IReactContext m_reactContext{ nullptr };
25 25
         WebViewBridge::WebBridge m_webBridge{ nullptr };
@@ -27,14 +27,12 @@ namespace winrt::ReactNativeWebView::implementation {
27 27
         winrt::Windows::UI::Xaml::Controls::WebView::NavigationStarting_revoker m_navigationStartingRevoker{};
28 28
         winrt::Windows::UI::Xaml::Controls::WebView::NavigationCompleted_revoker m_navigationCompletedRevoker{};
29 29
         winrt::Windows::UI::Xaml::Controls::WebView::NavigationFailed_revoker m_navigationFailedRevoker{};
30
-        winrt::Windows::UI::Xaml::Controls::WebView::ScriptNotify_revoker m_scriptNotifyRevoker{};
31 30
 
32 31
         void RegisterEvents();
33 32
         void WriteWebViewNavigationEventArg(winrt::Windows::UI::Xaml::Controls::WebView const& sender, winrt::Microsoft::ReactNative::IJSValueWriter const& eventDataWriter);
34 33
         void OnNavigationStarting(winrt::Windows::UI::Xaml::Controls::WebView const& sender, winrt::Windows::UI::Xaml::Controls::WebViewNavigationStartingEventArgs const& args);
35 34
         void OnNavigationCompleted(winrt::Windows::UI::Xaml::Controls::WebView const& sender, winrt::Windows::UI::Xaml::Controls::WebViewNavigationCompletedEventArgs const& args);
36 35
         void OnNavigationFailed(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::Controls::WebViewNavigationFailedEventArgs const& args);
37
-        void OnScriptNotify(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::Controls::NotifyEventArgs const& args);
38 36
         void OnMessagePosted(hstring const& message);
39 37
     };
40 38
 } // namespace winrt::ReactNativeWebView::implementation

+ 1
- 1
windows/ReactNativeWebView/ReactWebViewManager.cpp Datei anzeigen

@@ -40,7 +40,7 @@ namespace winrt::ReactNativeWebView::implementation {
40 40
     IMapView<hstring, ViewManagerPropertyType> ReactWebViewManager::NativeProps() noexcept {
41 41
         auto nativeProps = winrt::single_threaded_map<hstring, ViewManagerPropertyType>();
42 42
         nativeProps.Insert(L"source", ViewManagerPropertyType::Map);
43
-        nativeProps.Insert(L"backgroundColor", ViewManagerPropertyType::Color); // Does this really work?
43
+        nativeProps.Insert(L"backgroundColor", ViewManagerPropertyType::Color);
44 44
         nativeProps.Insert(L"messagingEnabled", ViewManagerPropertyType::Boolean);
45 45
         return nativeProps.GetView();
46 46
     }