Procházet zdrojové kódy

feat(android): add clearHistory, clearCache and clearFormData (#450)

* add clearHistory, clearCache and clearFormData android webview api.

* remove pointless `async`

* add docs for new android webview methods

* Update Reference.md

* update commands types

* add more strict type for RNCWebViewUIManager `Commands` property
Stanislav Shakirov před 5 roky
rodič
revize
4a4f4a2c45

+ 30
- 12
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java Zobrazit soubor

@@ -115,6 +115,12 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
115 115
   public static final int COMMAND_INJECT_JAVASCRIPT = 6;
116 116
   public static final int COMMAND_LOAD_URL = 7;
117 117
   public static final int COMMAND_FOCUS = 8;
118
+
119
+  // android commands
120
+  public static final int COMMAND_CLEAR_FORM_DATA = 1000;
121
+  public static final int COMMAND_CLEAR_CACHE = 1001;
122
+  public static final int COMMAND_CLEAR_HISTORY = 1002;
123
+
118 124
   protected static final String REACT_CLASS = "RNCWebView";
119 125
   protected static final String HTML_ENCODING = "UTF-8";
120 126
   protected static final String HTML_MIME_TYPE = "text/html";
@@ -266,7 +272,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
266 272
         break;
267 273
       case "LOAD_CACHE_ELSE_NETWORK":
268 274
         cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK;
269
-        break;  
275
+        break;
270 276
       case "LOAD_NO_CACHE":
271 277
         cacheMode = WebSettings.LOAD_NO_CACHE;
272 278
         break;
@@ -545,17 +551,19 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
545 551
   @Override
546 552
   public @Nullable
547 553
   Map<String, Integer> getCommandsMap() {
548
-    Map map = MapBuilder.of(
549
-      "goBack", COMMAND_GO_BACK,
550
-      "goForward", COMMAND_GO_FORWARD,
551
-      "reload", COMMAND_RELOAD,
552
-      "stopLoading", COMMAND_STOP_LOADING,
553
-      "postMessage", COMMAND_POST_MESSAGE,
554
-      "injectJavaScript", COMMAND_INJECT_JAVASCRIPT,
555
-      "loadUrl", COMMAND_LOAD_URL
556
-    );
557
-    map.put("requestFocus", COMMAND_FOCUS);
558
-    return map;
554
+    return MapBuilder.<String, Integer>builder()
555
+      .put("goBack", COMMAND_GO_BACK)
556
+      .put("goForward", COMMAND_GO_FORWARD)
557
+      .put("reload", COMMAND_RELOAD)
558
+      .put("stopLoading", COMMAND_STOP_LOADING)
559
+      .put("postMessage", COMMAND_POST_MESSAGE)
560
+      .put("injectJavaScript", COMMAND_INJECT_JAVASCRIPT)
561
+      .put("loadUrl", COMMAND_LOAD_URL)
562
+      .put("requestFocus", COMMAND_FOCUS)
563
+      .put("clearFormData", COMMAND_CLEAR_FORM_DATA)
564
+      .put("clearCache", COMMAND_CLEAR_CACHE)
565
+      .put("clearHistory", COMMAND_CLEAR_HISTORY)
566
+      .build();
559 567
   }
560 568
 
561 569
   @Override
@@ -606,6 +614,16 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
606 614
       case COMMAND_FOCUS:
607 615
         root.requestFocus();
608 616
         break;
617
+      case COMMAND_CLEAR_FORM_DATA:
618
+        root.clearFormData();
619
+        break;
620
+      case COMMAND_CLEAR_CACHE:
621
+        boolean includeDiskFiles = args != null && args.getBoolean(0);
622
+        root.clearCache(includeDiskFiles);
623
+        break;
624
+      case COMMAND_CLEAR_HISTORY:
625
+        root.clearHistory();
626
+        break;
609 627
     }
610 628
   }
611 629
 

+ 29
- 0
docs/Reference.md Zobrazit soubor

