From c1c624aacab2e31a764b57ba6b3c02b54c05d1c7 Mon Sep 17 00:00:00 2001 From: Mike Summerfeldt <20338451+IT-MikeS@users.noreply.github.com> Date: Thu, 12 Aug 2021 12:17:00 -0400 Subject: [PATCH] feat(core): implement CapacitorCustomPlatform for 3rd party platforms (#4771) Co-authored-by: jcesarmobile --- core/src/definitions-internal.ts | 9 +++++++++ core/src/platforms.ts | 10 +++++++++- core/src/runtime.ts | 23 +++++++++++++++++++++-- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/core/src/definitions-internal.ts b/core/src/definitions-internal.ts index 6660a9793..b6af23d75 100644 --- a/core/src/definitions-internal.ts +++ b/core/src/definitions-internal.ts @@ -165,9 +165,18 @@ export interface StoredCallback { reject?: (...args: any[]) => any; } +export interface CapacitorCustomPlatformInstance { + name: string; + plugins: { [pluginName: string]: any }; +} + export interface WindowCapacitor { Capacitor?: CapacitorInstance; + /** + * @deprecated Use `CapacitorCustomPlatform` instead + */ CapacitorPlatforms?: CapacitorPlatformsInstance; + CapacitorCustomPlatform?: CapacitorCustomPlatformInstance; Ionic?: { WebView?: { getServerBasePath?: any; diff --git a/core/src/platforms.ts b/core/src/platforms.ts index 65da81da8..54460f7e6 100644 --- a/core/src/platforms.ts +++ b/core/src/platforms.ts @@ -48,6 +48,9 @@ const createCapacitorPlatforms = (win: any): CapacitorPlatformsInstance => { const initPlatforms = (win: any) => (win.CapacitorPlatforms = createCapacitorPlatforms(win)); +/** + * @deprecated Set `CapacitorCustomPlatform` on the window object prior to runtime executing in the web app instead + */ export const CapacitorPlatforms = /*#__PURE__*/ initPlatforms( (typeof globalThis !== 'undefined' ? globalThis @@ -59,6 +62,11 @@ export const CapacitorPlatforms = /*#__PURE__*/ initPlatforms( ? global : {}) as any, ); - +/** + * @deprecated Set `CapacitorCustomPlatform` on the window object prior to runtime executing in the web app instead + */ export const addPlatform = CapacitorPlatforms.addPlatform; +/** + * @deprecated Set `CapacitorCustomPlatform` on the window object prior to runtime executing in the web app instead + */ export const setPlatform = CapacitorPlatforms.setPlatform; diff --git a/core/src/runtime.ts b/core/src/runtime.ts index 4fbc46739..b196269ad 100644 --- a/core/src/runtime.ts +++ b/core/src/runtime.ts @@ -1,5 +1,6 @@ import type { CapacitorGlobal, PluginImplementations } from './definitions'; import type { + CapacitorCustomPlatformInstance, CapacitorInstance, PluginHeader, WindowCapacitor, @@ -14,15 +15,24 @@ export interface RegisteredPlugin { } export const createCapacitor = (win: WindowCapacitor): CapacitorInstance => { + const capCustomPlatform: CapacitorCustomPlatformInstance = + win.CapacitorCustomPlatform || null; const cap: CapacitorInstance = win.Capacitor || ({} as any); const Plugins = (cap.Plugins = cap.Plugins || ({} as any)); + /** + * @deprecated Use `capCustomPlatform` instead, default functions like registerPlugin will function with the new object. + */ const capPlatforms: CapacitorPlatformsInstance = win.CapacitorPlatforms; - const defaultGetPlatform = () => getPlatformId(win); + const defaultGetPlatform = () => { + return capCustomPlatform !== null + ? capCustomPlatform.name + : getPlatformId(win); + }; const getPlatform = capPlatforms?.currentPlatform?.getPlatform || defaultGetPlatform; - const defaultIsNativePlatform = () => getPlatformId(win) !== 'web'; + const defaultIsNativePlatform = () => getPlatform() !== 'web'; const isNativePlatform = capPlatforms?.currentPlatform?.isNativePlatform || defaultIsNativePlatform; @@ -89,6 +99,15 @@ export const createCapacitor = (win: WindowCapacitor): CapacitorInstance => { typeof jsImplementations[platform] === 'function' ? (jsImplementation = await jsImplementations[platform]()) : (jsImplementation = jsImplementations[platform]); + } else if ( + capCustomPlatform !== null && + !jsImplementation && + 'web' in jsImplementations + ) { + jsImplementation = + typeof jsImplementations['web'] === 'function' + ? (jsImplementation = await jsImplementations['web']()) + : (jsImplementation = jsImplementations['web']); } return jsImplementation;