浏览代码

fix(windows): Fix windows local asset path (#1335 by @kaiguo)

[skip ci]
Kai Guo 4 年前
父节点
当前提交
20a3f90c0f
没有帐户链接到提交者的电子邮件
共有 6 个文件被更改,包括 49 次插入43 次删除
  1. 1
    1
      docs/Guide.md
  2. 14
    0
      example/App.tsx
  3. 9
    0
      example/assets/test.html
  4. 16
    0
      example/examples/LocalPageLoad.tsx
  5. 7
    8
      windows/ReactNativeWebView/ReactWebViewManager.cpp
  6. 2
    34
      yarn.lock

+ 1
- 1
docs/Guide.md 查看文件

@@ -55,7 +55,7 @@ class MyWeb extends Component {
55 55
 
56 56
 ### Loading local HTML files
57 57
 
58
-Sometimes you would have bundled an HTML file along with the app and would like to load the HTML asset into your WebView. To do this on iOS, you can just import the html file like any other asset as shown below.
58
+Sometimes you would have bundled an HTML file along with the app and would like to load the HTML asset into your WebView. To do this on iOS and Windows, you can just import the html file like any other asset as shown below.
59 59
 
60 60
 ```js
61 61
 import React, { Component } from 'react';

+ 14
- 0
example/App.tsx 查看文件

@@ -16,6 +16,7 @@ import Background from './examples/Background';
16 16
 import Downloads from './examples/Downloads';
17 17
 import Uploads from './examples/Uploads';
18 18
 import Injection from './examples/Injection';
19
+import LocalPageLoad from './examples/LocalPageLoad';
19 20
 
20 21
 const TESTS = {
21 22
   Alerts: {
@@ -66,6 +67,14 @@ const TESTS = {
66 67
       return <Injection />;
67 68
     },
68 69
   },
70
+  PageLoad: {
71
+    title: 'LocalPageLoad',
72
+    testId: 'LocalPageLoad',
73
+    description: 'Local Page load test',
74
+    render() {
75
+      return <LocalPageLoad />;
76
+    },
77
+  },
69 78
 };
70 79
 
71 80
 type Props = {};
@@ -124,6 +133,11 @@ export default class App extends Component<Props, State> {
124 133
             title="Injection"
125 134
             onPress={() => this._changeTest('Injection')}
126 135
           />
136
+          <Button
137
+            testID="testType_pageLoad"
138
+            title="LocalPageLoad"
139
+            onPress={() => this._changeTest('PageLoad')}
140
+          />
127 141
           {Platform.OS == "ios" && <Button
128 142
             testID="testType_downloads"
129 143
             title="Downloads"

+ 9
- 0
example/assets/test.html 查看文件

@@ -0,0 +1,9 @@
1
+<!doctype html>
2
+<html>
3
+    <head>
4
+        <title>Test</title>
5
+    </head>
6
+    <body>
7
+        <h2>Local page test</h2>
8
+    </body>
9
+</html>

+ 16
- 0
example/examples/LocalPageLoad.tsx 查看文件

@@ -0,0 +1,16 @@
1
+import React, {Component} from 'react';
2
+import {View, Text, Alert, TextInput, Button} from 'react-native';
3
+import WebView from 'react-native-webview';
4
+const localHtmlFile = require('../assets/test.html');
5
+
6
+export default class LocalPageLoad extends Component<Props, State> {
7
+    render() {
8
+      return (
9
+        <View>
10
+            <View style={{ width: '100%', height: '100%' }}>
11
+                <WebView source={localHtmlFile}/>
12
+          </View>
13
+        </View>
14
+      );
15
+    }
16
+  }

+ 7
- 8
windows/ReactNativeWebView/ReactWebViewManager.cpp 查看文件

@@ -58,7 +58,6 @@ namespace winrt::ReactNativeWebView::implementation {
58 58
                     auto const& srcMap = propertyValue.AsObject();
59 59
                     if (srcMap.find("uri") != srcMap.end()) {
60 60
                         auto uriString = srcMap.at("uri").AsString();
61
-                        // non-uri sources not yet supported
62 61
                         if (uriString.length() == 0) {
63 62
                             continue;
64 63
                         }
@@ -67,9 +66,9 @@ namespace winrt::ReactNativeWebView::implementation {
67 66
                         if (srcMap.find("__packager_asset") != srcMap.end()) {
68 67
                             isPackagerAsset = srcMap.at("__packager_asset").AsBoolean();
69 68
                         }
70
-
71
-                        if (isPackagerAsset && uriString.find("assets") == 0) {
72
-                            uriString.replace(0, 6, "ms-appx://");
69
+                        if (isPackagerAsset && uriString.find("file://") == 0) {
70
+                            auto bundleRootPath = winrt::to_string(ReactNativeHost().InstanceSettings().BundleRootPath());
71
+                            uriString.replace(0, 7, bundleRootPath.empty() ? "ms-appx-web:///Bundle/" : bundleRootPath);
73 72
                         }
74 73
 
75 74
                         webView.Navigate(winrt::Uri(to_hstring(uriString)));
@@ -94,10 +93,10 @@ namespace winrt::ReactNativeWebView::implementation {
94 93
 
95 94
     ConstantProviderDelegate ReactWebViewManager::ExportedCustomDirectEventTypeConstants() noexcept {
96 95
         return [](winrt::IJSValueWriter const& constantWriter) {
97
-            WriteCustomDirectEventTypeConstant(constantWriter, "onLoadingStart");
98
-            WriteCustomDirectEventTypeConstant(constantWriter, "onLoadingFinish");
99
-            WriteCustomDirectEventTypeConstant(constantWriter, "onLoadingError");
100
-            WriteCustomDirectEventTypeConstant(constantWriter, "onMessage");
96
+            WriteCustomDirectEventTypeConstant(constantWriter, "LoadingStart");
97
+            WriteCustomDirectEventTypeConstant(constantWriter, "LoadingFinish");
98
+            WriteCustomDirectEventTypeConstant(constantWriter, "LoadingError");
99
+            WriteCustomDirectEventTypeConstant(constantWriter, "Message");
101 100
         };
102 101
     }
103 102
 

+ 2
- 34
yarn.lock 查看文件

@@ -5233,7 +5233,7 @@ debug@^3.1.0, debug@^3.2.6:
5233 5233
   dependencies:
5234 5234
     ms "^2.1.1"
5235 5235
 
5236
-debuglog@*, debuglog@^1.0.1:
5236
+debuglog@^1.0.1:
5237 5237
   version "1.0.1"
5238 5238
   resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
5239 5239
   integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
@@ -7225,7 +7225,7 @@ import-local@^2.0.0:
7225 7225
     pkg-dir "^3.0.0"
7226 7226
     resolve-cwd "^2.0.0"
7227 7227
 
7228
-imurmurhash@*, imurmurhash@^0.1.4:
7228
+imurmurhash@^0.1.4:
7229 7229
   version "0.1.4"
7230 7230
   resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
7231 7231
   integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
@@ -8733,11 +8733,6 @@ lockfile@^1.0.4:
8733 8733
   dependencies:
8734 8734
     signal-exit "^3.0.2"
8735 8735
 
8736
-lodash._baseindexof@*:
8737
-  version "3.1.0"
8738
-  resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
8739
-  integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=
8740
-
8741 8736
 lodash._baseuniq@~4.6.0:
8742 8737
   version "4.6.0"
8743 8738
   resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
@@ -8746,33 +8741,11 @@ lodash._baseuniq@~4.6.0:
8746 8741
     lodash._createset "~4.0.0"
8747 8742
     lodash._root "~3.0.0"
8748 8743
 
8749
-lodash._bindcallback@*:
8750
-  version "3.0.1"
8751
-  resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
8752
-  integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=
8753
-
8754
-lodash._cacheindexof@*:
8755
-  version "3.0.2"
8756
-  resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
8757
-  integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=
8758
-
8759
-lodash._createcache@*:
8760
-  version "3.1.2"
8761
-  resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
8762
-  integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
8763
-  dependencies:
8764
-    lodash._getnative "^3.0.0"
8765
-
8766 8744
 lodash._createset@~4.0.0:
8767 8745
   version "4.0.3"
8768 8746
   resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
8769 8747
   integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=
8770 8748
 
8771
-lodash._getnative@*, lodash._getnative@^3.0.0:
8772
-  version "3.9.1"
8773
-  resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
8774
-  integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=
8775
-
8776 8749
 lodash._root@~3.0.0:
8777 8750
   version "3.0.1"
8778 8751
   resolved "https://registry.yarnpkg.com/lodash._root/-/lodash._root-3.0.1.tgz#fba1c4524c19ee9a5f8136b4609f017cf4ded692"
@@ -8843,11 +8816,6 @@ lodash.merge@^4.6.1:
8843 8816
   resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
8844 8817
   integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==
8845 8818
 
8846
-lodash.restparam@*:
8847
-  version "3.6.1"
8848
-  resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
8849
-  integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=
8850
-
8851 8819
 lodash.set@^4.3.2:
8852 8820
   version "4.3.2"
8853 8821
   resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"