@tusi-ai/plugins 0.0.0-stage → 0.0.1-beta.1
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/README.md +87 -2
- package/dist/calendar/index.cjs +119 -0
- package/dist/calendar/index.d.mts +30 -0
- package/dist/calendar/index.d.ts +30 -0
- package/dist/calendar/index.js +117 -0
- package/dist/camera/index.cjs +351 -0
- package/dist/camera/index.d.mts +98 -0
- package/dist/camera/index.d.ts +98 -0
- package/dist/camera/index.js +349 -0
- package/dist/clipboard/index.cjs +139 -0
- package/dist/clipboard/index.d.mts +41 -0
- package/dist/clipboard/index.d.ts +41 -0
- package/dist/clipboard/index.js +137 -0
- package/dist/contact-picker/index.cjs +100 -0
- package/dist/contact-picker/index.d.mts +37 -0
- package/dist/contact-picker/index.d.ts +37 -0
- package/dist/contact-picker/index.js +98 -0
- package/dist/filesystem/index.cjs +447 -0
- package/dist/filesystem/index.d.mts +202 -0
- package/dist/filesystem/index.d.ts +202 -0
- package/dist/filesystem/index.js +445 -0
- package/dist/geolocation/index.cjs +114 -0
- package/dist/geolocation/index.d.mts +49 -0
- package/dist/geolocation/index.d.ts +49 -0
- package/dist/geolocation/index.js +112 -0
- package/dist/index.cjs +40 -0
- package/dist/index.d.mts +29 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +38 -0
- package/dist/local-notifications/index.cjs +152 -0
- package/dist/local-notifications/index.d.mts +84 -0
- package/dist/local-notifications/index.d.ts +84 -0
- package/dist/local-notifications/index.js +150 -0
- package/dist/share/index.cjs +145 -0
- package/dist/share/index.d.mts +45 -0
- package/dist/share/index.d.ts +45 -0
- package/dist/share/index.js +143 -0
- package/package.json +123 -3
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import { getRuntimeEnv, RuntimeEnv, TusiError, ErrorCode, bridge } from '@tusi-ai/core';
|
|
2
|
+
|
|
3
|
+
var __async = (__this, __arguments, generator) => {
|
|
4
|
+
return new Promise((resolve, reject) => {
|
|
5
|
+
var fulfilled = (value) => {
|
|
6
|
+
try {
|
|
7
|
+
step(generator.next(value));
|
|
8
|
+
} catch (e) {
|
|
9
|
+
reject(e);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var rejected = (value) => {
|
|
13
|
+
try {
|
|
14
|
+
step(generator.throw(value));
|
|
15
|
+
} catch (e) {
|
|
16
|
+
reject(e);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
20
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
var BaseAdapter = class {
|
|
24
|
+
/**
|
|
25
|
+
* 绑定插件名,后续原生调用会拼成 `pluginName.method`。
|
|
26
|
+
*
|
|
27
|
+
* @param pluginName - JSB 插件名,例如 `Camera`
|
|
28
|
+
*/
|
|
29
|
+
constructor(pluginName) {
|
|
30
|
+
this.pluginName = pluginName;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* 调用 `pluginName.method`。只给原生适配器使用。
|
|
34
|
+
*
|
|
35
|
+
* @param method - 插件内的方法名,例如 `getPhoto`
|
|
36
|
+
* @param params - 原样交给 {@link bridge}
|
|
37
|
+
* @returns 客户端响应的 `data`
|
|
38
|
+
*/
|
|
39
|
+
callNative(method, params) {
|
|
40
|
+
return bridge.call(`${this.pluginName}.${method}`, params);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* 当前环境没有这个能力。在进入 bridge 之前抛出。
|
|
44
|
+
*
|
|
45
|
+
* @param method - 插件内的方法名
|
|
46
|
+
* @throws {@link TusiError} {@link ErrorCode.Unsupported}
|
|
47
|
+
*/
|
|
48
|
+
unsupported(method) {
|
|
49
|
+
throw new TusiError(
|
|
50
|
+
ErrorCode.Unsupported,
|
|
51
|
+
`${this.pluginName}.${method} is not supported`,
|
|
52
|
+
`${this.pluginName}.${method}`
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// src/camera/adapter.ts
|
|
58
|
+
var CameraAdapter = class extends BaseAdapter {
|
|
59
|
+
constructor() {
|
|
60
|
+
super("Camera");
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// src/camera/browser.ts
|
|
65
|
+
var BrowserCameraAdapter = class extends CameraAdapter {
|
|
66
|
+
/**
|
|
67
|
+
* 调起浏览器相机。已注册 `pwa-camera-modal` 时使用它,否则退回文件选择。
|
|
68
|
+
*
|
|
69
|
+
* @param options - 使用 `resultType` 和 `direction`
|
|
70
|
+
* @returns 按 `resultType` 填充的照片
|
|
71
|
+
* @throws {@link TusiError} {@link ErrorCode.Cancelled} 用户取消
|
|
72
|
+
*/
|
|
73
|
+
getPhoto(options) {
|
|
74
|
+
return new Promise((resolve, reject) => {
|
|
75
|
+
void this.cameraExperience(options, resolve, reject);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* 查询摄像头权限。相册在 Web 上按 Capacitor 记为 `granted`。
|
|
80
|
+
*
|
|
81
|
+
* @returns 两项权限状态
|
|
82
|
+
* @throws {@link TusiError} {@link ErrorCode.Unsupported},当前浏览器没有 Permissions API 或不能查询 `camera`
|
|
83
|
+
*/
|
|
84
|
+
checkPermissions() {
|
|
85
|
+
return __async(this, null, function* () {
|
|
86
|
+
if (typeof navigator === "undefined" || !navigator.permissions) {
|
|
87
|
+
throw new TusiError(
|
|
88
|
+
ErrorCode.Unsupported,
|
|
89
|
+
"Permissions API not available in this browser",
|
|
90
|
+
"Camera.checkPermissions"
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
try {
|
|
94
|
+
const permission = yield navigator.permissions.query({
|
|
95
|
+
name: "camera"
|
|
96
|
+
});
|
|
97
|
+
const camera = permission.state === "granted" || permission.state === "denied" ? permission.state : "prompt";
|
|
98
|
+
return {
|
|
99
|
+
camera,
|
|
100
|
+
photos: "granted"
|
|
101
|
+
};
|
|
102
|
+
} catch (e) {
|
|
103
|
+
throw new TusiError(
|
|
104
|
+
ErrorCode.Unsupported,
|
|
105
|
+
"Camera permissions are not available in this browser",
|
|
106
|
+
"Camera.checkPermissions"
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Web 上不实现授权弹窗,与 Capacitor 的 `requestPermissions` 一致。
|
|
113
|
+
*
|
|
114
|
+
* @param _permissions - 保留签名。Web 实现不读取它
|
|
115
|
+
* @throws {@link TusiError} {@link ErrorCode.Unsupported}
|
|
116
|
+
*/
|
|
117
|
+
requestPermissions(_permissions) {
|
|
118
|
+
return __async(this, null, function* () {
|
|
119
|
+
throw new TusiError(
|
|
120
|
+
ErrorCode.Unsupported,
|
|
121
|
+
"Not implemented on web.",
|
|
122
|
+
"Camera.requestPermissions"
|
|
123
|
+
);
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* 使用 `pwa-camera-modal` 拍照。元素未注册或初始化失败时改走文件选择。
|
|
128
|
+
*
|
|
129
|
+
* @param options - 决定朝向和返回形态
|
|
130
|
+
* @param resolve - 成功回调
|
|
131
|
+
* @param reject - 失败回调
|
|
132
|
+
*/
|
|
133
|
+
cameraExperience(options, resolve, reject) {
|
|
134
|
+
return __async(this, null, function* () {
|
|
135
|
+
const cameraModal = this.createCameraModal(options.direction);
|
|
136
|
+
if (!cameraModal) {
|
|
137
|
+
this.fileInputExperience(options, resolve, reject);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
document.body.appendChild(cameraModal);
|
|
141
|
+
try {
|
|
142
|
+
yield cameraModal.componentOnReady();
|
|
143
|
+
cameraModal.addEventListener("onPhoto", (event) => {
|
|
144
|
+
const photo = event.detail;
|
|
145
|
+
if (photo === null) {
|
|
146
|
+
reject(new TusiError(ErrorCode.Cancelled, "User cancelled photos app", "Camera.getPhoto"));
|
|
147
|
+
} else if (photo instanceof Error) {
|
|
148
|
+
reject(photo);
|
|
149
|
+
} else {
|
|
150
|
+
void this.readCameraPhoto(photo, options).then(resolve, reject);
|
|
151
|
+
}
|
|
152
|
+
cameraModal.dismiss();
|
|
153
|
+
document.body.removeChild(cameraModal);
|
|
154
|
+
});
|
|
155
|
+
cameraModal.present();
|
|
156
|
+
} catch (e) {
|
|
157
|
+
this.fileInputExperience(options, resolve, reject);
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* 找到已注册的 `pwa-camera-modal`。
|
|
163
|
+
*
|
|
164
|
+
* @param direction - `FRONT` 使用前置,其余使用后置
|
|
165
|
+
* @returns 模态元素。浏览器没有该自定义元素时返回 `undefined`
|
|
166
|
+
*/
|
|
167
|
+
createCameraModal(direction) {
|
|
168
|
+
if (typeof customElements === "undefined" || !customElements.get("pwa-camera-modal")) {
|
|
169
|
+
return void 0;
|
|
170
|
+
}
|
|
171
|
+
const cameraModal = document.createElement("pwa-camera-modal");
|
|
172
|
+
cameraModal.facingMode = direction === "FRONT" ? "user" : "environment";
|
|
173
|
+
return cameraModal;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* 用隐藏的文件选择器拍照,逻辑与 Capacitor 的 `fileInputExperience` 相同。
|
|
177
|
+
*
|
|
178
|
+
* @param options - 决定 `capture` 和返回形态
|
|
179
|
+
* @param resolve - 成功回调
|
|
180
|
+
* @param reject - 用户取消时回调
|
|
181
|
+
*/
|
|
182
|
+
fileInputExperience(options, resolve, reject) {
|
|
183
|
+
let input = document.querySelector("#_capacitor-camera-input");
|
|
184
|
+
const cleanup = () => {
|
|
185
|
+
var _a;
|
|
186
|
+
(_a = input == null ? void 0 : input.parentNode) == null ? void 0 : _a.removeChild(input);
|
|
187
|
+
};
|
|
188
|
+
if (!input) {
|
|
189
|
+
input = document.createElement("input");
|
|
190
|
+
input.id = "_capacitor-camera-input";
|
|
191
|
+
input.type = "file";
|
|
192
|
+
input.hidden = true;
|
|
193
|
+
document.body.appendChild(input);
|
|
194
|
+
input.addEventListener("change", () => {
|
|
195
|
+
var _a;
|
|
196
|
+
const file = (_a = input == null ? void 0 : input.files) == null ? void 0 : _a[0];
|
|
197
|
+
if (!file) {
|
|
198
|
+
reject(new TusiError(ErrorCode.Cancelled, "User cancelled photos app", "Camera.getPhoto"));
|
|
199
|
+
cleanup();
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
let format = "jpeg";
|
|
203
|
+
if (file.type === "image/png") {
|
|
204
|
+
format = "png";
|
|
205
|
+
} else if (file.type === "image/gif") {
|
|
206
|
+
format = "gif";
|
|
207
|
+
}
|
|
208
|
+
if (options.resultType === "dataUrl" || options.resultType === "base64") {
|
|
209
|
+
const reader = new FileReader();
|
|
210
|
+
reader.addEventListener("load", () => {
|
|
211
|
+
var _a2, _b;
|
|
212
|
+
const dataUrl = String((_a2 = reader.result) != null ? _a2 : "");
|
|
213
|
+
if (options.resultType === "dataUrl") {
|
|
214
|
+
resolve({ dataUrl, format });
|
|
215
|
+
} else {
|
|
216
|
+
resolve({
|
|
217
|
+
base64String: (_b = dataUrl.split(",")[1]) != null ? _b : "",
|
|
218
|
+
format
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
cleanup();
|
|
222
|
+
});
|
|
223
|
+
reader.readAsDataURL(file);
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
resolve({
|
|
227
|
+
webPath: URL.createObjectURL(file),
|
|
228
|
+
format
|
|
229
|
+
});
|
|
230
|
+
cleanup();
|
|
231
|
+
});
|
|
232
|
+
input.addEventListener("cancel", () => {
|
|
233
|
+
reject(new TusiError(ErrorCode.Cancelled, "User cancelled photos app", "Camera.getPhoto"));
|
|
234
|
+
cleanup();
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
input.accept = "image/*";
|
|
238
|
+
input.capture = options.direction === "FRONT" ? "user" : "environment";
|
|
239
|
+
input.click();
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* 把 `pwa-camera-modal` 给出的 Blob 转成 {@link Photo}。
|
|
243
|
+
*
|
|
244
|
+
* @param photo - 模态返回的图片数据
|
|
245
|
+
* @param options - 决定返回 `webPath`、`dataUrl` 还是 `base64String`
|
|
246
|
+
* @returns 带格式的照片
|
|
247
|
+
*/
|
|
248
|
+
readCameraPhoto(photo, options) {
|
|
249
|
+
return new Promise((resolve, reject) => {
|
|
250
|
+
var _a;
|
|
251
|
+
const format = (_a = photo.type.split("/")[1]) != null ? _a : "jpeg";
|
|
252
|
+
if (options.resultType === "uri") {
|
|
253
|
+
resolve({
|
|
254
|
+
webPath: URL.createObjectURL(photo),
|
|
255
|
+
format
|
|
256
|
+
});
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
const reader = new FileReader();
|
|
260
|
+
reader.onloadend = () => {
|
|
261
|
+
var _a2, _b;
|
|
262
|
+
const dataUrl = String((_a2 = reader.result) != null ? _a2 : "");
|
|
263
|
+
if (options.resultType === "dataUrl") {
|
|
264
|
+
resolve({ dataUrl, format });
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
resolve({
|
|
268
|
+
base64String: (_b = dataUrl.split(",")[1]) != null ? _b : "",
|
|
269
|
+
format
|
|
270
|
+
});
|
|
271
|
+
};
|
|
272
|
+
reader.onerror = () => {
|
|
273
|
+
reject(new TusiError(ErrorCode.SchemeFailed, "Failed to read photo", "Camera.getPhoto"));
|
|
274
|
+
};
|
|
275
|
+
reader.readAsDataURL(photo);
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
// src/camera/native.ts
|
|
281
|
+
var NativeCameraAdapter = class extends CameraAdapter {
|
|
282
|
+
getPhoto(options) {
|
|
283
|
+
return this.callNative("getPhoto", options);
|
|
284
|
+
}
|
|
285
|
+
checkPermissions() {
|
|
286
|
+
return this.callNative("checkPermissions");
|
|
287
|
+
}
|
|
288
|
+
requestPermissions(permissions) {
|
|
289
|
+
return this.callNative("requestPermissions", permissions);
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
// src/camera/index.ts
|
|
294
|
+
var adapter;
|
|
295
|
+
function getAdapter() {
|
|
296
|
+
if (!adapter) {
|
|
297
|
+
adapter = getRuntimeEnv() === RuntimeEnv.Browser ? new BrowserCameraAdapter() : new NativeCameraAdapter();
|
|
298
|
+
}
|
|
299
|
+
return adapter;
|
|
300
|
+
}
|
|
301
|
+
function validateOptions(options) {
|
|
302
|
+
const resultTypes = ["uri", "base64", "dataUrl"];
|
|
303
|
+
if (!options || !resultTypes.includes(options.resultType)) {
|
|
304
|
+
throw new TusiError(ErrorCode.InvalidParams, "resultType is required", "Camera.getPhoto");
|
|
305
|
+
}
|
|
306
|
+
if (options.source !== void 0 && options.source !== "CAMERA") {
|
|
307
|
+
throw new TusiError(ErrorCode.InvalidParams, "source must be CAMERA", "Camera.getPhoto");
|
|
308
|
+
}
|
|
309
|
+
if (options.quality !== void 0 && (options.quality < 0 || options.quality > 100)) {
|
|
310
|
+
throw new TusiError(ErrorCode.InvalidParams, "quality must be between 0 and 100", "Camera.getPhoto");
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
var Camera = {
|
|
314
|
+
/**
|
|
315
|
+
* 调起相机并返回一张照片。
|
|
316
|
+
*
|
|
317
|
+
* @param options - `resultType` 必填。`source` 如果出现,只能是 `CAMERA`
|
|
318
|
+
* @returns 照片。`uri` 使用可加载的 `webPath`,大图不要走 base64
|
|
319
|
+
* @throws {@link TusiError}
|
|
320
|
+
* {@link ErrorCode.InvalidParams} 参数不合法,{@link ErrorCode.Cancelled} 用户取消。权限不足导致拍不了时为 {@link ErrorCode.PermissionDenied}
|
|
321
|
+
*/
|
|
322
|
+
getPhoto(options) {
|
|
323
|
+
try {
|
|
324
|
+
validateOptions(options);
|
|
325
|
+
} catch (error) {
|
|
326
|
+
return Promise.reject(error);
|
|
327
|
+
}
|
|
328
|
+
return getAdapter().getPhoto(options);
|
|
329
|
+
},
|
|
330
|
+
/**
|
|
331
|
+
* 查询相机和相册权限,不弹出授权框。
|
|
332
|
+
*
|
|
333
|
+
* @returns 两项权限的当前状态
|
|
334
|
+
*/
|
|
335
|
+
checkPermissions() {
|
|
336
|
+
return getAdapter().checkPermissions();
|
|
337
|
+
},
|
|
338
|
+
/**
|
|
339
|
+
* 申请相机或相册权限。
|
|
340
|
+
*
|
|
341
|
+
* @param permissions - 申请范围。不传时包含 `camera` 和 `photos`
|
|
342
|
+
* @returns 申请结束后的状态。拒绝写在 `camera` 或 `photos` 字段里
|
|
343
|
+
*/
|
|
344
|
+
requestPermissions(permissions) {
|
|
345
|
+
return getAdapter().requestPermissions(permissions);
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
export { Camera };
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var core = require('@tusi-ai/core');
|
|
4
|
+
|
|
5
|
+
var __async = (__this, __arguments, generator) => {
|
|
6
|
+
return new Promise((resolve, reject) => {
|
|
7
|
+
var fulfilled = (value) => {
|
|
8
|
+
try {
|
|
9
|
+
step(generator.next(value));
|
|
10
|
+
} catch (e) {
|
|
11
|
+
reject(e);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
var rejected = (value) => {
|
|
15
|
+
try {
|
|
16
|
+
step(generator.throw(value));
|
|
17
|
+
} catch (e) {
|
|
18
|
+
reject(e);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
22
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
var BaseAdapter = class {
|
|
26
|
+
/**
|
|
27
|
+
* 绑定插件名,后续原生调用会拼成 `pluginName.method`。
|
|
28
|
+
*
|
|
29
|
+
* @param pluginName - JSB 插件名,例如 `Camera`
|
|
30
|
+
*/
|
|
31
|
+
constructor(pluginName) {
|
|
32
|
+
this.pluginName = pluginName;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* 调用 `pluginName.method`。只给原生适配器使用。
|
|
36
|
+
*
|
|
37
|
+
* @param method - 插件内的方法名,例如 `getPhoto`
|
|
38
|
+
* @param params - 原样交给 {@link bridge}
|
|
39
|
+
* @returns 客户端响应的 `data`
|
|
40
|
+
*/
|
|
41
|
+
callNative(method, params) {
|
|
42
|
+
return core.bridge.call(`${this.pluginName}.${method}`, params);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* 当前环境没有这个能力。在进入 bridge 之前抛出。
|
|
46
|
+
*
|
|
47
|
+
* @param method - 插件内的方法名
|
|
48
|
+
* @throws {@link TusiError} {@link ErrorCode.Unsupported}
|
|
49
|
+
*/
|
|
50
|
+
unsupported(method) {
|
|
51
|
+
throw new core.TusiError(
|
|
52
|
+
core.ErrorCode.Unsupported,
|
|
53
|
+
`${this.pluginName}.${method} is not supported`,
|
|
54
|
+
`${this.pluginName}.${method}`
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// src/clipboard/adapter.ts
|
|
60
|
+
var ClipboardAdapter = class extends BaseAdapter {
|
|
61
|
+
constructor() {
|
|
62
|
+
super("Clipboard");
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// src/clipboard/browser.ts
|
|
67
|
+
var BrowserClipboardAdapter = class extends ClipboardAdapter {
|
|
68
|
+
write(options) {
|
|
69
|
+
return __async(this, null, function* () {
|
|
70
|
+
var _a, _b;
|
|
71
|
+
const value = (_b = (_a = options.image) != null ? _a : options.url) != null ? _b : options.string;
|
|
72
|
+
if (value === void 0) {
|
|
73
|
+
throw new core.TusiError(core.ErrorCode.InvalidParams, "string, image or url is required", "Clipboard.write");
|
|
74
|
+
}
|
|
75
|
+
if (!clipboard()) this.unsupported("write");
|
|
76
|
+
yield navigator.clipboard.writeText(value);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
read() {
|
|
80
|
+
return __async(this, null, function* () {
|
|
81
|
+
if (!clipboard()) this.unsupported("read");
|
|
82
|
+
try {
|
|
83
|
+
const value = yield navigator.clipboard.readText();
|
|
84
|
+
if (!value) {
|
|
85
|
+
throw new core.TusiError(core.ErrorCode.NotFound, "Clipboard is empty", "Clipboard.read");
|
|
86
|
+
}
|
|
87
|
+
return { value, type: "text/plain" };
|
|
88
|
+
} catch (error) {
|
|
89
|
+
if (error instanceof core.TusiError) throw error;
|
|
90
|
+
throw new core.TusiError(core.ErrorCode.NotFound, "Clipboard read failed", "Clipboard.read", error);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
function clipboard() {
|
|
96
|
+
return typeof navigator !== "undefined" && !!navigator.clipboard;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// src/clipboard/native.ts
|
|
100
|
+
var NativeClipboardAdapter = class extends ClipboardAdapter {
|
|
101
|
+
write(options) {
|
|
102
|
+
return this.callNative("write", options);
|
|
103
|
+
}
|
|
104
|
+
read() {
|
|
105
|
+
return this.callNative("read");
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
// src/clipboard/index.ts
|
|
110
|
+
var adapter;
|
|
111
|
+
function getAdapter() {
|
|
112
|
+
adapter != null ? adapter : adapter = core.getRuntimeEnv() === core.RuntimeEnv.Browser ? new BrowserClipboardAdapter() : new NativeClipboardAdapter();
|
|
113
|
+
return adapter;
|
|
114
|
+
}
|
|
115
|
+
var Clipboard = {
|
|
116
|
+
/**
|
|
117
|
+
* 写入剪贴板。
|
|
118
|
+
*
|
|
119
|
+
* @param options - `image`、`url`、`string` 三选一,优先级依次降低
|
|
120
|
+
* @throws {@link TusiError}
|
|
121
|
+
* {@link ErrorCode.InvalidParams} 没有可写内容,{@link ErrorCode.Unsupported} 当前环境没有剪贴板 API
|
|
122
|
+
*/
|
|
123
|
+
write(options) {
|
|
124
|
+
return __async(this, null, function* () {
|
|
125
|
+
return getAdapter().write(options);
|
|
126
|
+
});
|
|
127
|
+
},
|
|
128
|
+
/**
|
|
129
|
+
* 读取剪贴板。
|
|
130
|
+
*
|
|
131
|
+
* @returns 内容和类型
|
|
132
|
+
* @throws {@link TusiError} {@link ErrorCode.NotFound} 剪贴板为空或没有读取权限
|
|
133
|
+
*/
|
|
134
|
+
read() {
|
|
135
|
+
return getAdapter().read();
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
exports.Clipboard = Clipboard;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/** {@link Clipboard.write} 的入参。`image`、`url`、`string` 三选一,优先级依次降低。 */
|
|
2
|
+
interface WriteOptions {
|
|
3
|
+
/** 要复制的文本。 */
|
|
4
|
+
string?: string;
|
|
5
|
+
/** 要复制的图片,data URL。 */
|
|
6
|
+
image?: string;
|
|
7
|
+
/** 要复制的链接。 */
|
|
8
|
+
url?: string;
|
|
9
|
+
/** 复制内容的可见标签。只在 Android 使用。 */
|
|
10
|
+
label?: string;
|
|
11
|
+
}
|
|
12
|
+
/** {@link Clipboard.read} 的结果。 */
|
|
13
|
+
interface ReadResult {
|
|
14
|
+
/** 剪贴板内容。 */
|
|
15
|
+
value: string;
|
|
16
|
+
/** 内容类型。 */
|
|
17
|
+
type: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 剪贴板插件。浏览器使用 Clipboard API,包壳读写系统剪贴板。
|
|
22
|
+
*/
|
|
23
|
+
declare const Clipboard: {
|
|
24
|
+
/**
|
|
25
|
+
* 写入剪贴板。
|
|
26
|
+
*
|
|
27
|
+
* @param options - `image`、`url`、`string` 三选一,优先级依次降低
|
|
28
|
+
* @throws {@link TusiError}
|
|
29
|
+
* {@link ErrorCode.InvalidParams} 没有可写内容,{@link ErrorCode.Unsupported} 当前环境没有剪贴板 API
|
|
30
|
+
*/
|
|
31
|
+
write(options: WriteOptions): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* 读取剪贴板。
|
|
34
|
+
*
|
|
35
|
+
* @returns 内容和类型
|
|
36
|
+
* @throws {@link TusiError} {@link ErrorCode.NotFound} 剪贴板为空或没有读取权限
|
|
37
|
+
*/
|
|
38
|
+
read(): Promise<ReadResult>;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export { Clipboard, type ReadResult, type WriteOptions };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/** {@link Clipboard.write} 的入参。`image`、`url`、`string` 三选一,优先级依次降低。 */
|
|
2
|
+
interface WriteOptions {
|
|
3
|
+
/** 要复制的文本。 */
|
|
4
|
+
string?: string;
|
|
5
|
+
/** 要复制的图片,data URL。 */
|
|
6
|
+
image?: string;
|
|
7
|
+
/** 要复制的链接。 */
|
|
8
|
+
url?: string;
|
|
9
|
+
/** 复制内容的可见标签。只在 Android 使用。 */
|
|
10
|
+
label?: string;
|
|
11
|
+
}
|
|
12
|
+
/** {@link Clipboard.read} 的结果。 */
|
|
13
|
+
interface ReadResult {
|
|
14
|
+
/** 剪贴板内容。 */
|
|
15
|
+
value: string;
|
|
16
|
+
/** 内容类型。 */
|
|
17
|
+
type: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 剪贴板插件。浏览器使用 Clipboard API,包壳读写系统剪贴板。
|
|
22
|
+
*/
|
|
23
|
+
declare const Clipboard: {
|
|
24
|
+
/**
|
|
25
|
+
* 写入剪贴板。
|
|
26
|
+
*
|
|
27
|
+
* @param options - `image`、`url`、`string` 三选一,优先级依次降低
|
|
28
|
+
* @throws {@link TusiError}
|
|
29
|
+
* {@link ErrorCode.InvalidParams} 没有可写内容,{@link ErrorCode.Unsupported} 当前环境没有剪贴板 API
|
|
30
|
+
*/
|
|
31
|
+
write(options: WriteOptions): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* 读取剪贴板。
|
|
34
|
+
*
|
|
35
|
+
* @returns 内容和类型
|
|
36
|
+
* @throws {@link TusiError} {@link ErrorCode.NotFound} 剪贴板为空或没有读取权限
|
|
37
|
+
*/
|
|
38
|
+
read(): Promise<ReadResult>;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export { Clipboard, type ReadResult, type WriteOptions };
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { getRuntimeEnv, RuntimeEnv, TusiError, ErrorCode, bridge } from '@tusi-ai/core';
|
|
2
|
+
|
|
3
|
+
var __async = (__this, __arguments, generator) => {
|
|
4
|
+
return new Promise((resolve, reject) => {
|
|
5
|
+
var fulfilled = (value) => {
|
|
6
|
+
try {
|
|
7
|
+
step(generator.next(value));
|
|
8
|
+
} catch (e) {
|
|
9
|
+
reject(e);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var rejected = (value) => {
|
|
13
|
+
try {
|
|
14
|
+
step(generator.throw(value));
|
|
15
|
+
} catch (e) {
|
|
16
|
+
reject(e);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
20
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
var BaseAdapter = class {
|
|
24
|
+
/**
|
|
25
|
+
* 绑定插件名,后续原生调用会拼成 `pluginName.method`。
|
|
26
|
+
*
|
|
27
|
+
* @param pluginName - JSB 插件名,例如 `Camera`
|
|
28
|
+
*/
|
|
29
|
+
constructor(pluginName) {
|
|
30
|
+
this.pluginName = pluginName;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* 调用 `pluginName.method`。只给原生适配器使用。
|
|
34
|
+
*
|
|
35
|
+
* @param method - 插件内的方法名,例如 `getPhoto`
|
|
36
|
+
* @param params - 原样交给 {@link bridge}
|
|
37
|
+
* @returns 客户端响应的 `data`
|
|
38
|
+
*/
|
|
39
|
+
callNative(method, params) {
|
|
40
|
+
return bridge.call(`${this.pluginName}.${method}`, params);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* 当前环境没有这个能力。在进入 bridge 之前抛出。
|
|
44
|
+
*
|
|
45
|
+
* @param method - 插件内的方法名
|
|
46
|
+
* @throws {@link TusiError} {@link ErrorCode.Unsupported}
|
|
47
|
+
*/
|
|
48
|
+
unsupported(method) {
|
|
49
|
+
throw new TusiError(
|
|
50
|
+
ErrorCode.Unsupported,
|
|
51
|
+
`${this.pluginName}.${method} is not supported`,
|
|
52
|
+
`${this.pluginName}.${method}`
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// src/clipboard/adapter.ts
|
|
58
|
+
var ClipboardAdapter = class extends BaseAdapter {
|
|
59
|
+
constructor() {
|
|
60
|
+
super("Clipboard");
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// src/clipboard/browser.ts
|
|
65
|
+
var BrowserClipboardAdapter = class extends ClipboardAdapter {
|
|
66
|
+
write(options) {
|
|
67
|
+
return __async(this, null, function* () {
|
|
68
|
+
var _a, _b;
|
|
69
|
+
const value = (_b = (_a = options.image) != null ? _a : options.url) != null ? _b : options.string;
|
|
70
|
+
if (value === void 0) {
|
|
71
|
+
throw new TusiError(ErrorCode.InvalidParams, "string, image or url is required", "Clipboard.write");
|
|
72
|
+
}
|
|
73
|
+
if (!clipboard()) this.unsupported("write");
|
|
74
|
+
yield navigator.clipboard.writeText(value);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
read() {
|
|
78
|
+
return __async(this, null, function* () {
|
|
79
|
+
if (!clipboard()) this.unsupported("read");
|
|
80
|
+
try {
|
|
81
|
+
const value = yield navigator.clipboard.readText();
|
|
82
|
+
if (!value) {
|
|
83
|
+
throw new TusiError(ErrorCode.NotFound, "Clipboard is empty", "Clipboard.read");
|
|
84
|
+
}
|
|
85
|
+
return { value, type: "text/plain" };
|
|
86
|
+
} catch (error) {
|
|
87
|
+
if (error instanceof TusiError) throw error;
|
|
88
|
+
throw new TusiError(ErrorCode.NotFound, "Clipboard read failed", "Clipboard.read", error);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
function clipboard() {
|
|
94
|
+
return typeof navigator !== "undefined" && !!navigator.clipboard;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// src/clipboard/native.ts
|
|
98
|
+
var NativeClipboardAdapter = class extends ClipboardAdapter {
|
|
99
|
+
write(options) {
|
|
100
|
+
return this.callNative("write", options);
|
|
101
|
+
}
|
|
102
|
+
read() {
|
|
103
|
+
return this.callNative("read");
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
// src/clipboard/index.ts
|
|
108
|
+
var adapter;
|
|
109
|
+
function getAdapter() {
|
|
110
|
+
adapter != null ? adapter : adapter = getRuntimeEnv() === RuntimeEnv.Browser ? new BrowserClipboardAdapter() : new NativeClipboardAdapter();
|
|
111
|
+
return adapter;
|
|
112
|
+
}
|
|
113
|
+
var Clipboard = {
|
|
114
|
+
/**
|
|
115
|
+
* 写入剪贴板。
|
|
116
|
+
*
|
|
117
|
+
* @param options - `image`、`url`、`string` 三选一,优先级依次降低
|
|
118
|
+
* @throws {@link TusiError}
|
|
119
|
+
* {@link ErrorCode.InvalidParams} 没有可写内容,{@link ErrorCode.Unsupported} 当前环境没有剪贴板 API
|
|
120
|
+
*/
|
|
121
|
+
write(options) {
|
|
122
|
+
return __async(this, null, function* () {
|
|
123
|
+
return getAdapter().write(options);
|
|
124
|
+
});
|
|
125
|
+
},
|
|
126
|
+
/**
|
|
127
|
+
* 读取剪贴板。
|
|
128
|
+
*
|
|
129
|
+
* @returns 内容和类型
|
|
130
|
+
* @throws {@link TusiError} {@link ErrorCode.NotFound} 剪贴板为空或没有读取权限
|
|
131
|
+
*/
|
|
132
|
+
read() {
|
|
133
|
+
return getAdapter().read();
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
export { Clipboard };
|