|
@@ -26,10 +26,13 @@ namespace winrt::ReactNativeWebView::implementation {
|
26
|
26
|
#else
|
27
|
27
|
m_webView = winrt::WebView();
|
28
|
28
|
#endif
|
29
|
|
- this->Content(m_webView);
|
30
|
29
|
RegisterEvents();
|
31
|
30
|
}
|
32
|
31
|
|
|
32
|
+ winrt::WebView ReactWebView::GetView() {
|
|
33
|
+ return m_webView;
|
|
34
|
+ }
|
|
35
|
+
|
33
|
36
|
void ReactWebView::RegisterEvents() {
|
34
|
37
|
m_navigationStartingRevoker = m_webView.NavigationStarting(
|
35
|
38
|
winrt::auto_revoke, [ref = get_weak()](auto const& sender, auto const& args) {
|
|
@@ -61,54 +64,54 @@ namespace winrt::ReactNativeWebView::implementation {
|
61
|
64
|
});
|
62
|
65
|
}
|
63
|
66
|
|
64
|
|
- void ReactWebView::WriteWebViewNavigationEventArg(winrt::WebView const& sender, winrt::IJSValueWriter const& eventDataWriter) {
|
65
|
|
- auto tag = this->GetValue(winrt::FrameworkElement::TagProperty()).as<winrt::IPropertyValue>().GetInt64();
|
66
|
|
- WriteProperty(eventDataWriter, L"canGoBack", sender.CanGoBack());
|
67
|
|
- WriteProperty(eventDataWriter, L"canGoForward", sender.CanGoForward());
|
68
|
|
- WriteProperty(eventDataWriter, L"loading", !sender.IsLoaded());
|
|
67
|
+ void ReactWebView::WriteWebViewNavigationEventArg(winrt::IJSValueWriter const& eventDataWriter) {
|
|
68
|
+ auto tag = m_webView.GetValue(winrt::FrameworkElement::TagProperty()).as<winrt::IPropertyValue>().GetInt64();
|
|
69
|
+ WriteProperty(eventDataWriter, L"canGoBack", m_webView.CanGoBack());
|
|
70
|
+ WriteProperty(eventDataWriter, L"canGoForward", m_webView.CanGoForward());
|
|
71
|
+ WriteProperty(eventDataWriter, L"loading", !m_webView.IsLoaded());
|
69
|
72
|
WriteProperty(eventDataWriter, L"target", tag);
|
70
|
|
- WriteProperty(eventDataWriter, L"title", sender.DocumentTitle());
|
71
|
|
- if (auto uri = sender.Source()) {
|
72
|
|
- WriteProperty(eventDataWriter, L"url", uri.AbsoluteCanonicalUri());
|
|
73
|
+ WriteProperty(eventDataWriter, L"title", m_webView.DocumentTitle());
|
|
74
|
+ if (auto uri = m_webView.Source()) {
|
|
75
|
+ WriteProperty(eventDataWriter, L"url", uri.AbsoluteCanonicalUri());
|
73
|
76
|
}
|
74
|
77
|
}
|
75
|
78
|
|
76
|
79
|
void ReactWebView::OnNavigationStarting(winrt::WebView const& webView, winrt::WebViewNavigationStartingEventArgs const& /*args*/) {
|
77
|
80
|
m_reactContext.DispatchEvent(
|
78
|
|
- *this,
|
|
81
|
+ webView,
|
79
|
82
|
L"topLoadingStart",
|
80
|
83
|
[&](winrt::IJSValueWriter const& eventDataWriter) noexcept {
|
81
|
84
|
eventDataWriter.WriteObjectBegin();
|
82
|
|
- WriteWebViewNavigationEventArg(webView, eventDataWriter);
|
|
85
|
+ WriteWebViewNavigationEventArg(eventDataWriter);
|
83
|
86
|
eventDataWriter.WriteObjectEnd();
|
84
|
87
|
});
|
85
|
88
|
}
|
86
|
89
|
|
87
|
90
|
void ReactWebView::OnNavigationCompleted(winrt::WebView const& webView, winrt::WebViewNavigationCompletedEventArgs const& /*args*/) {
|
88
|
91
|
m_reactContext.DispatchEvent(
|
89
|
|
- *this,
|
|
92
|
+ webView,
|
90
|
93
|
L"topLoadingFinish",
|
91
|
94
|
[&](winrt::IJSValueWriter const& eventDataWriter) noexcept {
|
92
|
95
|
eventDataWriter.WriteObjectBegin();
|
93
|
|
- WriteWebViewNavigationEventArg(webView, eventDataWriter);
|
|
96
|
+ WriteWebViewNavigationEventArg(eventDataWriter);
|
94
|
97
|
eventDataWriter.WriteObjectEnd();
|
95
|
98
|
});
|
96
|
99
|
|
97
|
100
|
winrt::hstring windowAlert = L"window.alert = function (msg) {window.external.notify(`{\"type\":\"__alert\",\"message\":\"${msg}\"}`)};";
|
98
|
101
|
winrt::hstring postMessage = L"window.ReactNativeWebView = {postMessage: function (data) {window.external.notify(String(data))}};";
|
99
|
|
- webView.InvokeScriptAsync(L"eval", { windowAlert + postMessage });
|
|
102
|
+ m_webView.InvokeScriptAsync(L"eval", { windowAlert + postMessage });
|
100
|
103
|
}
|
101
|
104
|
|
102
|
105
|
void ReactWebView::OnNavigationFailed(winrt::IInspectable const& /*sender*/, winrt::WebViewNavigationFailedEventArgs const& args) {
|
103
|
106
|
m_reactContext.DispatchEvent(
|
104
|
|
- *this,
|
|
107
|
+ m_webView,
|
105
|
108
|
L"topLoadingError",
|
106
|
109
|
[&](winrt::IJSValueWriter const& eventDataWriter) noexcept {
|
107
|
110
|
auto httpCode = static_cast<int32_t>(args.WebErrorStatus());
|
108
|
111
|
eventDataWriter.WriteObjectBegin();
|
109
|
112
|
{
|
110
|
113
|
WriteProperty(eventDataWriter, L"code", httpCode);
|
111
|
|
- WriteWebViewNavigationEventArg(m_webView, eventDataWriter);
|
|
114
|
+ WriteWebViewNavigationEventArg(eventDataWriter);
|
112
|
115
|
}
|
113
|
116
|
eventDataWriter.WriteObjectEnd();
|
114
|
117
|
});
|
|
@@ -131,7 +134,7 @@ namespace winrt::ReactNativeWebView::implementation {
|
131
|
134
|
|
132
|
135
|
void ReactWebView::PostMessage(winrt::hstring const& message) {
|
133
|
136
|
m_reactContext.DispatchEvent(
|
134
|
|
- *this,
|
|
137
|
+ m_webView,
|
135
|
138
|
L"topMessage",
|
136
|
139
|
[&](winrt::Microsoft::ReactNative::IJSValueWriter const& eventDataWriter) noexcept {
|
137
|
140
|
eventDataWriter.WriteObjectBegin();
|