@@ -70,6 +70,9 @@ This document lays out the current public properties and methods for the React N
70 70
 - [`reload`](Reference.md#reload)
71 71
 - [`stopLoading`](Reference.md#stoploading)
72 72
 - [`injectJavaScript`](Reference.md#injectjavascriptstr)
73
+- [`clearFormData`](Reference.md#clearFormData)
74
+- [`clearCache`](Reference.md#clearCache)
75
+- [`clearHistory`](Reference.md#clearHistory)
73 76
 
74 77
 ---
75 78
 
@@ -1108,6 +1111,32 @@ Executes the JavaScript string.
1108 1111
 
1109 1112
 To learn more, read the [Communicating between JS and Native](Guide.md#communicating-between-js-and-native) guide.
1110 1113
 
1114
+### `clearFormData()`
1115
+(android only)
1116
+
1117
+```javascript
1118
+clearFormData();
1119
+```
1120
+Removes the autocomplete popup from the currently focused form field, if present. [developer.android.com reference](https://developer.android.com/reference/android/webkit/WebView.html#clearFormData())
1121
+
1122
+
1123
+### `clearCache(bool)`
1124
+(android only)
1125
+```javascript
1126
+clearCache(true);
1127
+```
1128
+
1129
+Clears the resource cache. Note that the cache is per-application, so this will clear the cache for all WebViews used. [developer.android.com reference](https://developer.android.com/reference/android/webkit/WebView.html#clearCache(boolean))
1130
+
1131
+
1132
+### `clearHistory()`
1133
+(android only)
1134
+```javascript
1135
+clearHistory();
1136
+```
1137
+
1138
+Tells this WebView to clear its internal back/forward list. [developer.android.com reference](https://developer.android.com/reference/android/webkit/WebView.html#clearHistory())
1139
+
1111 1140
 ## Other Docs
1112 1141
 
1113 1142
 Also check out our [Getting Started Guide](Getting-Started.md) and [In-Depth Guide](Guide.md).

+ 26
- 2
src/WebView.android.tsx Zobrazit soubor

@@ -27,12 +27,12 @@ import {
27 27
   AndroidWebViewProps,
28 28
   NativeWebViewAndroid,
29 29
   State,
30
-  RNCWebViewUIManager,
30
+  RNCWebViewUIManagerAndroid,
31 31
 } from './WebViewTypes';
32 32
 
33 33
 import styles from './WebView.styles';
34 34
 
35
-const UIManager = NotTypedUIManager as RNCWebViewUIManager;
35
+const UIManager = NotTypedUIManager as RNCWebViewUIManagerAndroid;
36 36
 
37 37
 const RNCWebView = requireNativeComponent(
38 38
   'RNCWebView',
@@ -123,6 +123,30 @@ class WebView extends React.Component<AndroidWebViewProps, State> {
123 123
     );
124 124
   };
125 125
 
126
+  clearFormData = () => {
127
+    UIManager.dispatchViewManagerCommand(
128
+       this.getWebViewHandle(),
129
+       this.getCommands().clearFormData,
130
+        undefined,
131
+    );
132
+  }
133
+
134
+  clearCache = (includeDiskFiles: boolean) => {
135
+    UIManager.dispatchViewManagerCommand(
136
+       this.getWebViewHandle(),
137
+       this.getCommands().clearCache,
138
+       [includeDiskFiles],
139
+    );
140
+  };
141
+
142
+  clearHistory = () => {
143
+    UIManager.dispatchViewManagerCommand(
144
+       this.getWebViewHandle(),
145
+       this.getCommands().clearHistory,
146
+        undefined,
147
+    );
148
+  };
149
+
126 150
   /**
127 151
    * Injects a javascript string into the referenced WebView. Deliberately does not
128 152
    * return a response because using eval() to return a response breaks this method

+ 2
- 2
src/WebView.ios.tsx Zobrazit soubor

@@ -28,12 +28,12 @@ import {
28 28
   NativeWebViewIOS,
29 29
   ViewManager,
30 30
   State,
31
-  RNCWebViewUIManager,
31
+  RNCWebViewUIManagerIOS,
32 32
 } from './WebViewTypes';
33 33
 
34 34
 import styles from './WebView.styles';
35 35
 
36
-const UIManager = NotTypedUIManager as RNCWebViewUIManager;
36
+const UIManager = NotTypedUIManager as RNCWebViewUIManagerIOS;
37 37
 
38 38
 const { resolveAssetSource } = Image;
39 39
 const processDecelerationRate = (

+ 17
- 8
src/WebViewTypes.ts Zobrazit soubor

@@ -14,14 +14,23 @@ import {
14 14
 
15 15
 type WebViewCommands = 'goForward' | 'goBack' | 'reload' | 'stopLoading' | 'postMessage' | 'injectJavaScript' | 'loadUrl' | 'requestFocus';
16 16
 
17
-export interface RNCWebViewUIManager extends UIManagerStatic {
17
+type AndroidWebViewCommands = 'clearHistory' | 'clearCache' | 'clearFormData';
18
+
19
+
20
+
21
+interface RNCWebViewUIManager<Commands extends string> extends UIManagerStatic {
18 22
   getViewManagerConfig: (
19
-    name: string,
23
+      name: string,
20 24
   ) => {
21
-    Commands: { [key in WebViewCommands]: number };
25
+    Commands: {[key in Commands]: number};
22 26
   };
23 27
 }
24 28
 
29
+export type RNCWebViewUIManagerAndroid = RNCWebViewUIManager<WebViewCommands | AndroidWebViewCommands>
30
+export type RNCWebViewUIManagerIOS = RNCWebViewUIManager<WebViewCommands>
31
+
32
+
33
+
25 34
 type WebViewState = 'IDLE' | 'LOADING' | 'ERROR';
26 35
 
27 36
 interface BaseState {
@@ -457,7 +466,7 @@ export interface AndroidWebViewProps extends WebViewSharedProps {
457 466
   /**
458 467
    * https://developer.android.com/reference/android/webkit/WebSettings.html#setCacheMode(int)
459 468
    * Set the cacheMode. Possible values are:
460
-   * 
469
+   *
461 470
    * - `'LOAD_DEFAULT'` (default)
462 471
    * - `'LOAD_CACHE_ELSE_NETWORK'`
463 472
    * - `'LOAD_NO_CACHE'`
@@ -492,15 +501,15 @@ export interface AndroidWebViewProps extends WebViewSharedProps {
492 501
    */
493 502
   geolocationEnabled?: boolean;
494 503
 
495
-  
504
+
496 505
   /**
497
-   * Boolean that sets whether JavaScript running in the context of a file 
498
-   * scheme URL should be allowed to access content from other file scheme URLs. 
506
+   * Boolean that sets whether JavaScript running in the context of a file
507
+   * scheme URL should be allowed to access content from other file scheme URLs.
499 508
    * Including accessing content from other file scheme URLs
500 509
    * @platform android
501 510
    */
502 511
   allowFileAccessFromFileURLs?: boolean;
503
-  
512
+
504 513
   /**
505 514
    * Boolean that sets whether JavaScript running in the context of a file
506 515
    * scheme URL should be allowed to access content from any origin.