Skip to content

Commit

Permalink
feat(webview): add setServerAssetPath method
Browse files Browse the repository at this point in the history
Co-authored-by: Mark Anderson <[email protected]>
  • Loading branch information
ItsChaceD and markemer authored Jan 24, 2024
1 parent 4c03451 commit 881235b
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions android/capacitor/src/main/assets/native-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ var nativeBridge = (function (exports) {
callback(result.path);
});
};
IonicWebView.setServerAssetPath = (path) => {
var _a;
(_a = Plugins === null || Plugins === void 0 ? void 0 : Plugins.WebView) === null || _a === void 0 ? void 0 : _a.setServerAssetPath({ path });
};
IonicWebView.setServerBasePath = (path) => {
var _a;
(_a = Plugins === null || Plugins === void 0 ? void 0 : Plugins.WebView) === null || _a === void 0 ? void 0 : _a.setServerBasePath({ path });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ public class WebView extends Plugin {
public static final String WEBVIEW_PREFS_NAME = "CapWebViewSettings";
public static final String CAP_SERVER_PATH = "serverBasePath";

@PluginMethod
public void setServerAssetPath(PluginCall call) {
String path = call.getString("path");
bridge.setServerAssetPath(path);
call.resolve();
}

@PluginMethod
public void setServerBasePath(PluginCall call) {
String path = call.getString("path");
Expand Down
4 changes: 4 additions & 0 deletions core/native-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ const initBridge = (w: any): void => {
});
};

IonicWebView.setServerAssetPath = (path: any) => {
Plugins?.WebView?.setServerAssetPath({ path });
};

IonicWebView.setServerBasePath = (path: any) => {
Plugins?.WebView?.setServerBasePath({ path });
};
Expand Down
1 change: 1 addition & 0 deletions core/src/core-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { WebPlugin } from './web-plugin';

/******** WEB VIEW PLUGIN ********/
export interface WebViewPlugin extends Plugin {
setServerAssetPath(options: WebViewPath): Promise<void>;
setServerBasePath(options: WebViewPath): Promise<void>;
getServerBasePath(): Promise<WebViewPath>;
persistServerBasePath(): Promise<void>;
Expand Down
1 change: 1 addition & 0 deletions core/src/definitions-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ export interface WindowCapacitor {
WebView?: {
getServerBasePath?: any;
setServerBasePath?: any;
setServerAssetPath?: any;
persistServerBasePath?: any;
convertFileSrc?: any;
};
Expand Down
1 change: 1 addition & 0 deletions ios/Capacitor/Capacitor/Plugins/DefaultPlugins.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
)

CAP_PLUGIN(CAPWebViewPlugin, "WebView",
CAP_PLUGIN_METHOD(setServerAssetPath, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(setServerBasePath, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getServerBasePath, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(persistServerBasePath, CAPPluginReturnPromise);
Expand Down
7 changes: 7 additions & 0 deletions ios/Capacitor/Capacitor/Plugins/WebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import Foundation
@objc(CAPWebViewPlugin)
public class CAPWebViewPlugin: CAPPlugin {

@objc func setServerAssetPath(_ call: CAPPluginCall) {
if let path = call.getString("path"), let viewController = bridge?.viewController as? CAPBridgeViewController {
viewController.setServerBasePath(path: Bundle.main.url(forResource: path, withExtension: nil)?.path ?? path)
call.resolve()
}
}

@objc func setServerBasePath(_ call: CAPPluginCall) {
if let path = call.getString("path"), let viewController = bridge?.viewController as? CAPBridgeViewController {
viewController.setServerBasePath(path: path)
Expand Down
4 changes: 4 additions & 0 deletions ios/Capacitor/Capacitor/assets/native-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ var nativeBridge = (function (exports) {
callback(result.path);
});
};
IonicWebView.setServerAssetPath = (path) => {
var _a;
(_a = Plugins === null || Plugins === void 0 ? void 0 : Plugins.WebView) === null || _a === void 0 ? void 0 : _a.setServerAssetPath({ path });
};
IonicWebView.setServerBasePath = (path) => {
var _a;
(_a = Plugins === null || Plugins === void 0 ? void 0 : Plugins.WebView) === null || _a === void 0 ? void 0 : _a.setServerBasePath({ path });
Expand Down

0 comments on commit 881235b

Please sign in to comment.