|
@@ -20,7 +20,19 @@ namespace winrt::ReactNativeWebView::implementation {
|
20
|
20
|
}
|
21
|
21
|
|
22
|
22
|
winrt::FrameworkElement ReactWebViewManager::CreateView() noexcept {
|
23
|
|
- return winrt::WebView();
|
|
23
|
+ auto webView = winrt::WebView();
|
|
24
|
+ m_webView = winrt::make_weak(webView);
|
|
25
|
+ RegisterEvents();
|
|
26
|
+ return webView;
|
|
27
|
+ }
|
|
28
|
+
|
|
29
|
+ void ReactWebViewManager::RegisterEvents() {
|
|
30
|
+ if (auto webView = m_webView.get()) {
|
|
31
|
+ m_navigationCompletedRevoker = webView.NavigationCompleted(
|
|
32
|
+ winrt::auto_revoke, [=](auto const& sender, auto const& args) {
|
|
33
|
+ OnNavigationCompleted(sender, args);
|
|
34
|
+ });
|
|
35
|
+ }
|
24
|
36
|
}
|
25
|
37
|
|
26
|
38
|
// IViewManagerWithReactContext
|
|
@@ -48,44 +60,49 @@ namespace winrt::ReactNativeWebView::implementation {
|
48
|
60
|
for (auto const& pair : propertyMap) {
|
49
|
61
|
auto const& propertyName = pair.first;
|
50
|
62
|
auto const& propertyValue = pair.second;
|
|
63
|
+
|
51
|
64
|
if (propertyName == "source") {
|
52
|
65
|
if (!propertyValue.IsNull()) {
|
53
|
66
|
auto const& srcMap = propertyValue.Object();
|
54
|
|
- auto uriString = srcMap.at("uri").String();
|
55
|
|
- auto const isPackagerAsset = srcMap.at("__packager_asset").Boolean();
|
56
|
67
|
|
|
68
|
+ auto uriString = srcMap.at("uri").String();
|
57
|
69
|
// non-uri sources not yet supported
|
58
|
70
|
if (uriString.length() == 0) {
|
59
|
71
|
continue;
|
60
|
72
|
}
|
61
|
73
|
|
|
74
|
+ bool isPackagerAsset = false;
|
|
75
|
+ if (srcMap.find("__packager_asset") != srcMap.end()) {
|
|
76
|
+ isPackagerAsset = srcMap.at("__packager_asset").Boolean();
|
|
77
|
+ }
|
|
78
|
+
|
62
|
79
|
if (isPackagerAsset && uriString.find("assets") == 0) {
|
63
|
80
|
uriString.replace(0, 6, "ms-appx://");
|
64
|
81
|
}
|
65
|
|
- auto const uri = winrt::Uri(to_hstring(uriString));
|
66
|
82
|
|
67
|
|
- SetSource(webView, uri);
|
|
83
|
+ SetSource(winrt::Uri(to_hstring(uriString)));
|
68
|
84
|
}
|
69
|
85
|
}
|
70
|
86
|
}
|
71
|
87
|
}
|
72
|
88
|
}
|
73
|
89
|
|
74
|
|
- void ReactWebViewManager::SetSource(winrt::WebView const& webView, winrt::Uri const& uri) {
|
75
|
|
- auto const tag = webView.GetValue(winrt::FrameworkElement::TagProperty()).as<winrt::IPropertyValue>().GetInt64();
|
76
|
|
-
|
77
|
|
- m_reactContext.DispatchEvent(
|
78
|
|
- webView,
|
79
|
|
- L"topLoadStart",
|
80
|
|
- [tag](winrt::IJSValueWriter const& eventDataWriter) noexcept {
|
81
|
|
- eventDataWriter.WriteObjectBegin();
|
82
|
|
- {
|
83
|
|
- WriteProperty(eventDataWriter, L"target", tag);
|
84
|
|
- }
|
85
|
|
- eventDataWriter.WriteObjectEnd();
|
86
|
|
- });
|
|
90
|
+ void ReactWebViewManager::SetSource(winrt::Uri const& uri) {
|
|
91
|
+ if (auto webView = m_webView.get()) {
|
|
92
|
+ auto tag = webView.GetValue(winrt::FrameworkElement::TagProperty()).as<winrt::IPropertyValue>().GetInt64();
|
|
93
|
+ m_reactContext.DispatchEvent(
|
|
94
|
+ webView,
|
|
95
|
+ L"topLoadingStart",
|
|
96
|
+ [tag](winrt::IJSValueWriter const& eventDataWriter) noexcept {
|
|
97
|
+ eventDataWriter.WriteObjectBegin();
|
|
98
|
+ {
|
|
99
|
+ WriteProperty(eventDataWriter, L"target", tag);
|
|
100
|
+ }
|
|
101
|
+ eventDataWriter.WriteObjectEnd();
|
|
102
|
+ });
|
87
|
103
|
|
88
|
|
- webView.Navigate(uri);
|
|
104
|
+ webView.Navigate(uri);
|
|
105
|
+ }
|
89
|
106
|
}
|
90
|
107
|
|
91
|
108
|
// IViewManagerWithExportedEventTypeConstants
|
|
@@ -95,10 +112,57 @@ namespace winrt::ReactNativeWebView::implementation {
|
95
|
112
|
|
96
|
113
|
ConstantProvider ReactWebViewManager::ExportedCustomDirectEventTypeConstants() noexcept {
|
97
|
114
|
return [](winrt::Microsoft::ReactNative::IJSValueWriter const& constantWriter) {
|
98
|
|
- WriteCustomDirectEventTypeConstant(constantWriter, "onLoadStart");
|
|
115
|
+ WriteCustomDirectEventTypeConstant(constantWriter, "onLoadingStart");
|
99
|
116
|
WriteCustomDirectEventTypeConstant(constantWriter, "onLoad");
|
100
|
|
- WriteCustomDirectEventTypeConstant(constantWriter, "onLoadEnd");
|
|
117
|
+ WriteCustomDirectEventTypeConstant(constantWriter, "onLoadingFinish");
|
101
|
118
|
};
|
102
|
119
|
}
|
103
|
120
|
|
|
121
|
+ // IViewManagerWithCommands
|
|
122
|
+ IMapView<hstring, int64_t> ReactWebViewManager::Commands() noexcept {
|
|
123
|
+ auto commands = winrt::single_threaded_map<hstring, int64_t>();
|
|
124
|
+ commands.Insert(L"goForward", 0);
|
|
125
|
+ commands.Insert(L"goBack", 1);
|
|
126
|
+ commands.Insert(L"reload", 2);
|
|
127
|
+ return commands.GetView();
|
|
128
|
+ }
|
|
129
|
+
|
|
130
|
+ void ReactWebViewManager::DispatchCommand(
|
|
131
|
+ FrameworkElement const& view,
|
|
132
|
+ int64_t commandId,
|
|
133
|
+ winrt::Microsoft::ReactNative::IJSValueReader const& commandArgsReader) noexcept {
|
|
134
|
+ if (auto webView = view.try_as<winrt::WebView>()) {
|
|
135
|
+ switch (commandId) {
|
|
136
|
+ case 0: // goForward
|
|
137
|
+ webView.GoForward();
|
|
138
|
+ break;
|
|
139
|
+ case 1: // goBack
|
|
140
|
+ webView.GoBack();
|
|
141
|
+ break;
|
|
142
|
+ case 2: // reload
|
|
143
|
+ webView.Refresh();
|
|
144
|
+ break;
|
|
145
|
+ }
|
|
146
|
+ }
|
|
147
|
+ }
|
|
148
|
+
|
|
149
|
+ void ReactWebViewManager::OnNavigationCompleted(winrt::WebView const& webView, winrt::WebViewNavigationCompletedEventArgs const& args) {
|
|
150
|
+ m_reactContext.DispatchEvent(
|
|
151
|
+ webView,
|
|
152
|
+ L"topLoadingFinish",
|
|
153
|
+ [webView, args](winrt::IJSValueWriter const& eventDataWriter) noexcept {
|
|
154
|
+ eventDataWriter.WriteObjectBegin();
|
|
155
|
+ {
|
|
156
|
+ auto tag = webView.GetValue(winrt::FrameworkElement::TagProperty()).as<winrt::IPropertyValue>().GetInt64();
|
|
157
|
+ WriteProperty(eventDataWriter, L"canGoBack", webView.CanGoBack());
|
|
158
|
+ WriteProperty(eventDataWriter, L"canGoForward", webView.CanGoForward());
|
|
159
|
+ WriteProperty(eventDataWriter, L"loading", false);
|
|
160
|
+ WriteProperty(eventDataWriter, L"target", tag);
|
|
161
|
+ WriteProperty(eventDataWriter, L"title", webView.DocumentTitle());
|
|
162
|
+ WriteProperty(eventDataWriter, L"url", args.Uri().ToString());
|
|
163
|
+ }
|
|
164
|
+ eventDataWriter.WriteObjectEnd();
|
|
165
|
+ });
|
|
166
|
+ }
|
|
167
|
+
|
104
|
168
|
} // namespace winrt::ReactWebView::implementation
|