No Description

ReactWebViewManager.cpp 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "pch.h"
  2. #include "ReactWebViewManager.h"
  3. #include "NativeModules.h"
  4. #include "ReactWebView.h"
  5. #include "JSValueXaml.h"
  6. namespace winrt {
  7. using namespace Microsoft::ReactNative;
  8. using namespace Windows::Foundation;
  9. using namespace Windows::Foundation::Collections;
  10. using namespace Windows::UI;
  11. using namespace Windows::UI::Xaml;
  12. using namespace Windows::UI::Xaml::Controls;
  13. }
  14. namespace winrt::ReactNativeWebView::implementation {
  15. ReactWebViewManager::ReactWebViewManager() {}
  16. // IViewManager
  17. winrt::hstring ReactWebViewManager::Name() noexcept {
  18. return L"RCTWebView";
  19. }
  20. winrt::FrameworkElement ReactWebViewManager::CreateView() noexcept {
  21. auto view = winrt::ReactNativeWebView::ReactWebView(m_reactContext);
  22. return view;
  23. }
  24. // IViewManagerWithReactContext
  25. winrt::IReactContext ReactWebViewManager::ReactContext() noexcept {
  26. return m_reactContext;
  27. }
  28. void ReactWebViewManager::ReactContext(IReactContext reactContext) noexcept {
  29. m_reactContext = reactContext;
  30. }
  31. // IViewManagerWithNativeProperties
  32. IMapView<hstring, ViewManagerPropertyType> ReactWebViewManager::NativeProps() noexcept {
  33. auto nativeProps = winrt::single_threaded_map<hstring, ViewManagerPropertyType>();
  34. nativeProps.Insert(L"source", ViewManagerPropertyType::Map);
  35. return nativeProps.GetView();
  36. }
  37. void ReactWebViewManager::UpdateProperties(
  38. FrameworkElement const& view,
  39. IJSValueReader const& propertyMapReader) noexcept {
  40. auto control = view.try_as<winrt::UserControl>();
  41. auto content = control.Content();
  42. if (auto webView = content.try_as<winrt::WebView>()) {
  43. const JSValueObject& propertyMap = JSValueObject::ReadFrom(propertyMapReader);
  44. for (auto const& pair : propertyMap) {
  45. auto const& propertyName = pair.first;
  46. auto const& propertyValue = pair.second;
  47. if (propertyValue.IsNull()) continue;
  48. if (propertyName == "source") {
  49. auto const& srcMap = propertyValue.AsObject();
  50. if (srcMap.find("uri") != srcMap.end()) {
  51. auto uriString = srcMap.at("uri").AsString();
  52. if (uriString.length() == 0) {
  53. continue;
  54. }
  55. bool isPackagerAsset = false;
  56. if (srcMap.find("__packager_asset") != srcMap.end()) {
  57. isPackagerAsset = srcMap.at("__packager_asset").AsBoolean();
  58. }
  59. if (isPackagerAsset && uriString.find("file://") == 0) {
  60. auto bundleRootPath = winrt::to_string(ReactNativeHost().InstanceSettings().BundleRootPath());
  61. uriString.replace(0, 7, bundleRootPath.empty() ? "ms-appx-web:///Bundle/" : bundleRootPath);
  62. }
  63. webView.Navigate(winrt::Uri(to_hstring(uriString)));
  64. }
  65. else if (srcMap.find("html") != srcMap.end()) {
  66. auto htmlString = srcMap.at("html").AsString();
  67. webView.NavigateToString(to_hstring(htmlString));
  68. }
  69. }
  70. else if (propertyName == "backgroundColor") {
  71. auto color = propertyValue.To<winrt::Color>();
  72. webView.DefaultBackgroundColor(color.A==0 ? winrt::Colors::Transparent() : color);
  73. }
  74. }
  75. }
  76. }
  77. // IViewManagerWithExportedEventTypeConstants
  78. ConstantProviderDelegate ReactWebViewManager::ExportedCustomBubblingEventTypeConstants() noexcept {
  79. return nullptr;
  80. }
  81. ConstantProviderDelegate ReactWebViewManager::ExportedCustomDirectEventTypeConstants() noexcept {
  82. return [](winrt::IJSValueWriter const& constantWriter) {
  83. WriteCustomDirectEventTypeConstant(constantWriter, "LoadingStart");
  84. WriteCustomDirectEventTypeConstant(constantWriter, "LoadingFinish");
  85. WriteCustomDirectEventTypeConstant(constantWriter, "LoadingError");
  86. WriteCustomDirectEventTypeConstant(constantWriter, "Message");
  87. };
  88. }
  89. // IViewManagerWithCommands
  90. IVectorView<hstring> ReactWebViewManager::Commands() noexcept {
  91. auto commands = winrt::single_threaded_vector<hstring>();
  92. commands.Append(L"goForward");
  93. commands.Append(L"goBack");
  94. commands.Append(L"reload");
  95. commands.Append(L"stopLoading");
  96. commands.Append(L"injectJavaScript");
  97. commands.Append(L"postMessage");
  98. return commands.GetView();
  99. }
  100. void ReactWebViewManager::DispatchCommand(
  101. FrameworkElement const& view,
  102. winrt::hstring const& commandId,
  103. winrt::IJSValueReader const& commandArgsReader) noexcept {
  104. auto control = view.try_as<winrt::UserControl>();
  105. auto content = control.Content();
  106. auto commandArgs = JSValue::ReadArrayFrom(commandArgsReader);
  107. if (auto webView = content.try_as<winrt::WebView>()) {
  108. if (commandId == L"goForward") {
  109. if (webView.CanGoForward()) {
  110. webView.GoForward();
  111. }
  112. }
  113. else if (commandId == L"goBack") {
  114. if (webView.CanGoBack()) {
  115. webView.GoBack();
  116. }
  117. }
  118. else if (commandId == L"reload") {
  119. webView.Refresh();
  120. }
  121. else if (commandId == L"stopLoading") {
  122. webView.Stop();
  123. }
  124. else if (commandId == L"injectJavaScript") {
  125. webView.InvokeScriptAsync(L"eval", { winrt::to_hstring(commandArgs[0].AsString()) });
  126. } else if(commandId == L"postMessage") {
  127. if (auto reactWebView = content.try_as<ReactNativeWebView::ReactWebView>()) {
  128. reactWebView.PostMessage(winrt::to_hstring(commandArgs[0].AsString()));
  129. }
  130. }
  131. }
  132. }
  133. } // namespace winrt::ReactWebView::implementation