@craft-native/react 0.0.74 → 0.0.76
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.
- package/LICENSE.md +21 -0
- package/package.json +1 -1
- package/dist/hooks/useDatabase.d.ts +0 -13
- package/dist/hooks/useDatabase.d.ts.map +0 -1
- package/dist/hooks/useFileSystem.d.ts +0 -15
- package/dist/hooks/useFileSystem.d.ts.map +0 -1
- package/dist/hooks/useHttp.d.ts +0 -21
- package/dist/hooks/useHttp.d.ts.map +0 -1
- package/dist/hooks/useNotification.d.ts +0 -20
- package/dist/hooks/useNotification.d.ts.map +0 -1
- package/dist/hooks/useTray.d.ts +0 -22
- package/dist/hooks/useTray.d.ts.map +0 -1
- package/dist/hooks/useWindow.d.ts +0 -19
- package/dist/hooks/useWindow.d.ts.map +0 -1
- package/dist/index.d.ts +0 -114
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -648
- package/dist/index.mjs +0 -608
package/dist/index.js
DELETED
|
@@ -1,648 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/index.ts
|
|
21
|
-
var index_exports = {};
|
|
22
|
-
__export(index_exports, {
|
|
23
|
-
useCraft: () => useCraft2,
|
|
24
|
-
useCraftEvent: () => useCraftEvent,
|
|
25
|
-
useCraftInvoke: () => useCraftInvoke,
|
|
26
|
-
useDatabase: () => useDatabase,
|
|
27
|
-
useDeviceInfo: () => useDeviceInfo,
|
|
28
|
-
useFileSystem: () => useFileSystem,
|
|
29
|
-
useHaptic: () => useHaptic,
|
|
30
|
-
useHttp: () => useHttp,
|
|
31
|
-
useIsDesktop: () => useIsDesktop,
|
|
32
|
-
useIsMobile: () => useIsMobile,
|
|
33
|
-
useNotification: () => useNotification,
|
|
34
|
-
usePermission: () => usePermission,
|
|
35
|
-
usePlatform: () => usePlatform,
|
|
36
|
-
useToast: () => useToast,
|
|
37
|
-
useTray: () => useTray,
|
|
38
|
-
useWindow: () => useWindow
|
|
39
|
-
});
|
|
40
|
-
module.exports = __toCommonJS(index_exports);
|
|
41
|
-
var import_react8 = require("react");
|
|
42
|
-
|
|
43
|
-
// src/hooks/useWindow.ts
|
|
44
|
-
var import_react2 = require("react");
|
|
45
|
-
|
|
46
|
-
// src/index.tsx
|
|
47
|
-
var import_react = require("react");
|
|
48
|
-
function useCraft() {
|
|
49
|
-
return window.craft;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// src/hooks/useWindow.ts
|
|
53
|
-
function useWindow() {
|
|
54
|
-
const { craft, isReady } = useCraft();
|
|
55
|
-
const [isFullscreen, setIsFullscreen] = (0, import_react2.useState)(false);
|
|
56
|
-
const [isMaximized, setIsMaximized] = (0, import_react2.useState)(false);
|
|
57
|
-
const [isMinimized, setIsMinimized] = (0, import_react2.useState)(false);
|
|
58
|
-
const setTitle = (0, import_react2.useCallback)(
|
|
59
|
-
async (title) => {
|
|
60
|
-
if (!isReady || !craft) return;
|
|
61
|
-
await craft.invoke("window.setTitle", { title });
|
|
62
|
-
},
|
|
63
|
-
[craft, isReady]
|
|
64
|
-
);
|
|
65
|
-
const setSize = (0, import_react2.useCallback)(
|
|
66
|
-
async (width, height) => {
|
|
67
|
-
if (!isReady || !craft) return;
|
|
68
|
-
await craft.invoke("window.setSize", { width, height });
|
|
69
|
-
},
|
|
70
|
-
[craft, isReady]
|
|
71
|
-
);
|
|
72
|
-
const setPosition = (0, import_react2.useCallback)(
|
|
73
|
-
async (x, y) => {
|
|
74
|
-
if (!isReady || !craft) return;
|
|
75
|
-
await craft.invoke("window.setPosition", { x, y });
|
|
76
|
-
},
|
|
77
|
-
[craft, isReady]
|
|
78
|
-
);
|
|
79
|
-
const maximize = (0, import_react2.useCallback)(async () => {
|
|
80
|
-
if (!isReady || !craft) return;
|
|
81
|
-
await craft.invoke("window.maximize");
|
|
82
|
-
setIsMaximized(true);
|
|
83
|
-
}, [craft, isReady]);
|
|
84
|
-
const minimize = (0, import_react2.useCallback)(async () => {
|
|
85
|
-
if (!isReady || !craft) return;
|
|
86
|
-
await craft.invoke("window.minimize");
|
|
87
|
-
setIsMinimized(true);
|
|
88
|
-
}, [craft, isReady]);
|
|
89
|
-
const restore = (0, import_react2.useCallback)(async () => {
|
|
90
|
-
if (!isReady || !craft) return;
|
|
91
|
-
await craft.invoke("window.restore");
|
|
92
|
-
setIsMaximized(false);
|
|
93
|
-
setIsMinimized(false);
|
|
94
|
-
}, [craft, isReady]);
|
|
95
|
-
const toggleFullscreen = (0, import_react2.useCallback)(async () => {
|
|
96
|
-
if (!isReady || !craft) return;
|
|
97
|
-
await craft.invoke("window.toggleFullscreen");
|
|
98
|
-
setIsFullscreen(!isFullscreen);
|
|
99
|
-
}, [craft, isReady, isFullscreen]);
|
|
100
|
-
const close = (0, import_react2.useCallback)(async () => {
|
|
101
|
-
if (!isReady || !craft) return;
|
|
102
|
-
await craft.invoke("window.close");
|
|
103
|
-
}, [craft, isReady]);
|
|
104
|
-
const center = (0, import_react2.useCallback)(async () => {
|
|
105
|
-
if (!isReady || !craft) return;
|
|
106
|
-
await craft.invoke("window.center");
|
|
107
|
-
}, [craft, isReady]);
|
|
108
|
-
const setAlwaysOnTop = (0, import_react2.useCallback)(
|
|
109
|
-
async (alwaysOnTop) => {
|
|
110
|
-
if (!isReady || !craft) return;
|
|
111
|
-
await craft.invoke("window.setAlwaysOnTop", { alwaysOnTop });
|
|
112
|
-
},
|
|
113
|
-
[craft, isReady]
|
|
114
|
-
);
|
|
115
|
-
return {
|
|
116
|
-
isFullscreen,
|
|
117
|
-
isMaximized,
|
|
118
|
-
isMinimized,
|
|
119
|
-
setTitle,
|
|
120
|
-
setSize,
|
|
121
|
-
setPosition,
|
|
122
|
-
maximize,
|
|
123
|
-
minimize,
|
|
124
|
-
restore,
|
|
125
|
-
toggleFullscreen,
|
|
126
|
-
close,
|
|
127
|
-
center,
|
|
128
|
-
setAlwaysOnTop
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// src/hooks/useTray.ts
|
|
133
|
-
var import_react3 = require("react");
|
|
134
|
-
function useTray() {
|
|
135
|
-
const { craft, isReady } = useCraft();
|
|
136
|
-
const [isVisible, setIsVisible] = (0, import_react3.useState)(false);
|
|
137
|
-
const create = (0, import_react3.useCallback)(
|
|
138
|
-
async (icon, tooltip) => {
|
|
139
|
-
if (!isReady || !craft) return;
|
|
140
|
-
await craft.invoke("tray.create", { icon, tooltip });
|
|
141
|
-
setIsVisible(true);
|
|
142
|
-
},
|
|
143
|
-
[craft, isReady]
|
|
144
|
-
);
|
|
145
|
-
const destroy = (0, import_react3.useCallback)(async () => {
|
|
146
|
-
if (!isReady || !craft) return;
|
|
147
|
-
await craft.invoke("tray.destroy");
|
|
148
|
-
setIsVisible(false);
|
|
149
|
-
}, [craft, isReady]);
|
|
150
|
-
const setIcon = (0, import_react3.useCallback)(
|
|
151
|
-
async (icon) => {
|
|
152
|
-
if (!isReady || !craft) return;
|
|
153
|
-
await craft.invoke("tray.setIcon", { icon });
|
|
154
|
-
},
|
|
155
|
-
[craft, isReady]
|
|
156
|
-
);
|
|
157
|
-
const setTooltip = (0, import_react3.useCallback)(
|
|
158
|
-
async (tooltip) => {
|
|
159
|
-
if (!isReady || !craft) return;
|
|
160
|
-
await craft.invoke("tray.setTooltip", { tooltip });
|
|
161
|
-
},
|
|
162
|
-
[craft, isReady]
|
|
163
|
-
);
|
|
164
|
-
const setMenu = (0, import_react3.useCallback)(
|
|
165
|
-
async (menu) => {
|
|
166
|
-
if (!isReady || !craft) return;
|
|
167
|
-
await craft.invoke("tray.setMenu", { menu });
|
|
168
|
-
},
|
|
169
|
-
[craft, isReady]
|
|
170
|
-
);
|
|
171
|
-
const setTitle = (0, import_react3.useCallback)(
|
|
172
|
-
async (title) => {
|
|
173
|
-
if (!isReady || !craft) return;
|
|
174
|
-
await craft.invoke("tray.setTitle", { title });
|
|
175
|
-
},
|
|
176
|
-
[craft, isReady]
|
|
177
|
-
);
|
|
178
|
-
return {
|
|
179
|
-
isVisible,
|
|
180
|
-
create,
|
|
181
|
-
destroy,
|
|
182
|
-
setIcon,
|
|
183
|
-
setTooltip,
|
|
184
|
-
setMenu,
|
|
185
|
-
setTitle
|
|
186
|
-
};
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// src/hooks/useNotification.ts
|
|
190
|
-
var import_react4 = require("react");
|
|
191
|
-
function useNotification() {
|
|
192
|
-
const { craft, isReady } = useCraft();
|
|
193
|
-
const send = (0, import_react4.useCallback)(
|
|
194
|
-
async (options) => {
|
|
195
|
-
if (!isReady || !craft) return;
|
|
196
|
-
await craft.invoke("notification.send", options);
|
|
197
|
-
},
|
|
198
|
-
[craft, isReady]
|
|
199
|
-
);
|
|
200
|
-
const requestPermission = (0, import_react4.useCallback)(async () => {
|
|
201
|
-
if (!isReady || !craft) return false;
|
|
202
|
-
const result = await craft.requestPermission("notifications");
|
|
203
|
-
return result.granted;
|
|
204
|
-
}, [craft, isReady]);
|
|
205
|
-
return {
|
|
206
|
-
send,
|
|
207
|
-
requestPermission
|
|
208
|
-
};
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
// src/hooks/useFileSystem.ts
|
|
212
|
-
var import_react5 = require("react");
|
|
213
|
-
function useFileSystem() {
|
|
214
|
-
const { craft, isReady } = useCraft();
|
|
215
|
-
const [loading, setLoading] = (0, import_react5.useState)(false);
|
|
216
|
-
const [error, setError] = (0, import_react5.useState)(null);
|
|
217
|
-
const readFile = (0, import_react5.useCallback)(
|
|
218
|
-
async (path, encoding = "utf8") => {
|
|
219
|
-
if (!isReady || !craft) throw new Error("Craft not ready");
|
|
220
|
-
setLoading(true);
|
|
221
|
-
setError(null);
|
|
222
|
-
try {
|
|
223
|
-
const result = await craft.invoke("fs.readFile", { path, encoding });
|
|
224
|
-
return result;
|
|
225
|
-
} catch (err) {
|
|
226
|
-
setError(err);
|
|
227
|
-
throw err;
|
|
228
|
-
} finally {
|
|
229
|
-
setLoading(false);
|
|
230
|
-
}
|
|
231
|
-
},
|
|
232
|
-
[craft, isReady]
|
|
233
|
-
);
|
|
234
|
-
const writeFile = (0, import_react5.useCallback)(
|
|
235
|
-
async (path, data, encoding = "utf8") => {
|
|
236
|
-
if (!isReady || !craft) throw new Error("Craft not ready");
|
|
237
|
-
setLoading(true);
|
|
238
|
-
setError(null);
|
|
239
|
-
try {
|
|
240
|
-
await craft.invoke("fs.writeFile", { path, data, encoding });
|
|
241
|
-
} catch (err) {
|
|
242
|
-
setError(err);
|
|
243
|
-
throw err;
|
|
244
|
-
} finally {
|
|
245
|
-
setLoading(false);
|
|
246
|
-
}
|
|
247
|
-
},
|
|
248
|
-
[craft, isReady]
|
|
249
|
-
);
|
|
250
|
-
const readDir = (0, import_react5.useCallback)(
|
|
251
|
-
async (path) => {
|
|
252
|
-
if (!isReady || !craft) throw new Error("Craft not ready");
|
|
253
|
-
setLoading(true);
|
|
254
|
-
setError(null);
|
|
255
|
-
try {
|
|
256
|
-
const result = await craft.invoke("fs.readDir", { path });
|
|
257
|
-
return result;
|
|
258
|
-
} catch (err) {
|
|
259
|
-
setError(err);
|
|
260
|
-
throw err;
|
|
261
|
-
} finally {
|
|
262
|
-
setLoading(false);
|
|
263
|
-
}
|
|
264
|
-
},
|
|
265
|
-
[craft, isReady]
|
|
266
|
-
);
|
|
267
|
-
const mkdir = (0, import_react5.useCallback)(
|
|
268
|
-
async (path, recursive = false) => {
|
|
269
|
-
if (!isReady || !craft) throw new Error("Craft not ready");
|
|
270
|
-
setLoading(true);
|
|
271
|
-
setError(null);
|
|
272
|
-
try {
|
|
273
|
-
await craft.invoke("fs.mkdir", { path, recursive });
|
|
274
|
-
} catch (err) {
|
|
275
|
-
setError(err);
|
|
276
|
-
throw err;
|
|
277
|
-
} finally {
|
|
278
|
-
setLoading(false);
|
|
279
|
-
}
|
|
280
|
-
},
|
|
281
|
-
[craft, isReady]
|
|
282
|
-
);
|
|
283
|
-
const remove = (0, import_react5.useCallback)(
|
|
284
|
-
async (path, recursive = false) => {
|
|
285
|
-
if (!isReady || !craft) throw new Error("Craft not ready");
|
|
286
|
-
setLoading(true);
|
|
287
|
-
setError(null);
|
|
288
|
-
try {
|
|
289
|
-
await craft.invoke("fs.remove", { path, recursive });
|
|
290
|
-
} catch (err) {
|
|
291
|
-
setError(err);
|
|
292
|
-
throw err;
|
|
293
|
-
} finally {
|
|
294
|
-
setLoading(false);
|
|
295
|
-
}
|
|
296
|
-
},
|
|
297
|
-
[craft, isReady]
|
|
298
|
-
);
|
|
299
|
-
const exists = (0, import_react5.useCallback)(
|
|
300
|
-
async (path) => {
|
|
301
|
-
if (!isReady || !craft) throw new Error("Craft not ready");
|
|
302
|
-
try {
|
|
303
|
-
const result = await craft.invoke("fs.exists", { path });
|
|
304
|
-
return result;
|
|
305
|
-
} catch (err) {
|
|
306
|
-
setError(err);
|
|
307
|
-
return false;
|
|
308
|
-
}
|
|
309
|
-
},
|
|
310
|
-
[craft, isReady]
|
|
311
|
-
);
|
|
312
|
-
const stat = (0, import_react5.useCallback)(
|
|
313
|
-
async (path) => {
|
|
314
|
-
if (!isReady || !craft) throw new Error("Craft not ready");
|
|
315
|
-
setLoading(true);
|
|
316
|
-
setError(null);
|
|
317
|
-
try {
|
|
318
|
-
const result = await craft.invoke("fs.stat", { path });
|
|
319
|
-
return result;
|
|
320
|
-
} catch (err) {
|
|
321
|
-
setError(err);
|
|
322
|
-
throw err;
|
|
323
|
-
} finally {
|
|
324
|
-
setLoading(false);
|
|
325
|
-
}
|
|
326
|
-
},
|
|
327
|
-
[craft, isReady]
|
|
328
|
-
);
|
|
329
|
-
return {
|
|
330
|
-
readFile,
|
|
331
|
-
writeFile,
|
|
332
|
-
readDir,
|
|
333
|
-
mkdir,
|
|
334
|
-
remove,
|
|
335
|
-
exists,
|
|
336
|
-
stat,
|
|
337
|
-
loading,
|
|
338
|
-
error
|
|
339
|
-
};
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
// src/hooks/useDatabase.ts
|
|
343
|
-
var import_react6 = require("react");
|
|
344
|
-
function useDatabase(dbPath) {
|
|
345
|
-
const { craft, isReady } = useCraft();
|
|
346
|
-
const [loading, setLoading] = (0, import_react6.useState)(false);
|
|
347
|
-
const [error, setError] = (0, import_react6.useState)(null);
|
|
348
|
-
const execute = (0, import_react6.useCallback)(
|
|
349
|
-
async (sql, params = []) => {
|
|
350
|
-
if (!isReady || !craft) throw new Error("Craft not ready");
|
|
351
|
-
setLoading(true);
|
|
352
|
-
setError(null);
|
|
353
|
-
try {
|
|
354
|
-
const result = await craft.invoke("db.execute", { path: dbPath, sql, params });
|
|
355
|
-
return result;
|
|
356
|
-
} catch (err) {
|
|
357
|
-
setError(err);
|
|
358
|
-
throw err;
|
|
359
|
-
} finally {
|
|
360
|
-
setLoading(false);
|
|
361
|
-
}
|
|
362
|
-
},
|
|
363
|
-
[craft, isReady, dbPath]
|
|
364
|
-
);
|
|
365
|
-
const query = (0, import_react6.useCallback)(
|
|
366
|
-
async (sql, params = []) => {
|
|
367
|
-
if (!isReady || !craft) throw new Error("Craft not ready");
|
|
368
|
-
setLoading(true);
|
|
369
|
-
setError(null);
|
|
370
|
-
try {
|
|
371
|
-
const result = await craft.invoke("db.query", { path: dbPath, sql, params });
|
|
372
|
-
return result;
|
|
373
|
-
} catch (err) {
|
|
374
|
-
setError(err);
|
|
375
|
-
throw err;
|
|
376
|
-
} finally {
|
|
377
|
-
setLoading(false);
|
|
378
|
-
}
|
|
379
|
-
},
|
|
380
|
-
[craft, isReady, dbPath]
|
|
381
|
-
);
|
|
382
|
-
const transaction = (0, import_react6.useCallback)(
|
|
383
|
-
async (callback) => {
|
|
384
|
-
if (!isReady || !craft) throw new Error("Craft not ready");
|
|
385
|
-
setLoading(true);
|
|
386
|
-
setError(null);
|
|
387
|
-
try {
|
|
388
|
-
await craft.invoke("db.transaction.begin", { path: dbPath });
|
|
389
|
-
try {
|
|
390
|
-
await callback({
|
|
391
|
-
execute: async (sql, params = []) => {
|
|
392
|
-
return await craft.invoke("db.execute", { path: dbPath, sql, params });
|
|
393
|
-
}
|
|
394
|
-
});
|
|
395
|
-
await craft.invoke("db.transaction.commit", { path: dbPath });
|
|
396
|
-
} catch (err) {
|
|
397
|
-
await craft.invoke("db.transaction.rollback", { path: dbPath });
|
|
398
|
-
throw err;
|
|
399
|
-
}
|
|
400
|
-
} catch (err) {
|
|
401
|
-
setError(err);
|
|
402
|
-
throw err;
|
|
403
|
-
} finally {
|
|
404
|
-
setLoading(false);
|
|
405
|
-
}
|
|
406
|
-
},
|
|
407
|
-
[craft, isReady, dbPath]
|
|
408
|
-
);
|
|
409
|
-
return {
|
|
410
|
-
execute,
|
|
411
|
-
query,
|
|
412
|
-
transaction,
|
|
413
|
-
loading,
|
|
414
|
-
error
|
|
415
|
-
};
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
// src/hooks/useHttp.ts
|
|
419
|
-
var import_react7 = require("react");
|
|
420
|
-
function useHttp() {
|
|
421
|
-
const { craft, isReady } = useCraft();
|
|
422
|
-
const [loading, setLoading] = (0, import_react7.useState)(false);
|
|
423
|
-
const [error, setError] = (0, import_react7.useState)(null);
|
|
424
|
-
const fetch = (0, import_react7.useCallback)(
|
|
425
|
-
async (url, options = {}) => {
|
|
426
|
-
if (!isReady || !craft) throw new Error("Craft not ready");
|
|
427
|
-
setLoading(true);
|
|
428
|
-
setError(null);
|
|
429
|
-
try {
|
|
430
|
-
const result = await craft.invoke("http.fetch", { url, ...options });
|
|
431
|
-
return result;
|
|
432
|
-
} catch (err) {
|
|
433
|
-
setError(err);
|
|
434
|
-
throw err;
|
|
435
|
-
} finally {
|
|
436
|
-
setLoading(false);
|
|
437
|
-
}
|
|
438
|
-
},
|
|
439
|
-
[craft, isReady]
|
|
440
|
-
);
|
|
441
|
-
const download = (0, import_react7.useCallback)(
|
|
442
|
-
async (url, path, options = {}) => {
|
|
443
|
-
if (!isReady || !craft) throw new Error("Craft not ready");
|
|
444
|
-
setLoading(true);
|
|
445
|
-
setError(null);
|
|
446
|
-
if (options.onProgress) {
|
|
447
|
-
craft.on("download.progress", options.onProgress);
|
|
448
|
-
}
|
|
449
|
-
try {
|
|
450
|
-
await craft.invoke("http.download", { url, path });
|
|
451
|
-
} catch (err) {
|
|
452
|
-
setError(err);
|
|
453
|
-
throw err;
|
|
454
|
-
} finally {
|
|
455
|
-
if (options.onProgress) {
|
|
456
|
-
craft.off("download.progress", options.onProgress);
|
|
457
|
-
}
|
|
458
|
-
setLoading(false);
|
|
459
|
-
}
|
|
460
|
-
},
|
|
461
|
-
[craft, isReady]
|
|
462
|
-
);
|
|
463
|
-
const upload = (0, import_react7.useCallback)(
|
|
464
|
-
async (url, filePath, options = {}) => {
|
|
465
|
-
if (!isReady || !craft) throw new Error("Craft not ready");
|
|
466
|
-
setLoading(true);
|
|
467
|
-
setError(null);
|
|
468
|
-
if (options.onProgress) {
|
|
469
|
-
craft.on("upload.progress", options.onProgress);
|
|
470
|
-
}
|
|
471
|
-
try {
|
|
472
|
-
const result = await craft.invoke("http.upload", { url, filePath, ...options });
|
|
473
|
-
return result;
|
|
474
|
-
} catch (err) {
|
|
475
|
-
setError(err);
|
|
476
|
-
throw err;
|
|
477
|
-
} finally {
|
|
478
|
-
if (options.onProgress) {
|
|
479
|
-
craft.off("upload.progress", options.onProgress);
|
|
480
|
-
}
|
|
481
|
-
setLoading(false);
|
|
482
|
-
}
|
|
483
|
-
},
|
|
484
|
-
[craft, isReady]
|
|
485
|
-
);
|
|
486
|
-
return {
|
|
487
|
-
fetch,
|
|
488
|
-
download,
|
|
489
|
-
upload,
|
|
490
|
-
loading,
|
|
491
|
-
error
|
|
492
|
-
};
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
// src/index.ts
|
|
496
|
-
function useCraft2() {
|
|
497
|
-
const [craft, setCraft] = (0, import_react8.useState)(null);
|
|
498
|
-
const [isReady, setIsReady] = (0, import_react8.useState)(false);
|
|
499
|
-
(0, import_react8.useEffect)(() => {
|
|
500
|
-
if (window.craft) {
|
|
501
|
-
setCraft(window.craft);
|
|
502
|
-
setIsReady(true);
|
|
503
|
-
} else {
|
|
504
|
-
const checkCraft = setInterval(() => {
|
|
505
|
-
if (window.craft) {
|
|
506
|
-
setCraft(window.craft);
|
|
507
|
-
setIsReady(true);
|
|
508
|
-
clearInterval(checkCraft);
|
|
509
|
-
}
|
|
510
|
-
}, 100);
|
|
511
|
-
return () => clearInterval(checkCraft);
|
|
512
|
-
}
|
|
513
|
-
}, []);
|
|
514
|
-
return { craft, isReady };
|
|
515
|
-
}
|
|
516
|
-
function usePlatform() {
|
|
517
|
-
const { craft, isReady } = useCraft2();
|
|
518
|
-
const [platform, setPlatform] = (0, import_react8.useState)(null);
|
|
519
|
-
const [loading, setLoading] = (0, import_react8.useState)(true);
|
|
520
|
-
const [error, setError] = (0, import_react8.useState)(null);
|
|
521
|
-
(0, import_react8.useEffect)(() => {
|
|
522
|
-
if (!isReady || !craft) return;
|
|
523
|
-
craft.getPlatform().then(setPlatform).catch(setError).finally(() => setLoading(false));
|
|
524
|
-
}, [craft, isReady]);
|
|
525
|
-
return { platform, loading, error };
|
|
526
|
-
}
|
|
527
|
-
function useDeviceInfo() {
|
|
528
|
-
const { craft, isReady } = useCraft2();
|
|
529
|
-
const [deviceInfo, setDeviceInfo] = (0, import_react8.useState)(null);
|
|
530
|
-
const [loading, setLoading] = (0, import_react8.useState)(true);
|
|
531
|
-
const [error, setError] = (0, import_react8.useState)(null);
|
|
532
|
-
(0, import_react8.useEffect)(() => {
|
|
533
|
-
if (!isReady || !craft) return;
|
|
534
|
-
craft.getDeviceInfo().then(setDeviceInfo).catch(setError).finally(() => setLoading(false));
|
|
535
|
-
}, [craft, isReady]);
|
|
536
|
-
return { deviceInfo, loading, error };
|
|
537
|
-
}
|
|
538
|
-
function useToast() {
|
|
539
|
-
const { craft, isReady } = useCraft2();
|
|
540
|
-
const showToast = (0, import_react8.useCallback)(
|
|
541
|
-
async (message, duration = "short") => {
|
|
542
|
-
if (!isReady || !craft) {
|
|
543
|
-
console.warn("Craft not ready");
|
|
544
|
-
return;
|
|
545
|
-
}
|
|
546
|
-
await craft.showToast(message, duration);
|
|
547
|
-
},
|
|
548
|
-
[craft, isReady]
|
|
549
|
-
);
|
|
550
|
-
return { showToast };
|
|
551
|
-
}
|
|
552
|
-
function useHaptic() {
|
|
553
|
-
const { craft, isReady } = useCraft2();
|
|
554
|
-
const haptic = (0, import_react8.useCallback)(
|
|
555
|
-
async (type = "selection") => {
|
|
556
|
-
if (!isReady || !craft) {
|
|
557
|
-
console.warn("Craft not ready");
|
|
558
|
-
return;
|
|
559
|
-
}
|
|
560
|
-
await craft.haptic(type);
|
|
561
|
-
},
|
|
562
|
-
[craft, isReady]
|
|
563
|
-
);
|
|
564
|
-
return { haptic };
|
|
565
|
-
}
|
|
566
|
-
function usePermission(permission) {
|
|
567
|
-
const { craft, isReady } = useCraft2();
|
|
568
|
-
const [granted, setGranted] = (0, import_react8.useState)(null);
|
|
569
|
-
const [loading, setLoading] = (0, import_react8.useState)(false);
|
|
570
|
-
const [error, setError] = (0, import_react8.useState)(null);
|
|
571
|
-
const request = (0, import_react8.useCallback)(async () => {
|
|
572
|
-
if (!isReady || !craft) {
|
|
573
|
-
console.warn("Craft not ready");
|
|
574
|
-
return;
|
|
575
|
-
}
|
|
576
|
-
setLoading(true);
|
|
577
|
-
try {
|
|
578
|
-
const result = await craft.requestPermission(permission);
|
|
579
|
-
setGranted(result.granted);
|
|
580
|
-
} catch (err) {
|
|
581
|
-
setError(err);
|
|
582
|
-
} finally {
|
|
583
|
-
setLoading(false);
|
|
584
|
-
}
|
|
585
|
-
}, [craft, isReady, permission]);
|
|
586
|
-
return { granted, request, loading, error };
|
|
587
|
-
}
|
|
588
|
-
function useCraftEvent(event, callback) {
|
|
589
|
-
const { craft, isReady } = useCraft2();
|
|
590
|
-
const callbackRef = (0, import_react8.useRef)(callback);
|
|
591
|
-
(0, import_react8.useEffect)(() => {
|
|
592
|
-
callbackRef.current = callback;
|
|
593
|
-
}, [callback]);
|
|
594
|
-
(0, import_react8.useEffect)(() => {
|
|
595
|
-
if (!isReady || !craft) return;
|
|
596
|
-
const handler = (...args) => callbackRef.current(...args);
|
|
597
|
-
craft.on(event, handler);
|
|
598
|
-
return () => {
|
|
599
|
-
craft.off(event, handler);
|
|
600
|
-
};
|
|
601
|
-
}, [craft, isReady, event]);
|
|
602
|
-
}
|
|
603
|
-
function useCraftInvoke() {
|
|
604
|
-
const { craft, isReady } = useCraft2();
|
|
605
|
-
const invoke = (0, import_react8.useCallback)(
|
|
606
|
-
async (method, params) => {
|
|
607
|
-
if (!isReady || !craft) {
|
|
608
|
-
throw new Error("Craft not ready");
|
|
609
|
-
}
|
|
610
|
-
return await craft.invoke(method, params);
|
|
611
|
-
},
|
|
612
|
-
[craft, isReady]
|
|
613
|
-
);
|
|
614
|
-
return { invoke, isReady };
|
|
615
|
-
}
|
|
616
|
-
function useIsMobile() {
|
|
617
|
-
const { platform } = usePlatform();
|
|
618
|
-
return (0, import_react8.useMemo)(() => {
|
|
619
|
-
if (!platform) return false;
|
|
620
|
-
return platform.platform === "ios" || platform.platform === "android";
|
|
621
|
-
}, [platform]);
|
|
622
|
-
}
|
|
623
|
-
function useIsDesktop() {
|
|
624
|
-
const { platform } = usePlatform();
|
|
625
|
-
return (0, import_react8.useMemo)(() => {
|
|
626
|
-
if (!platform) return false;
|
|
627
|
-
return platform.platform === "macos" || platform.platform === "windows" || platform.platform === "linux";
|
|
628
|
-
}, [platform]);
|
|
629
|
-
}
|
|
630
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
631
|
-
0 && (module.exports = {
|
|
632
|
-
useCraft,
|
|
633
|
-
useCraftEvent,
|
|
634
|
-
useCraftInvoke,
|
|
635
|
-
useDatabase,
|
|
636
|
-
useDeviceInfo,
|
|
637
|
-
useFileSystem,
|
|
638
|
-
useHaptic,
|
|
639
|
-
useHttp,
|
|
640
|
-
useIsDesktop,
|
|
641
|
-
useIsMobile,
|
|
642
|
-
useNotification,
|
|
643
|
-
usePermission,
|
|
644
|
-
usePlatform,
|
|
645
|
-
useToast,
|
|
646
|
-
useTray,
|
|
647
|
-
useWindow
|
|
648
|
-
});
|