123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- #include "pch.h"
- #include "ReactWebViewManager.h"
- #include "NativeModules.h"
-
- namespace winrt {
- using namespace Microsoft::ReactNative;
- using namespace Windows::Foundation;
- using namespace Windows::Foundation::Collections;
- using namespace Windows::UI::Xaml;
- using namespace Windows::UI::Xaml::Controls;
- }
-
- namespace winrt::ReactNativeWebView::implementation {
-
- ReactWebViewManager::ReactWebViewManager() {}
-
- // IViewManager
- winrt::hstring ReactWebViewManager::Name() noexcept {
- return L"RCTWebView";
- }
-
- winrt::FrameworkElement ReactWebViewManager::CreateView() noexcept {
- return winrt::WebView();
- }
-
- // IViewManagerWithReactContext
- winrt::IReactContext ReactWebViewManager::ReactContext() noexcept {
- return m_reactContext;
- }
-
- void ReactWebViewManager::ReactContext(IReactContext reactContext) noexcept {
- m_reactContext = reactContext;
- }
-
- // IViewManagerWithNativeProperties
- IMapView<hstring, ViewManagerPropertyType> ReactWebViewManager::NativeProps() noexcept {
- auto nativeProps = winrt::single_threaded_map<hstring, ViewManagerPropertyType>();
- nativeProps.Insert(L"source", ViewManagerPropertyType::Map);
- return nativeProps.GetView();
- }
-
- void ReactWebViewManager::UpdateProperties(
- FrameworkElement const& view,
- IJSValueReader const& propertyMapReader) noexcept {
- if (auto webView = view.try_as<winrt::WebView>()) {
- const JSValueObject& propertyMap = JSValue::ReadObjectFrom(propertyMapReader);
-
- for (auto const& pair : propertyMap) {
- auto const& propertyName = pair.first;
- auto const& propertyValue = pair.second;
- if (propertyName == "source") {
- if (!propertyValue.IsNull()) {
- auto const& srcMap = propertyValue.Object();
- auto uriString = srcMap.at("uri").String();
- auto const isPackagerAsset = srcMap.at("__packager_asset").Boolean();
-
- // non-uri sources not yet supported
- if (uriString.length() == 0) {
- continue;
- }
-
- if (isPackagerAsset && uriString.find("assets") == 0) {
- uriString.replace(0, 6, "ms-appx://");
- }
- auto const uri = winrt::Uri(to_hstring(uriString));
-
- SetSource(webView, uri);
- }
- }
- }
- }
- }
-
- void ReactWebViewManager::SetSource(winrt::WebView const& webView, winrt::Uri const& uri) {
- auto const tag = webView.GetValue(winrt::FrameworkElement::TagProperty()).as<winrt::IPropertyValue>().GetInt64();
-
- m_reactContext.DispatchEvent(
- webView,
- L"topLoadStart",
- [tag](winrt::IJSValueWriter const& eventDataWriter) noexcept {
- eventDataWriter.WriteObjectBegin();
- {
- WriteProperty(eventDataWriter, L"target", tag);
- }
- eventDataWriter.WriteObjectEnd();
- });
-
- webView.Navigate(uri);
- }
-
- // IViewManagerWithExportedEventTypeConstants
- ConstantProvider ReactWebViewManager::ExportedCustomBubblingEventTypeConstants() noexcept {
- return nullptr;
- }
-
- ConstantProvider ReactWebViewManager::ExportedCustomDirectEventTypeConstants() noexcept {
- return [](winrt::Microsoft::ReactNative::IJSValueWriter const& constantWriter) {
- WriteCustomDirectEventTypeConstant(constantWriter, "onLoadStart");
- WriteCustomDirectEventTypeConstant(constantWriter, "onLoad");
- WriteCustomDirectEventTypeConstant(constantWriter, "onLoadEnd");
- };
- }
-
- } // namespace winrt::ReactWebView::implementation
|