diff --git a/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java b/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java index c8222c2..4480c06 100644 --- a/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java +++ b/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java @@ -115,6 +115,12 @@ public class RNCWebViewManager extends SimpleViewManager { 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 { 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 { @Override public @Nullable Map 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.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 { 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; } } diff --git a/docs/Reference.md b/docs/Reference.md index 3cf7a33..df50211 100644 --- a/docs/Reference.md +++ b/docs/Reference.md @@ -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). diff --git a/src/WebView.android.tsx b/src/WebView.android.tsx index 1ee534b..4a06cdb 100644 --- a/src/WebView.android.tsx +++ b/src/WebView.android.tsx @@ -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 { ); }; + 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 diff --git a/src/WebView.ios.tsx b/src/WebView.ios.tsx index c822dd2..243f815 100644 --- a/src/WebView.ios.tsx +++ b/src/WebView.ios.tsx @@ -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 = ( diff --git a/src/WebViewTypes.ts b/src/WebViewTypes.ts index 567c798..579ff70 100644 --- a/src/WebViewTypes.ts +++ b/src/WebViewTypes.ts @@ -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 extends UIManagerStatic { getViewManagerConfig: ( - name: string, + name: string, ) => { - Commands: { [key in WebViewCommands]: number }; + Commands: {[key in Commands]: number}; }; } +export type RNCWebViewUIManagerAndroid = RNCWebViewUIManager +export type RNCWebViewUIManagerIOS = RNCWebViewUIManager + + + 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.