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
This commit is contained in:
Stanislav Shakirov
2019-11-12 10:09:16 +01:00
committed by Thibault Malbranche
parent 5d6128909c
commit 4a4f4a2c45
5 changed files with 104 additions and 24 deletions
@@ -115,6 +115,12 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
public static final int COMMAND_INJECT_JAVASCRIPT = 6;
public static final int COMMAND_LOAD_URL = 7;
public static final int COMMAND_FOCUS = 8;
// android commands
public static final int COMMAND_CLEAR_FORM_DATA = 1000;
public static final int COMMAND_CLEAR_CACHE = 1001;
public static final int COMMAND_CLEAR_HISTORY = 1002;
protected static final String REACT_CLASS = "RNCWebView";
protected static final String HTML_ENCODING = "UTF-8";
protected static final String HTML_MIME_TYPE = "text/html";
@@ -266,7 +272,7 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
break;
case "LOAD_CACHE_ELSE_NETWORK":
cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK;
break;
break;
case "LOAD_NO_CACHE":
cacheMode = WebSettings.LOAD_NO_CACHE;
break;
@@ -545,17 +551,19 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
@Override
public @Nullable
Map<String, Integer> getCommandsMap() {
Map map = MapBuilder.of(
"goBack", COMMAND_GO_BACK,
"goForward", COMMAND_GO_FORWARD,
"reload", COMMAND_RELOAD,
"stopLoading", COMMAND_STOP_LOADING,
"postMessage", COMMAND_POST_MESSAGE,
"injectJavaScript", COMMAND_INJECT_JAVASCRIPT,
"loadUrl", COMMAND_LOAD_URL
);
map.put("requestFocus", COMMAND_FOCUS);
return map;
return MapBuilder.<String, Integer>builder()
.put("goBack", COMMAND_GO_BACK)
.put("goForward", COMMAND_GO_FORWARD)
.put("reload", COMMAND_RELOAD)
.put("stopLoading", COMMAND_STOP_LOADING)
.put("postMessage", COMMAND_POST_MESSAGE)
.put("injectJavaScript", COMMAND_INJECT_JAVASCRIPT)
.put("loadUrl", COMMAND_LOAD_URL)
.put("requestFocus", COMMAND_FOCUS)
.put("clearFormData", COMMAND_CLEAR_FORM_DATA)
.put("clearCache", COMMAND_CLEAR_CACHE)
.put("clearHistory", COMMAND_CLEAR_HISTORY)
.build();
}
@Override
@@ -606,6 +614,16 @@ public class RNCWebViewManager extends SimpleViewManager<WebView> {
case COMMAND_FOCUS:
root.requestFocus();
break;
case COMMAND_CLEAR_FORM_DATA:
root.clearFormData();
break;
case COMMAND_CLEAR_CACHE:
boolean includeDiskFiles = args != null && args.getBoolean(0);
root.clearCache(includeDiskFiles);
break;
case COMMAND_CLEAR_HISTORY:
root.clearHistory();
break;
}
}
+29
View File
@@ -70,6 +70,9 @@ This document lays out the current public properties and methods for the React N
- [`reload`](Reference.md#reload)
- [`stopLoading`](Reference.md#stoploading)
- [`injectJavaScript`](Reference.md#injectjavascriptstr)
- [`clearFormData`](Reference.md#clearFormData)
- [`clearCache`](Reference.md#clearCache)
- [`clearHistory`](Reference.md#clearHistory)
---
@@ -1108,6 +1111,32 @@ Executes the JavaScript string.
To learn more, read the [Communicating between JS and Native](Guide.md#communicating-between-js-and-native) guide.
### `clearFormData()`
(android only)
```javascript
clearFormData();
```
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())
### `clearCache(bool)`
(android only)
```javascript
clearCache(true);
```
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))
### `clearHistory()`
(android only)
```javascript
clearHistory();
```
Tells this WebView to clear its internal back/forward list. [developer.android.com reference](https://developer.android.com/reference/android/webkit/WebView.html#clearHistory())
## Other Docs
Also check out our [Getting Started Guide](Getting-Started.md) and [In-Depth Guide](Guide.md).
+26 -2
View File
@@ -27,12 +27,12 @@ import {
AndroidWebViewProps,
NativeWebViewAndroid,
State,
RNCWebViewUIManager,
RNCWebViewUIManagerAndroid,
} from './WebViewTypes';
import styles from './WebView.styles';
const UIManager = NotTypedUIManager as RNCWebViewUIManager;
const UIManager = NotTypedUIManager as RNCWebViewUIManagerAndroid;
const RNCWebView = requireNativeComponent(
'RNCWebView',
@@ -123,6 +123,30 @@ class WebView extends React.Component<AndroidWebViewProps, State> {
);
};
clearFormData = () => {
UIManager.dispatchViewManagerCommand(
this.getWebViewHandle(),
this.getCommands().clearFormData,
undefined,
);
}
clearCache = (includeDiskFiles: boolean) => {
UIManager.dispatchViewManagerCommand(
this.getWebViewHandle(),
this.getCommands().clearCache,
[includeDiskFiles],
);
};
clearHistory = () => {
UIManager.dispatchViewManagerCommand(
this.getWebViewHandle(),
this.getCommands().clearHistory,
undefined,
);
};
/**
* Injects a javascript string into the referenced WebView. Deliberately does not
* return a response because using eval() to return a response breaks this method
+2 -2
View File
@@ -28,12 +28,12 @@ import {
NativeWebViewIOS,
ViewManager,
State,
RNCWebViewUIManager,
RNCWebViewUIManagerIOS,
} from './WebViewTypes';
import styles from './WebView.styles';
const UIManager = NotTypedUIManager as RNCWebViewUIManager;
const UIManager = NotTypedUIManager as RNCWebViewUIManagerIOS;
const { resolveAssetSource } = Image;
const processDecelerationRate = (
+17 -8
View File
@@ -14,14 +14,23 @@ import {
type WebViewCommands = 'goForward' | 'goBack' | 'reload' | 'stopLoading' | 'postMessage' | 'injectJavaScript' | 'loadUrl' | 'requestFocus';
export interface RNCWebViewUIManager extends UIManagerStatic {
type AndroidWebViewCommands = 'clearHistory' | 'clearCache' | 'clearFormData';
interface RNCWebViewUIManager<Commands extends string> extends UIManagerStatic {
getViewManagerConfig: (
name: string,
name: string,
) => {
Commands: { [key in WebViewCommands]: number };
Commands: {[key in Commands]: number};
};
}
export type RNCWebViewUIManagerAndroid = RNCWebViewUIManager<WebViewCommands | AndroidWebViewCommands>
export type RNCWebViewUIManagerIOS = RNCWebViewUIManager<WebViewCommands>
type WebViewState = 'IDLE' | 'LOADING' | 'ERROR';
interface BaseState {
@@ -457,7 +466,7 @@ export interface AndroidWebViewProps extends WebViewSharedProps {
/**
* https://developer.android.com/reference/android/webkit/WebSettings.html#setCacheMode(int)
* Set the cacheMode. Possible values are:
*
*
* - `'LOAD_DEFAULT'` (default)
* - `'LOAD_CACHE_ELSE_NETWORK'`
* - `'LOAD_NO_CACHE'`
@@ -492,15 +501,15 @@ export interface AndroidWebViewProps extends WebViewSharedProps {
*/
geolocationEnabled?: boolean;
/**
* Boolean that sets whether JavaScript running in the context of a file
* scheme URL should be allowed to access content from other file scheme URLs.
* Boolean that sets whether JavaScript running in the context of a file
* scheme URL should be allowed to access content from other file scheme URLs.
* Including accessing content from other file scheme URLs
* @platform android
*/
allowFileAccessFromFileURLs?: boolean;
/**
* Boolean that sets whether JavaScript running in the context of a file
* scheme URL should be allowed to access content from any origin.