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