reflex-capacitor 0.3.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- reflex_capacitor/__init__.py +8 -0
- reflex_capacitor/android_signing.py +166 -0
- reflex_capacitor/bridge/__init__.py +34 -0
- reflex_capacitor/bridge/_script.py +48 -0
- reflex_capacitor/bridge/api.py +359 -0
- reflex_capacitor/bridge/api.pyi +60 -0
- reflex_capacitor/bridge/assets/bridge.js +805 -0
- reflex_capacitor/bridge/assets/image-editor.js +614 -0
- reflex_capacitor/bridge/inject.py +88 -0
- reflex_capacitor/bridge/ios_plist.py +112 -0
- reflex_capacitor/bridge/log.py +60 -0
- reflex_capacitor/bridge/plugins.py +259 -0
- reflex_capacitor/cli.py +793 -0
- reflex_capacitor/components/__init__.py +5 -0
- reflex_capacitor/components/image_editor.py +92 -0
- reflex_capacitor/config.py +50 -0
- reflex_capacitor/dev_util.py +59 -0
- reflex_capacitor/network_env.py +86 -0
- reflex_capacitor/plugin.py +392 -0
- reflex_capacitor/preflight.py +394 -0
- reflex_capacitor/scaffold/.gitignore +8 -0
- reflex_capacitor/scaffold/capacitor.config.json +8 -0
- reflex_capacitor/scaffold/package.json +13 -0
- reflex_capacitor-0.3.2.dist-info/METADATA +126 -0
- reflex_capacitor-0.3.2.dist-info/RECORD +28 -0
- reflex_capacitor-0.3.2.dist-info/WHEEL +4 -0
- reflex_capacitor-0.3.2.dist-info/entry_points.txt +2 -0
- reflex_capacitor-0.3.2.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,805 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* reflex-capacitor bridge — stable JS API for Reflex rx.call_script handlers.
|
|
3
|
+
* Requires capacitor.js + plugin.js bundles loaded first (see inject.py).
|
|
4
|
+
*/
|
|
5
|
+
(function () {
|
|
6
|
+
"use strict";
|
|
7
|
+
|
|
8
|
+
const Cap = typeof window !== "undefined" ? window.Capacitor : undefined;
|
|
9
|
+
const Plugins = Cap && Cap.Plugins ? Cap.Plugins : {};
|
|
10
|
+
const MAX_LOGS = 100;
|
|
11
|
+
const MAX_NATIVE_EVENTS = 200;
|
|
12
|
+
const logs = [];
|
|
13
|
+
const nativeEvents = [];
|
|
14
|
+
let listenersReady = false;
|
|
15
|
+
let pushListenersReady = false;
|
|
16
|
+
let backButtonMode = "emit";
|
|
17
|
+
|
|
18
|
+
function capPlatform() {
|
|
19
|
+
return Cap && Cap.getPlatform ? Cap.getPlatform() : "web";
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function capIsNative() {
|
|
23
|
+
return !!(Cap && Cap.isNativePlatform && Cap.isNativePlatform());
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function capIsAndroid() {
|
|
27
|
+
return capPlatform() === "android";
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function capIsIos() {
|
|
31
|
+
return capPlatform() === "ios";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function capIsWeb() {
|
|
35
|
+
return capPlatform() === "web";
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function capPlatformInfo() {
|
|
39
|
+
const p = capPlatform();
|
|
40
|
+
return {
|
|
41
|
+
platform: p,
|
|
42
|
+
isNative: capIsNative(),
|
|
43
|
+
isAndroid: p === "android",
|
|
44
|
+
isIos: p === "ios",
|
|
45
|
+
isWeb: p === "web",
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Prefer uri/webPath over huge dataUrl in native WebViews (Android + iOS). */
|
|
50
|
+
function preferCameraUriResult(saveGallery) {
|
|
51
|
+
return capIsNative() || !!saveGallery;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function plugin(name) {
|
|
55
|
+
if (Plugins[name]) return Plugins[name];
|
|
56
|
+
console.warn("reflex-capacitor: Capacitor plugin not loaded:", name);
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function addLog(level, method, detail) {
|
|
61
|
+
const entry = {
|
|
62
|
+
ts: new Date().toISOString(),
|
|
63
|
+
level: level,
|
|
64
|
+
method: method,
|
|
65
|
+
detail: detail || {},
|
|
66
|
+
};
|
|
67
|
+
logs.unshift(entry);
|
|
68
|
+
if (logs.length > MAX_LOGS) logs.length = MAX_LOGS;
|
|
69
|
+
const prefix = "[reflex-capacitor]";
|
|
70
|
+
if (level === "error") {
|
|
71
|
+
console.error(prefix, method, detail);
|
|
72
|
+
} else if (level === "warn") {
|
|
73
|
+
console.warn(prefix, method, detail);
|
|
74
|
+
} else {
|
|
75
|
+
console.log(prefix, method, detail);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function wrap(name, fn) {
|
|
80
|
+
return async function wrapped(args) {
|
|
81
|
+
const payload = args || {};
|
|
82
|
+
addLog("info", name, { phase: "start", args: payload });
|
|
83
|
+
try {
|
|
84
|
+
const result = await fn(payload);
|
|
85
|
+
addLog("info", name, { phase: "ok", args: payload, result: result });
|
|
86
|
+
return result;
|
|
87
|
+
} catch (err) {
|
|
88
|
+
const message = String(err && err.message ? err.message : err);
|
|
89
|
+
addLog("error", name, {
|
|
90
|
+
phase: "error",
|
|
91
|
+
args: payload,
|
|
92
|
+
error: message,
|
|
93
|
+
stack: err && err.stack ? err.stack : undefined,
|
|
94
|
+
});
|
|
95
|
+
return { ok: false, error: message };
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function isUserCancel(err) {
|
|
101
|
+
const msg = String(err && err.message ? err.message : err).toLowerCase();
|
|
102
|
+
return msg.indexOf("cancel") >= 0;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function locationGranted(perm) {
|
|
106
|
+
return (
|
|
107
|
+
(perm && perm.location === "granted") ||
|
|
108
|
+
(perm && perm.coarseLocation === "granted")
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async function ensureCameraPermission(Cam, saveGallery) {
|
|
113
|
+
let perm = await Cam.checkPermissions();
|
|
114
|
+
if (perm.camera !== "granted") {
|
|
115
|
+
perm = await Cam.requestPermissions({ permissions: ["camera"] });
|
|
116
|
+
if (perm.camera !== "granted") {
|
|
117
|
+
return { ok: false, error: "permission_denied", permission: perm };
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (saveGallery) {
|
|
121
|
+
perm = await Cam.checkPermissions();
|
|
122
|
+
if (perm.photos !== "granted") {
|
|
123
|
+
perm = await Cam.requestPermissions({ permissions: ["photos"] });
|
|
124
|
+
if (perm.photos !== "granted") {
|
|
125
|
+
return { ok: false, error: "photos_permission_denied", permission: perm };
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return { ok: true };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async function ensurePhotosPermission(Cam) {
|
|
133
|
+
let perm = await Cam.checkPermissions();
|
|
134
|
+
if (perm.photos !== "granted") {
|
|
135
|
+
perm = await Cam.requestPermissions({ permissions: ["photos"] });
|
|
136
|
+
if (perm.photos !== "granted") {
|
|
137
|
+
return { ok: false, error: "permission_denied", permission: perm };
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return { ok: true };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function pushNativeEvent(type, detail) {
|
|
144
|
+
const entry = {
|
|
145
|
+
ts: new Date().toISOString(),
|
|
146
|
+
type: type,
|
|
147
|
+
detail: detail || {},
|
|
148
|
+
};
|
|
149
|
+
nativeEvents.push(entry);
|
|
150
|
+
if (nativeEvents.length > MAX_NATIVE_EVENTS) {
|
|
151
|
+
nativeEvents.shift();
|
|
152
|
+
}
|
|
153
|
+
addLog("info", "nativeEvent", entry);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function setupPushListeners() {
|
|
157
|
+
if (pushListenersReady) {
|
|
158
|
+
return { ok: true, already: true };
|
|
159
|
+
}
|
|
160
|
+
const PN = plugin("PushNotifications");
|
|
161
|
+
if (!PN) {
|
|
162
|
+
return { ok: false, error: "plugin_missing" };
|
|
163
|
+
}
|
|
164
|
+
PN.addListener("registration", function (token) {
|
|
165
|
+
pushNativeEvent("pushRegistration", {
|
|
166
|
+
value: token && token.value ? token.value : "",
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
PN.addListener("registrationError", function (err) {
|
|
170
|
+
pushNativeEvent("pushRegistrationError", {
|
|
171
|
+
error: err && err.error ? String(err.error) : String(err),
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
PN.addListener("pushNotificationReceived", function (notification) {
|
|
175
|
+
const n = notification || {};
|
|
176
|
+
pushNativeEvent("pushNotificationReceived", {
|
|
177
|
+
id: n.id != null ? n.id : "",
|
|
178
|
+
title: n.title || "",
|
|
179
|
+
body: n.body || "",
|
|
180
|
+
data: n.data || {},
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
PN.addListener("pushNotificationActionPerformed", function (action) {
|
|
184
|
+
const a = action || {};
|
|
185
|
+
const n = a.notification || {};
|
|
186
|
+
pushNativeEvent("pushNotificationActionPerformed", {
|
|
187
|
+
actionId: a.actionId || "",
|
|
188
|
+
notification: {
|
|
189
|
+
id: n.id != null ? n.id : "",
|
|
190
|
+
title: n.title || "",
|
|
191
|
+
body: n.body || "",
|
|
192
|
+
data: n.data || {},
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
pushListenersReady = true;
|
|
197
|
+
return { ok: true };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
async function setupNativeListeners({ backButton }) {
|
|
201
|
+
if (listenersReady) {
|
|
202
|
+
return { ok: true, already: true, backButton: backButtonMode };
|
|
203
|
+
}
|
|
204
|
+
backButtonMode = backButton || "emit";
|
|
205
|
+
const AppPlugin = plugin("App");
|
|
206
|
+
if (AppPlugin) {
|
|
207
|
+
AppPlugin.addListener("appStateChange", function (state) {
|
|
208
|
+
pushNativeEvent("appStateChange", { isActive: state.isActive });
|
|
209
|
+
});
|
|
210
|
+
if (capIsAndroid()) {
|
|
211
|
+
AppPlugin.addListener("backButton", function () {
|
|
212
|
+
pushNativeEvent("backButton", {});
|
|
213
|
+
if (backButtonMode === "exit") {
|
|
214
|
+
AppPlugin.exitApp();
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
if (backButtonMode === "history" && window.history && window.history.length > 1) {
|
|
218
|
+
window.history.back();
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
AppPlugin.addListener("appUrlOpen", function (data) {
|
|
223
|
+
pushNativeEvent("appUrlOpen", { url: data && data.url ? data.url : "" });
|
|
224
|
+
});
|
|
225
|
+
AppPlugin.addListener("pause", function () {
|
|
226
|
+
pushNativeEvent("pause", {});
|
|
227
|
+
});
|
|
228
|
+
AppPlugin.addListener("resume", function () {
|
|
229
|
+
pushNativeEvent("resume", {});
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
const Keyboard = plugin("Keyboard");
|
|
233
|
+
if (Keyboard) {
|
|
234
|
+
Keyboard.addListener("keyboardWillShow", function (info) {
|
|
235
|
+
pushNativeEvent("keyboardWillShow", {
|
|
236
|
+
keyboardHeight: info && info.keyboardHeight != null ? info.keyboardHeight : 0,
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
Keyboard.addListener("keyboardWillHide", function () {
|
|
240
|
+
pushNativeEvent("keyboardWillHide", {});
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
listenersReady = true;
|
|
244
|
+
setupPushListeners();
|
|
245
|
+
return { ok: true, backButton: backButtonMode, platform: capPlatform() };
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function drainNativeEvents() {
|
|
249
|
+
const events = nativeEvents.slice();
|
|
250
|
+
nativeEvents.length = 0;
|
|
251
|
+
return { ok: true, count: events.length, events: events };
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const core = {
|
|
255
|
+
isNative() {
|
|
256
|
+
return capIsNative();
|
|
257
|
+
},
|
|
258
|
+
|
|
259
|
+
platform() {
|
|
260
|
+
return capPlatform();
|
|
261
|
+
},
|
|
262
|
+
|
|
263
|
+
platformInfo() {
|
|
264
|
+
return capPlatformInfo();
|
|
265
|
+
},
|
|
266
|
+
|
|
267
|
+
isAndroid() {
|
|
268
|
+
return capIsAndroid();
|
|
269
|
+
},
|
|
270
|
+
|
|
271
|
+
isIos() {
|
|
272
|
+
return capIsIos();
|
|
273
|
+
},
|
|
274
|
+
|
|
275
|
+
isWeb() {
|
|
276
|
+
return capIsWeb();
|
|
277
|
+
},
|
|
278
|
+
|
|
279
|
+
async notify({ title, body }) {
|
|
280
|
+
const LN = plugin("LocalNotifications");
|
|
281
|
+
if (!LN) {
|
|
282
|
+
if (typeof Notification !== "undefined" && Notification.permission === "granted") {
|
|
283
|
+
new Notification(title || "", { body: body || "" });
|
|
284
|
+
} else {
|
|
285
|
+
addLog("warn", "notify", { fallback: "web Notification unavailable" });
|
|
286
|
+
}
|
|
287
|
+
return { ok: true, fallback: "web" };
|
|
288
|
+
}
|
|
289
|
+
let perm = await LN.checkPermissions();
|
|
290
|
+
if (perm.display !== "granted") {
|
|
291
|
+
perm = await LN.requestPermissions();
|
|
292
|
+
if (perm.display !== "granted") {
|
|
293
|
+
addLog("warn", "notify", { permission: perm });
|
|
294
|
+
return { ok: false, error: "permission_denied", permission: perm };
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
await LN.schedule({
|
|
298
|
+
notifications: [
|
|
299
|
+
{
|
|
300
|
+
id: Math.floor(Date.now() % 2147483647),
|
|
301
|
+
title: title || "",
|
|
302
|
+
body: body || "",
|
|
303
|
+
},
|
|
304
|
+
],
|
|
305
|
+
});
|
|
306
|
+
return { ok: true };
|
|
307
|
+
},
|
|
308
|
+
|
|
309
|
+
async toast({ text, duration }) {
|
|
310
|
+
const T = plugin("Toast");
|
|
311
|
+
if (!T) {
|
|
312
|
+
addLog("warn", "toast", { fallback: "console", text: text });
|
|
313
|
+
return { ok: true, fallback: "console" };
|
|
314
|
+
}
|
|
315
|
+
await T.show({
|
|
316
|
+
text: text || "",
|
|
317
|
+
duration: duration === "long" ? "long" : "short",
|
|
318
|
+
});
|
|
319
|
+
return { ok: true };
|
|
320
|
+
},
|
|
321
|
+
|
|
322
|
+
async hapticsImpact({ style }) {
|
|
323
|
+
const H = plugin("Haptics");
|
|
324
|
+
if (!H) return { ok: false, error: "plugin_missing" };
|
|
325
|
+
await H.impact({ style: style || "MEDIUM" });
|
|
326
|
+
return { ok: true };
|
|
327
|
+
},
|
|
328
|
+
|
|
329
|
+
async hapticsNotification({ type }) {
|
|
330
|
+
const H = plugin("Haptics");
|
|
331
|
+
if (!H) return { ok: false, error: "plugin_missing" };
|
|
332
|
+
await H.notification({ type: type || "SUCCESS" });
|
|
333
|
+
return { ok: true };
|
|
334
|
+
},
|
|
335
|
+
|
|
336
|
+
/** Longer vibration — easier to feel on Android than short impact ticks. */
|
|
337
|
+
async hapticsVibrate({ duration }) {
|
|
338
|
+
const H = plugin("Haptics");
|
|
339
|
+
const ms = Math.max(10, Math.min(Number(duration) || 300, 5000));
|
|
340
|
+
if (!H) {
|
|
341
|
+
if (typeof navigator !== "undefined" && navigator.vibrate) {
|
|
342
|
+
navigator.vibrate(ms);
|
|
343
|
+
return { ok: true, fallback: "web_vibrate", duration: ms };
|
|
344
|
+
}
|
|
345
|
+
return { ok: false, error: "haptics_unavailable" };
|
|
346
|
+
}
|
|
347
|
+
if (typeof H.vibrate === "function") {
|
|
348
|
+
await H.vibrate({ duration: ms });
|
|
349
|
+
return { ok: true, duration: ms };
|
|
350
|
+
}
|
|
351
|
+
await H.impact({ style: "HEAVY" });
|
|
352
|
+
return { ok: true, fallback: "impact_only", duration: ms };
|
|
353
|
+
},
|
|
354
|
+
|
|
355
|
+
async share({ title, text, url, dialogTitle }) {
|
|
356
|
+
const S = plugin("Share");
|
|
357
|
+
if (!S) {
|
|
358
|
+
if (navigator.share) {
|
|
359
|
+
await navigator.share({ title, text, url });
|
|
360
|
+
return { ok: true, fallback: "web_share" };
|
|
361
|
+
}
|
|
362
|
+
return { ok: false, error: "share_unavailable" };
|
|
363
|
+
}
|
|
364
|
+
await S.share({ title, text, url, dialogTitle });
|
|
365
|
+
return { ok: true };
|
|
366
|
+
},
|
|
367
|
+
|
|
368
|
+
async clipboardWrite({ text }) {
|
|
369
|
+
const C = plugin("Clipboard");
|
|
370
|
+
if (!C) {
|
|
371
|
+
if (navigator.clipboard) {
|
|
372
|
+
await navigator.clipboard.writeText(text || "");
|
|
373
|
+
return { ok: true, fallback: "web_clipboard" };
|
|
374
|
+
}
|
|
375
|
+
return { ok: false, error: "clipboard_unavailable" };
|
|
376
|
+
}
|
|
377
|
+
await C.write({ string: text || "" });
|
|
378
|
+
return { ok: true };
|
|
379
|
+
},
|
|
380
|
+
|
|
381
|
+
async clipboardRead() {
|
|
382
|
+
const C = plugin("Clipboard");
|
|
383
|
+
if (!C) {
|
|
384
|
+
if (navigator.clipboard) return { value: await navigator.clipboard.readText() };
|
|
385
|
+
return { value: "", error: "clipboard_unavailable" };
|
|
386
|
+
}
|
|
387
|
+
const result = await C.read();
|
|
388
|
+
return { value: result && result.value ? result.value : "" };
|
|
389
|
+
},
|
|
390
|
+
|
|
391
|
+
async statusBarSetStyle({ style }) {
|
|
392
|
+
const SB = plugin("StatusBar");
|
|
393
|
+
if (!SB) return { ok: false, error: "plugin_missing" };
|
|
394
|
+
await SB.setStyle({ style: style || "DARK" });
|
|
395
|
+
return { ok: true };
|
|
396
|
+
},
|
|
397
|
+
|
|
398
|
+
async statusBarHide() {
|
|
399
|
+
const SB = plugin("StatusBar");
|
|
400
|
+
if (!SB) return { ok: false, error: "plugin_missing" };
|
|
401
|
+
await SB.hide();
|
|
402
|
+
return { ok: true };
|
|
403
|
+
},
|
|
404
|
+
|
|
405
|
+
async statusBarShow() {
|
|
406
|
+
const SB = plugin("StatusBar");
|
|
407
|
+
if (!SB) return { ok: false, error: "plugin_missing" };
|
|
408
|
+
await SB.show();
|
|
409
|
+
return { ok: true };
|
|
410
|
+
},
|
|
411
|
+
|
|
412
|
+
async splashHide() {
|
|
413
|
+
const SP = plugin("SplashScreen");
|
|
414
|
+
if (!SP) return { ok: false, error: "plugin_missing" };
|
|
415
|
+
await SP.hide();
|
|
416
|
+
return { ok: true };
|
|
417
|
+
},
|
|
418
|
+
|
|
419
|
+
async deviceInfo() {
|
|
420
|
+
const D = plugin("Device");
|
|
421
|
+
if (!D) return { platform: core.platform(), isVirtual: false, fallback: true };
|
|
422
|
+
return await D.getInfo();
|
|
423
|
+
},
|
|
424
|
+
|
|
425
|
+
async networkStatus() {
|
|
426
|
+
const N = plugin("Network");
|
|
427
|
+
if (!N) return { connected: navigator.onLine, connectionType: "unknown", fallback: true };
|
|
428
|
+
return await N.getStatus();
|
|
429
|
+
},
|
|
430
|
+
|
|
431
|
+
async appExit() {
|
|
432
|
+
const A = plugin("App");
|
|
433
|
+
if (!A) return { ok: false, error: "plugin_missing" };
|
|
434
|
+
await A.exitApp();
|
|
435
|
+
return { ok: true };
|
|
436
|
+
},
|
|
437
|
+
|
|
438
|
+
async prefSet({ key, value }) {
|
|
439
|
+
const P = plugin("Preferences");
|
|
440
|
+
if (!P) return { ok: false, error: "plugin_missing" };
|
|
441
|
+
await P.set({ key: key || "", value: String(value ?? "") });
|
|
442
|
+
return { ok: true, key: key };
|
|
443
|
+
},
|
|
444
|
+
|
|
445
|
+
async prefGet({ key }) {
|
|
446
|
+
const P = plugin("Preferences");
|
|
447
|
+
if (!P) return { ok: false, error: "plugin_missing", value: "" };
|
|
448
|
+
const result = await P.get({ key: key || "" });
|
|
449
|
+
return { ok: true, key: key, value: result && result.value != null ? result.value : "" };
|
|
450
|
+
},
|
|
451
|
+
|
|
452
|
+
async takePhoto({ quality, saveToGallery }) {
|
|
453
|
+
const Cam = plugin("Camera");
|
|
454
|
+
if (!Cam) return { ok: false, error: "plugin_missing" };
|
|
455
|
+
const saveGallery = !!saveToGallery;
|
|
456
|
+
const permResult = await ensureCameraPermission(Cam, saveGallery);
|
|
457
|
+
if (!permResult.ok) return permResult;
|
|
458
|
+
const q = Math.max(1, Math.min(Number(quality) || 90, 100));
|
|
459
|
+
const useUri = preferCameraUriResult(saveGallery);
|
|
460
|
+
try {
|
|
461
|
+
const photo = await Cam.getPhoto({
|
|
462
|
+
quality: q,
|
|
463
|
+
allowEditing: false,
|
|
464
|
+
resultType: useUri ? "uri" : "dataUrl",
|
|
465
|
+
source: "CAMERA",
|
|
466
|
+
saveToGallery: saveGallery,
|
|
467
|
+
});
|
|
468
|
+
return {
|
|
469
|
+
ok: true,
|
|
470
|
+
dataUrl: photo.dataUrl || "",
|
|
471
|
+
webPath: photo.webPath || "",
|
|
472
|
+
path: photo.path || "",
|
|
473
|
+
format: photo.format || "",
|
|
474
|
+
saved: !!photo.saved,
|
|
475
|
+
saveToGallery: saveGallery,
|
|
476
|
+
};
|
|
477
|
+
} catch (err) {
|
|
478
|
+
if (isUserCancel(err)) {
|
|
479
|
+
return { ok: false, cancelled: true, error: "user_cancelled" };
|
|
480
|
+
}
|
|
481
|
+
return { ok: false, error: String(err && err.message ? err.message : err) };
|
|
482
|
+
}
|
|
483
|
+
},
|
|
484
|
+
|
|
485
|
+
async pickImages({ limit, quality }) {
|
|
486
|
+
const Cam = plugin("Camera");
|
|
487
|
+
if (!Cam) return { ok: false, error: "plugin_missing" };
|
|
488
|
+
const permResult = await ensurePhotosPermission(Cam);
|
|
489
|
+
if (!permResult.ok) return permResult;
|
|
490
|
+
try {
|
|
491
|
+
const result = await Cam.pickImages({
|
|
492
|
+
quality: Math.max(1, Math.min(Number(quality) || 90, 100)),
|
|
493
|
+
limit: Math.max(1, Math.min(Number(limit) || 1, 10)),
|
|
494
|
+
});
|
|
495
|
+
const photos = (result.photos || []).map(function (p) {
|
|
496
|
+
return { webPath: p.webPath || "", format: p.format || "" };
|
|
497
|
+
});
|
|
498
|
+
return { ok: true, count: photos.length, photos: photos };
|
|
499
|
+
} catch (err) {
|
|
500
|
+
if (isUserCancel(err)) {
|
|
501
|
+
return { ok: false, cancelled: true, error: "user_cancelled", count: 0, photos: [] };
|
|
502
|
+
}
|
|
503
|
+
return { ok: false, error: String(err && err.message ? err.message : err) };
|
|
504
|
+
}
|
|
505
|
+
},
|
|
506
|
+
|
|
507
|
+
async getCurrentPosition({ enableHighAccuracy, timeout }) {
|
|
508
|
+
const G = plugin("Geolocation");
|
|
509
|
+
if (!G) return { ok: false, error: "plugin_missing" };
|
|
510
|
+
let perm = await G.checkPermissions();
|
|
511
|
+
if (!locationGranted(perm)) {
|
|
512
|
+
perm = await G.requestPermissions();
|
|
513
|
+
if (!locationGranted(perm)) {
|
|
514
|
+
return { ok: false, error: "permission_denied", permission: perm };
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
const timeoutMs = Math.max(8000, Math.min(Number(timeout) || 30000, 120000));
|
|
518
|
+
const attempts =
|
|
519
|
+
enableHighAccuracy === true
|
|
520
|
+
? [
|
|
521
|
+
{ accurate: true, label: "gps", timeout: Math.min(timeoutMs, 45000) },
|
|
522
|
+
{ accurate: false, label: "network", timeout: Math.min(timeoutMs, 15000) },
|
|
523
|
+
]
|
|
524
|
+
: [
|
|
525
|
+
{ accurate: false, label: "network", timeout: Math.min(timeoutMs, 15000) },
|
|
526
|
+
{ accurate: true, label: "gps", timeout: Math.min(timeoutMs, 45000) },
|
|
527
|
+
];
|
|
528
|
+
let lastError = null;
|
|
529
|
+
for (let i = 0; i < attempts.length; i++) {
|
|
530
|
+
const attempt = attempts[i];
|
|
531
|
+
try {
|
|
532
|
+
const pos = await G.getCurrentPosition({
|
|
533
|
+
enableHighAccuracy: attempt.accurate,
|
|
534
|
+
timeout: attempt.timeout,
|
|
535
|
+
maximumAge: 60000,
|
|
536
|
+
});
|
|
537
|
+
return {
|
|
538
|
+
ok: true,
|
|
539
|
+
latitude: pos.coords.latitude,
|
|
540
|
+
longitude: pos.coords.longitude,
|
|
541
|
+
accuracy: pos.coords.accuracy,
|
|
542
|
+
altitude: pos.coords.altitude,
|
|
543
|
+
timestamp: pos.timestamp,
|
|
544
|
+
source: attempt.label,
|
|
545
|
+
};
|
|
546
|
+
} catch (err) {
|
|
547
|
+
lastError = err;
|
|
548
|
+
addLog("warn", "getCurrentPosition", {
|
|
549
|
+
attempt: attempt.label,
|
|
550
|
+
timeout: attempt.timeout,
|
|
551
|
+
error: String(err),
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
return {
|
|
556
|
+
ok: false,
|
|
557
|
+
error: "location_timeout",
|
|
558
|
+
message: lastError ? String(lastError) : "unknown",
|
|
559
|
+
hint:
|
|
560
|
+
"已授权但仍超时:请确认系统「定位/GPS」已开启;室内先试网络定位,或到窗边/室外再试。",
|
|
561
|
+
timeout: timeoutMs,
|
|
562
|
+
};
|
|
563
|
+
},
|
|
564
|
+
|
|
565
|
+
async keyboardShow() {
|
|
566
|
+
const K = plugin("Keyboard");
|
|
567
|
+
if (!K) return { ok: false, error: "plugin_missing" };
|
|
568
|
+
await K.show();
|
|
569
|
+
return { ok: true };
|
|
570
|
+
},
|
|
571
|
+
|
|
572
|
+
async keyboardHide() {
|
|
573
|
+
const K = plugin("Keyboard");
|
|
574
|
+
if (!K) return { ok: false, error: "plugin_missing" };
|
|
575
|
+
await K.hide();
|
|
576
|
+
return { ok: true };
|
|
577
|
+
},
|
|
578
|
+
|
|
579
|
+
async browserOpen({ url }) {
|
|
580
|
+
const B = plugin("Browser");
|
|
581
|
+
if (!B) {
|
|
582
|
+
if (typeof window !== "undefined" && url) {
|
|
583
|
+
window.open(url, "_blank");
|
|
584
|
+
return { ok: true, fallback: "window_open" };
|
|
585
|
+
}
|
|
586
|
+
return { ok: false, error: "browser_unavailable" };
|
|
587
|
+
}
|
|
588
|
+
await B.open({ url: url || "about:blank" });
|
|
589
|
+
return { ok: true };
|
|
590
|
+
},
|
|
591
|
+
|
|
592
|
+
async fsWrite({ path, data, directory, encoding }) {
|
|
593
|
+
const FS = plugin("Filesystem");
|
|
594
|
+
if (!FS) return { ok: false, error: "plugin_missing" };
|
|
595
|
+
await FS.writeFile({
|
|
596
|
+
path: path || "reflex-capacitor.txt",
|
|
597
|
+
data: data || "",
|
|
598
|
+
directory: directory || "DATA",
|
|
599
|
+
encoding: encoding || "utf8",
|
|
600
|
+
});
|
|
601
|
+
return { ok: true, path: path, directory: directory || "DATA" };
|
|
602
|
+
},
|
|
603
|
+
|
|
604
|
+
async fsRead({ path, directory }) {
|
|
605
|
+
const FS = plugin("Filesystem");
|
|
606
|
+
if (!FS) return { ok: false, error: "plugin_missing" };
|
|
607
|
+
const result = await FS.readFile({
|
|
608
|
+
path: path || "reflex-capacitor.txt",
|
|
609
|
+
directory: directory || "DATA",
|
|
610
|
+
encoding: "utf8",
|
|
611
|
+
});
|
|
612
|
+
return {
|
|
613
|
+
ok: true,
|
|
614
|
+
path: path,
|
|
615
|
+
data: result.data != null ? result.data : "",
|
|
616
|
+
directory: directory || "DATA",
|
|
617
|
+
};
|
|
618
|
+
},
|
|
619
|
+
|
|
620
|
+
async invoke({ plugin: pluginName, method, args }) {
|
|
621
|
+
const P = Plugins[pluginName];
|
|
622
|
+
if (!P || typeof P[method] !== "function") {
|
|
623
|
+
return { ok: false, error: "invoke_not_found", plugin: pluginName, method: method };
|
|
624
|
+
}
|
|
625
|
+
const result = await P[method](args || {});
|
|
626
|
+
return { ok: true, result: result };
|
|
627
|
+
},
|
|
628
|
+
|
|
629
|
+
async pushRegister() {
|
|
630
|
+
const PN = plugin("PushNotifications");
|
|
631
|
+
if (!PN) {
|
|
632
|
+
return { ok: false, error: "plugin_missing" };
|
|
633
|
+
}
|
|
634
|
+
setupPushListeners();
|
|
635
|
+
let perm = await PN.checkPermissions();
|
|
636
|
+
if (perm.receive !== "granted") {
|
|
637
|
+
perm = await PN.requestPermissions();
|
|
638
|
+
if (perm.receive !== "granted") {
|
|
639
|
+
return { ok: false, error: "permission_denied", permission: perm };
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
await PN.register();
|
|
643
|
+
return { ok: true };
|
|
644
|
+
},
|
|
645
|
+
|
|
646
|
+
async pushCheckPermissions() {
|
|
647
|
+
const PN = plugin("PushNotifications");
|
|
648
|
+
if (!PN) {
|
|
649
|
+
return { ok: false, error: "plugin_missing" };
|
|
650
|
+
}
|
|
651
|
+
const perm = await PN.checkPermissions();
|
|
652
|
+
return { ok: true, permission: perm };
|
|
653
|
+
},
|
|
654
|
+
|
|
655
|
+
async pushRequestPermissions() {
|
|
656
|
+
const PN = plugin("PushNotifications");
|
|
657
|
+
if (!PN) {
|
|
658
|
+
return { ok: false, error: "plugin_missing" };
|
|
659
|
+
}
|
|
660
|
+
const perm = await PN.requestPermissions();
|
|
661
|
+
return {
|
|
662
|
+
ok: perm.receive === "granted",
|
|
663
|
+
permission: perm,
|
|
664
|
+
};
|
|
665
|
+
},
|
|
666
|
+
|
|
667
|
+
async editImage({ dataUrl, webPath, editor }) {
|
|
668
|
+
const ed = window.__REFLEX_CAPACITOR_IMAGE_EDITOR__;
|
|
669
|
+
if (!ed) return { ok: false, error: "image_editor_not_loaded" };
|
|
670
|
+
return ed.open({ dataUrl: dataUrl, webPath: webPath, editor: editor || {} });
|
|
671
|
+
},
|
|
672
|
+
|
|
673
|
+
async captureAndEdit({ source, editor }) {
|
|
674
|
+
const ed = window.__REFLEX_CAPACITOR_IMAGE_EDITOR__;
|
|
675
|
+
if (!ed) return { ok: false, error: "image_editor_not_loaded" };
|
|
676
|
+
const src = source || "prompt";
|
|
677
|
+
const edOpts = editor || {};
|
|
678
|
+
const q = Math.round((Number(edOpts.quality) || 0.85) * 100);
|
|
679
|
+
let photo = null;
|
|
680
|
+
if (src === "camera") {
|
|
681
|
+
photo = await core.takePhoto({ quality: q, saveToGallery: false });
|
|
682
|
+
if (!photo.ok) return photo;
|
|
683
|
+
} else if (src === "gallery") {
|
|
684
|
+
const picked = await core.pickImages({ limit: 1, quality: q });
|
|
685
|
+
if (!picked.ok) return picked;
|
|
686
|
+
if (!picked.photos || !picked.photos.length) {
|
|
687
|
+
return { ok: false, cancelled: true };
|
|
688
|
+
}
|
|
689
|
+
photo = { ok: true, webPath: picked.photos[0].webPath, format: picked.photos[0].format };
|
|
690
|
+
} else {
|
|
691
|
+
const Cam = plugin("Camera");
|
|
692
|
+
if (!Cam) return { ok: false, error: "plugin_missing" };
|
|
693
|
+
const permResult = await ensureCameraPermission(Cam, false);
|
|
694
|
+
if (!permResult.ok) return permResult;
|
|
695
|
+
try {
|
|
696
|
+
const raw = await Cam.getPhoto({
|
|
697
|
+
quality: q,
|
|
698
|
+
resultType: preferCameraUriResult(false) ? "uri" : "dataUrl",
|
|
699
|
+
source: "PROMPT",
|
|
700
|
+
});
|
|
701
|
+
photo = { ok: true, dataUrl: raw.dataUrl, webPath: raw.webPath, format: raw.format };
|
|
702
|
+
} catch (err) {
|
|
703
|
+
if (isUserCancel(err)) {
|
|
704
|
+
return { ok: false, cancelled: true, error: "user_cancelled" };
|
|
705
|
+
}
|
|
706
|
+
return { ok: false, error: String(err && err.message ? err.message : err) };
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
return ed.open({
|
|
710
|
+
dataUrl: photo.dataUrl,
|
|
711
|
+
webPath: photo.webPath,
|
|
712
|
+
editor: edOpts,
|
|
713
|
+
});
|
|
714
|
+
},
|
|
715
|
+
};
|
|
716
|
+
|
|
717
|
+
const bridge = {
|
|
718
|
+
isNative: core.isNative,
|
|
719
|
+
platform: core.platform,
|
|
720
|
+
|
|
721
|
+
getLogs({ limit }) {
|
|
722
|
+
const n = Math.max(1, Math.min(Number(limit) || 50, MAX_LOGS));
|
|
723
|
+
return logs.slice(0, n);
|
|
724
|
+
},
|
|
725
|
+
|
|
726
|
+
clearLogs() {
|
|
727
|
+
logs.length = 0;
|
|
728
|
+
addLog("info", "clearLogs", { cleared: true });
|
|
729
|
+
return { ok: true };
|
|
730
|
+
},
|
|
731
|
+
|
|
732
|
+
getDiagnostics() {
|
|
733
|
+
const expected = [
|
|
734
|
+
"LocalNotifications",
|
|
735
|
+
"Clipboard",
|
|
736
|
+
"Haptics",
|
|
737
|
+
"Share",
|
|
738
|
+
"StatusBar",
|
|
739
|
+
"App",
|
|
740
|
+
"SplashScreen",
|
|
741
|
+
"Toast",
|
|
742
|
+
"Device",
|
|
743
|
+
"Network",
|
|
744
|
+
"Preferences",
|
|
745
|
+
"Camera",
|
|
746
|
+
"Geolocation",
|
|
747
|
+
"Keyboard",
|
|
748
|
+
"Browser",
|
|
749
|
+
"Filesystem",
|
|
750
|
+
"PushNotifications",
|
|
751
|
+
];
|
|
752
|
+
const loaded = expected.filter(function (name) {
|
|
753
|
+
return !!Plugins[name];
|
|
754
|
+
});
|
|
755
|
+
const missing = expected.filter(function (name) {
|
|
756
|
+
return !Plugins[name];
|
|
757
|
+
});
|
|
758
|
+
return {
|
|
759
|
+
bridgeLoaded: true,
|
|
760
|
+
bridgeVersion: 2,
|
|
761
|
+
isNative: capIsNative(),
|
|
762
|
+
platform: capPlatform(),
|
|
763
|
+
isAndroid: capIsAndroid(),
|
|
764
|
+
isIos: capIsIos(),
|
|
765
|
+
isWeb: capIsWeb(),
|
|
766
|
+
capacitorCore: !!(Cap && Cap.isNativePlatform),
|
|
767
|
+
pluginsLoaded: loaded,
|
|
768
|
+
pluginsMissing: missing,
|
|
769
|
+
logCount: logs.length,
|
|
770
|
+
nativeListenerReady: listenersReady,
|
|
771
|
+
pushListenerReady: pushListenersReady,
|
|
772
|
+
nativeEventBuffer: nativeEvents.length,
|
|
773
|
+
location: typeof window !== "undefined" && window.location ? window.location.href : "",
|
|
774
|
+
userAgent: typeof navigator !== "undefined" ? navigator.userAgent : "",
|
|
775
|
+
};
|
|
776
|
+
},
|
|
777
|
+
|
|
778
|
+
setupNativeListeners: wrap("setupNativeListeners", setupNativeListeners),
|
|
779
|
+
drainNativeEvents: wrap("drainNativeEvents", function () {
|
|
780
|
+
return drainNativeEvents();
|
|
781
|
+
}),
|
|
782
|
+
};
|
|
783
|
+
|
|
784
|
+
Object.keys(core).forEach(function (key) {
|
|
785
|
+
if (
|
|
786
|
+
key === "isNative" ||
|
|
787
|
+
key === "platform" ||
|
|
788
|
+
key === "platformInfo" ||
|
|
789
|
+
key === "isAndroid" ||
|
|
790
|
+
key === "isIos" ||
|
|
791
|
+
key === "isWeb"
|
|
792
|
+
) {
|
|
793
|
+
return;
|
|
794
|
+
}
|
|
795
|
+
bridge[key] = wrap(key, core[key]);
|
|
796
|
+
});
|
|
797
|
+
|
|
798
|
+
bridge.platformInfo = core.platformInfo;
|
|
799
|
+
bridge.isAndroid = core.isAndroid;
|
|
800
|
+
bridge.isIos = core.isIos;
|
|
801
|
+
bridge.isWeb = core.isWeb;
|
|
802
|
+
|
|
803
|
+
window.__REFLEX_CAPACITOR__ = bridge;
|
|
804
|
+
addLog("info", "init", bridge.getDiagnostics());
|
|
805
|
+
})();
|