1 |
|
2 | const createCapacitorPlatforms = (win) => {
|
3 | const defaultPlatformMap = new Map();
|
4 | defaultPlatformMap.set('web', { name: 'web' });
|
5 | const capPlatforms = win.CapacitorPlatforms || {
|
6 | currentPlatform: { name: 'web' },
|
7 | platforms: defaultPlatformMap,
|
8 | };
|
9 | const addPlatform = (name, platform) => {
|
10 | capPlatforms.platforms.set(name, platform);
|
11 | };
|
12 | const setPlatform = (name) => {
|
13 | if (capPlatforms.platforms.has(name)) {
|
14 | capPlatforms.currentPlatform = capPlatforms.platforms.get(name);
|
15 | }
|
16 | };
|
17 | capPlatforms.addPlatform = addPlatform;
|
18 | capPlatforms.setPlatform = setPlatform;
|
19 | return capPlatforms;
|
20 | };
|
21 | const initPlatforms = (win) => (win.CapacitorPlatforms = createCapacitorPlatforms(win));
|
22 |
|
23 |
|
24 |
|
25 | const CapacitorPlatforms = initPlatforms((typeof globalThis !== 'undefined'
|
26 | ? globalThis
|
27 | : typeof self !== 'undefined'
|
28 | ? self
|
29 | : typeof window !== 'undefined'
|
30 | ? window
|
31 | : typeof global !== 'undefined'
|
32 | ? global
|
33 | : {}));
|
34 |
|
35 |
|
36 |
|
37 | const addPlatform = CapacitorPlatforms.addPlatform;
|
38 |
|
39 |
|
40 |
|
41 | const setPlatform = CapacitorPlatforms.setPlatform;
|
42 |
|
43 | const legacyRegisterWebPlugin = (cap, webPlugin) => {
|
44 | var _a;
|
45 | const config = webPlugin.config;
|
46 | const Plugins = cap.Plugins;
|
47 | if (!config || !config.name) {
|
48 |
|
49 | throw new Error(`Capacitor WebPlugin is using the deprecated "registerWebPlugin()" function, but without the config. Please use "registerPlugin()" instead to register this web plugin."`);
|
50 | }
|
51 |
|
52 | console.warn(`Capacitor plugin "${config.name}" is using the deprecated "registerWebPlugin()" function`);
|
53 | if (!Plugins[config.name] || ((_a = config === null || config === void 0 ? void 0 : config.platforms) === null || _a === void 0 ? void 0 : _a.includes(cap.getPlatform()))) {
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | Plugins[config.name] = webPlugin;
|
62 | }
|
63 | };
|
64 |
|
65 | var ExceptionCode;
|
66 | (function (ExceptionCode) {
|
67 | |
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | ExceptionCode["Unimplemented"] = "UNIMPLEMENTED";
|
74 | |
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | ExceptionCode["Unavailable"] = "UNAVAILABLE";
|
82 | })(ExceptionCode || (ExceptionCode = {}));
|
83 | class CapacitorException extends Error {
|
84 | constructor(message, code) {
|
85 | super(message);
|
86 | this.message = message;
|
87 | this.code = code;
|
88 | }
|
89 | }
|
90 | const getPlatformId = (win) => {
|
91 | var _a, _b;
|
92 | if (win === null || win === void 0 ? void 0 : win.androidBridge) {
|
93 | return 'android';
|
94 | }
|
95 | else if ((_b = (_a = win === null || win === void 0 ? void 0 : win.webkit) === null || _a === void 0 ? void 0 : _a.messageHandlers) === null || _b === void 0 ? void 0 : _b.bridge) {
|
96 | return 'ios';
|
97 | }
|
98 | else {
|
99 | return 'web';
|
100 | }
|
101 | };
|
102 |
|
103 | const createCapacitor = (win) => {
|
104 | var _a, _b, _c, _d, _e;
|
105 | const capCustomPlatform = win.CapacitorCustomPlatform || null;
|
106 | const cap = win.Capacitor || {};
|
107 | const Plugins = (cap.Plugins = cap.Plugins || {});
|
108 | |
109 |
|
110 |
|
111 | const capPlatforms = win.CapacitorPlatforms;
|
112 | const defaultGetPlatform = () => {
|
113 | return capCustomPlatform !== null
|
114 | ? capCustomPlatform.name
|
115 | : getPlatformId(win);
|
116 | };
|
117 | const getPlatform = ((_a = capPlatforms === null || capPlatforms === void 0 ? void 0 : capPlatforms.currentPlatform) === null || _a === void 0 ? void 0 : _a.getPlatform) || defaultGetPlatform;
|
118 | const defaultIsNativePlatform = () => getPlatform() !== 'web';
|
119 | const isNativePlatform = ((_b = capPlatforms === null || capPlatforms === void 0 ? void 0 : capPlatforms.currentPlatform) === null || _b === void 0 ? void 0 : _b.isNativePlatform) || defaultIsNativePlatform;
|
120 | const defaultIsPluginAvailable = (pluginName) => {
|
121 | const plugin = registeredPlugins.get(pluginName);
|
122 | if (plugin === null || plugin === void 0 ? void 0 : plugin.platforms.has(getPlatform())) {
|
123 |
|
124 | return true;
|
125 | }
|
126 | if (getPluginHeader(pluginName)) {
|
127 |
|
128 | return true;
|
129 | }
|
130 | return false;
|
131 | };
|
132 | const isPluginAvailable = ((_c = capPlatforms === null || capPlatforms === void 0 ? void 0 : capPlatforms.currentPlatform) === null || _c === void 0 ? void 0 : _c.isPluginAvailable) ||
|
133 | defaultIsPluginAvailable;
|
134 | const defaultGetPluginHeader = (pluginName) => { var _a; return (_a = cap.PluginHeaders) === null || _a === void 0 ? void 0 : _a.find(h => h.name === pluginName); };
|
135 | const getPluginHeader = ((_d = capPlatforms === null || capPlatforms === void 0 ? void 0 : capPlatforms.currentPlatform) === null || _d === void 0 ? void 0 : _d.getPluginHeader) || defaultGetPluginHeader;
|
136 | const handleError = (err) => win.console.error(err);
|
137 | const pluginMethodNoop = (_target, prop, pluginName) => {
|
138 | return Promise.reject(`${pluginName} does not have an implementation of "${prop}".`);
|
139 | };
|
140 | const registeredPlugins = new Map();
|
141 | const defaultRegisterPlugin = (pluginName, jsImplementations = {}) => {
|
142 | const registeredPlugin = registeredPlugins.get(pluginName);
|
143 | if (registeredPlugin) {
|
144 | console.warn(`Capacitor plugin "${pluginName}" already registered. Cannot register plugins twice.`);
|
145 | return registeredPlugin.proxy;
|
146 | }
|
147 | const platform = getPlatform();
|
148 | const pluginHeader = getPluginHeader(pluginName);
|
149 | let jsImplementation;
|
150 | const loadPluginImplementation = async () => {
|
151 | if (!jsImplementation && platform in jsImplementations) {
|
152 | jsImplementation =
|
153 | typeof jsImplementations[platform] === 'function'
|
154 | ? (jsImplementation = await jsImplementations[platform]())
|
155 | : (jsImplementation = jsImplementations[platform]);
|
156 | }
|
157 | else if (capCustomPlatform !== null &&
|
158 | !jsImplementation &&
|
159 | 'web' in jsImplementations) {
|
160 | jsImplementation =
|
161 | typeof jsImplementations['web'] === 'function'
|
162 | ? (jsImplementation = await jsImplementations['web']())
|
163 | : (jsImplementation = jsImplementations['web']);
|
164 | }
|
165 | return jsImplementation;
|
166 | };
|
167 | const createPluginMethod = (impl, prop) => {
|
168 | var _a, _b;
|
169 | if (pluginHeader) {
|
170 | const methodHeader = pluginHeader === null || pluginHeader === void 0 ? void 0 : pluginHeader.methods.find(m => prop === m.name);
|
171 | if (methodHeader) {
|
172 | if (methodHeader.rtype === 'promise') {
|
173 | return (options) => cap.nativePromise(pluginName, prop.toString(), options);
|
174 | }
|
175 | else {
|
176 | return (options, callback) => cap.nativeCallback(pluginName, prop.toString(), options, callback);
|
177 | }
|
178 | }
|
179 | else if (impl) {
|
180 | return (_a = impl[prop]) === null || _a === void 0 ? void 0 : _a.bind(impl);
|
181 | }
|
182 | }
|
183 | else if (impl) {
|
184 | return (_b = impl[prop]) === null || _b === void 0 ? void 0 : _b.bind(impl);
|
185 | }
|
186 | else {
|
187 | throw new CapacitorException(`"${pluginName}" plugin is not implemented on ${platform}`, ExceptionCode.Unimplemented);
|
188 | }
|
189 | };
|
190 | const createPluginMethodWrapper = (prop) => {
|
191 | let remove;
|
192 | const wrapper = (...args) => {
|
193 | const p = loadPluginImplementation().then(impl => {
|
194 | const fn = createPluginMethod(impl, prop);
|
195 | if (fn) {
|
196 | const p = fn(...args);
|
197 | remove = p === null || p === void 0 ? void 0 : p.remove;
|
198 | return p;
|
199 | }
|
200 | else {
|
201 | throw new CapacitorException(`"${pluginName}.${prop}()" is not implemented on ${platform}`, ExceptionCode.Unimplemented);
|
202 | }
|
203 | });
|
204 | if (prop === 'addListener') {
|
205 | p.remove = async () => remove();
|
206 | }
|
207 | return p;
|
208 | };
|
209 |
|
210 | wrapper.toString = () => `${prop.toString()}() { [capacitor code] }`;
|
211 | Object.defineProperty(wrapper, 'name', {
|
212 | value: prop,
|
213 | writable: false,
|
214 | configurable: false,
|
215 | });
|
216 | return wrapper;
|
217 | };
|
218 | const addListener = createPluginMethodWrapper('addListener');
|
219 | const removeListener = createPluginMethodWrapper('removeListener');
|
220 | const addListenerNative = (eventName, callback) => {
|
221 | const call = addListener({ eventName }, callback);
|
222 | const remove = async () => {
|
223 | const callbackId = await call;
|
224 | removeListener({
|
225 | eventName,
|
226 | callbackId,
|
227 | }, callback);
|
228 | };
|
229 | const p = new Promise(resolve => call.then(() => resolve({ remove })));
|
230 | p.remove = async () => {
|
231 | console.warn(`Using addListener() without 'await' is deprecated.`);
|
232 | await remove();
|
233 | };
|
234 | return p;
|
235 | };
|
236 | const proxy = new Proxy({}, {
|
237 | get(_, prop) {
|
238 | switch (prop) {
|
239 |
|
240 | case '$$typeof':
|
241 | return undefined;
|
242 | case 'toJSON':
|
243 | return () => ({});
|
244 | case 'addListener':
|
245 | return pluginHeader ? addListenerNative : addListener;
|
246 | case 'removeListener':
|
247 | return removeListener;
|
248 | default:
|
249 | return createPluginMethodWrapper(prop);
|
250 | }
|
251 | },
|
252 | });
|
253 | Plugins[pluginName] = proxy;
|
254 | registeredPlugins.set(pluginName, {
|
255 | name: pluginName,
|
256 | proxy,
|
257 | platforms: new Set([
|
258 | ...Object.keys(jsImplementations),
|
259 | ...(pluginHeader ? [platform] : []),
|
260 | ]),
|
261 | });
|
262 | return proxy;
|
263 | };
|
264 | const registerPlugin = ((_e = capPlatforms === null || capPlatforms === void 0 ? void 0 : capPlatforms.currentPlatform) === null || _e === void 0 ? void 0 : _e.registerPlugin) || defaultRegisterPlugin;
|
265 |
|
266 | if (!cap.convertFileSrc) {
|
267 | cap.convertFileSrc = filePath => filePath;
|
268 | }
|
269 | cap.getPlatform = getPlatform;
|
270 | cap.handleError = handleError;
|
271 | cap.isNativePlatform = isNativePlatform;
|
272 | cap.isPluginAvailable = isPluginAvailable;
|
273 | cap.pluginMethodNoop = pluginMethodNoop;
|
274 | cap.registerPlugin = registerPlugin;
|
275 | cap.Exception = CapacitorException;
|
276 | cap.DEBUG = !!cap.DEBUG;
|
277 | cap.isLoggingEnabled = !!cap.isLoggingEnabled;
|
278 |
|
279 | cap.platform = cap.getPlatform();
|
280 | cap.isNative = cap.isNativePlatform();
|
281 | return cap;
|
282 | };
|
283 | const initCapacitorGlobal = (win) => (win.Capacitor = createCapacitor(win));
|
284 |
|
285 | const Capacitor = initCapacitorGlobal(typeof globalThis !== 'undefined'
|
286 | ? globalThis
|
287 | : typeof self !== 'undefined'
|
288 | ? self
|
289 | : typeof window !== 'undefined'
|
290 | ? window
|
291 | : typeof global !== 'undefined'
|
292 | ? global
|
293 | : {});
|
294 | const registerPlugin = Capacitor.registerPlugin;
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 | const Plugins = Capacitor.Plugins;
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 | const registerWebPlugin = (plugin) => legacyRegisterWebPlugin(Capacitor, plugin);
|
313 |
|
314 |
|
315 |
|
316 |
|
317 | class WebPlugin {
|
318 | constructor(config) {
|
319 | this.listeners = {};
|
320 | this.windowListeners = {};
|
321 | if (config) {
|
322 |
|
323 | console.warn(`Capacitor WebPlugin "${config.name}" config object was deprecated in v3 and will be removed in v4.`);
|
324 | this.config = config;
|
325 | }
|
326 | }
|
327 | addListener(eventName, listenerFunc) {
|
328 | const listeners = this.listeners[eventName];
|
329 | if (!listeners) {
|
330 | this.listeners[eventName] = [];
|
331 | }
|
332 | this.listeners[eventName].push(listenerFunc);
|
333 |
|
334 |
|
335 | const windowListener = this.windowListeners[eventName];
|
336 | if (windowListener && !windowListener.registered) {
|
337 | this.addWindowListener(windowListener);
|
338 | }
|
339 | const remove = async () => this.removeListener(eventName, listenerFunc);
|
340 | const p = Promise.resolve({ remove });
|
341 | Object.defineProperty(p, 'remove', {
|
342 | value: async () => {
|
343 | console.warn(`Using addListener() without 'await' is deprecated.`);
|
344 | await remove();
|
345 | },
|
346 | });
|
347 | return p;
|
348 | }
|
349 | async removeAllListeners() {
|
350 | this.listeners = {};
|
351 | for (const listener in this.windowListeners) {
|
352 | this.removeWindowListener(this.windowListeners[listener]);
|
353 | }
|
354 | this.windowListeners = {};
|
355 | }
|
356 | notifyListeners(eventName, data) {
|
357 | const listeners = this.listeners[eventName];
|
358 | if (listeners) {
|
359 | listeners.forEach(listener => listener(data));
|
360 | }
|
361 | }
|
362 | hasListeners(eventName) {
|
363 | return !!this.listeners[eventName].length;
|
364 | }
|
365 | registerWindowListener(windowEventName, pluginEventName) {
|
366 | this.windowListeners[pluginEventName] = {
|
367 | registered: false,
|
368 | windowEventName,
|
369 | pluginEventName,
|
370 | handler: event => {
|
371 | this.notifyListeners(pluginEventName, event);
|
372 | },
|
373 | };
|
374 | }
|
375 | unimplemented(msg = 'not implemented') {
|
376 | return new Capacitor.Exception(msg, ExceptionCode.Unimplemented);
|
377 | }
|
378 | unavailable(msg = 'not available') {
|
379 | return new Capacitor.Exception(msg, ExceptionCode.Unavailable);
|
380 | }
|
381 | async removeListener(eventName, listenerFunc) {
|
382 | const listeners = this.listeners[eventName];
|
383 | if (!listeners) {
|
384 | return;
|
385 | }
|
386 | const index = listeners.indexOf(listenerFunc);
|
387 | this.listeners[eventName].splice(index, 1);
|
388 |
|
389 |
|
390 | if (!this.listeners[eventName].length) {
|
391 | this.removeWindowListener(this.windowListeners[eventName]);
|
392 | }
|
393 | }
|
394 | addWindowListener(handle) {
|
395 | window.addEventListener(handle.windowEventName, handle.handler);
|
396 | handle.registered = true;
|
397 | }
|
398 | removeWindowListener(handle) {
|
399 | if (!handle) {
|
400 | return;
|
401 | }
|
402 | window.removeEventListener(handle.windowEventName, handle.handler);
|
403 | handle.registered = false;
|
404 | }
|
405 | }
|
406 |
|
407 | const WebView = registerPlugin('WebView');
|
408 |
|
409 | export { Capacitor, CapacitorException, CapacitorPlatforms, ExceptionCode, Plugins, WebPlugin, WebView, addPlatform, registerPlugin, registerWebPlugin, setPlatform };
|
410 |
|