Нет описания

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. m_reactWebView = *winrt::make_self<ReactWebView>(m_reactContext);
  22. return m_reactWebView.GetView();
  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. if (auto webView = view.try_as<winrt::WebView>()) {
  41. const JSValueObject& propertyMap = JSValueObject::ReadFrom(propertyMapReader);
  42. for (auto const& pair : propertyMap) {
  43. auto const& propertyName = pair.first;
  44. auto const& propertyValue = pair.second;
  45. if (propertyValue.IsNull()) continue;
  46. if (propertyName == "source") {
  47. auto const& srcMap = propertyValue.AsObject();
  48. if (srcMap.find("uri") != srcMap.end()) {
  49. auto uriString = srcMap.at("uri").AsString();
  50. if (uriString.length() == 0) {
  51. continue;
  52. }
  53. bool isPackagerAsset = false;
  54. if (srcMap.find("__packager_asset") != srcMap.end()) {
  55. isPackagerAsset = srcMap.at("__packager_asset").AsBoolean();
  56. }
  57. if (isPackagerAsset && uriString.find("file://") == 0) {
  58. auto bundleRootPath = winrt::to_string(ReactNativeHost().InstanceSettings().BundleRootPath());
  59. uriString.replace(0, 7, bundleRootPath.empty() ? "ms-appx-web:///Bundle/" : bundleRootPath);
  60. }
  61. webView.Navigate(winrt::Uri(to_hstring(uriString)));
  62. }
  63. else if (srcMap.find("html") != srcMap.end()) {
  64. auto htmlString = srcMap.at("html").AsString();
  65. webView.NavigateToString(to_hstring(htmlString));
  66. }
  67. }
  68. else if (propertyName == "backgroundColor") {
  69. auto color = propertyValue.To<winrt::Color>();
  70. webView.DefaultBackgroundColor(color.A==0 ? winrt::Colors::Transparent() : color);
  71. }
  72. }
  73. }
  74. }
  75. // IViewManagerWithExportedEventTypeConstants
  76. ConstantProviderDelegate ReactWebViewManager::ExportedCustomBubblingEventTypeConstants() noexcept {
  77. return nullptr;
  78. }
  79. ConstantProviderDelegate ReactWebViewManager::ExportedCustomDirectEventTypeConstants() noexcept {
  80. return [](winrt::IJSValueWriter const& constantWriter) {
  81. WriteCustomDirectEventTypeConstant(constantWriter, "LoadingStart");
  82. WriteCustomDirectEventTypeConstant(constantWriter, "LoadingFinish");
  83. WriteCustomDirectEventTypeConstant(constantWriter, "LoadingError");
  84. WriteCustomDirectEventTypeConstant(constantWriter, "Message");
  85. };
  86. }
  87. // IViewManagerWithCommands
  88. IVectorView<hstring> ReactWebViewManager::Commands() noexcept {
  89. auto commands = winrt::single_threaded_vector<hstring>();
  90. commands.Append(L"goForward");
  91. commands.Append(L"goBack");
  92. commands.Append(L"reload");
  93. commands.Append(L"stopLoading");
  94. commands.Append(L"injectJavaScript");
  95. commands.Append(L"postMessage");
  96. return commands.GetView();
  97. }
  98. void ReactWebViewManager::DispatchCommand(
  99. FrameworkElement const& view,
  100. winrt::hstring const& commandId,
  101. winrt::IJSValueReader const& commandArgsReader) noexcept {
  102. auto commandArgs = JSValue::ReadArrayFrom(commandArgsReader);
  103. if (auto webView = view.try_as<winrt::WebView>()) {
  104. if (commandId == L"goForward") {
  105. if (webView.CanGoForward()) {
  106. webView.GoForward();
  107. }
  108. }
  109. else if (commandId == L"goBack") {
  110. if (webView.CanGoBack()) {
  111. webView.GoBack();
  112. }
  113. }
  114. else if (commandId == L"reload") {
  115. webView.Refresh();
  116. }
  117. else if (commandId == L"stopLoading") {
  118. webView.Stop();
  119. }
  120. else if (commandId == L"injectJavaScript") {
  121. webView.InvokeScriptAsync(L"eval", { winrt::to_hstring(commandArgs[0].AsString()) });
  122. } else if(commandId == L"postMessage") {
  123. m_reactWebView.PostMessage(winrt::to_hstring(commandArgs[0].AsString()));
  124. }
  125. }
  126. }
  127. } // namespace winrt::ReactWebView::implementation