@whitesev/utils 2.6.6 → 2.6.8
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/dist/index.amd.js +389 -14
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +389 -14
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +389 -14
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +389 -14
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +389 -14
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +389 -14
- package/dist/index.umd.js.map +1 -1
- package/dist/types/src/Utils.d.ts +35 -0
- package/package.json +3 -2
- package/src/LockFunction.ts +1 -1
- package/src/Utils.ts +155 -13
|
@@ -1825,6 +1825,41 @@ declare class Utils {
|
|
|
1825
1825
|
*/
|
|
1826
1826
|
Vue: typeof Vue;
|
|
1827
1827
|
ModuleRaid: typeof ModuleRaid;
|
|
1828
|
+
/**
|
|
1829
|
+
* 自动使用 Worker 执行 setTimeout
|
|
1830
|
+
* @param callback 回调函数
|
|
1831
|
+
* @param [timeout=0] 延迟时间,默认为0
|
|
1832
|
+
*/
|
|
1833
|
+
workerSetTimeout(callback: Function, timeout?: number): number;
|
|
1834
|
+
/**
|
|
1835
|
+
* 配合 .setTimeout 使用
|
|
1836
|
+
* @param timeId setTimeout 返回的`id`
|
|
1837
|
+
*/
|
|
1838
|
+
workerClearTimeout(timeId: number | undefined): void;
|
|
1839
|
+
/**
|
|
1840
|
+
* 自动使用 Worker 执行 setInterval
|
|
1841
|
+
* @param callback 回调函数
|
|
1842
|
+
* @param timeout 间隔时间,默认为0
|
|
1843
|
+
*/
|
|
1844
|
+
workerSetInterval(callback: Function, timeout?: number): number;
|
|
1845
|
+
/**
|
|
1846
|
+
* 配合 .setInterval 使用
|
|
1847
|
+
* @param timeId setInterval 返回的`id`
|
|
1848
|
+
*/
|
|
1849
|
+
workerClearInterval(timeId: number | undefined): void;
|
|
1850
|
+
/**
|
|
1851
|
+
* 获取剪贴板信息
|
|
1852
|
+
*/
|
|
1853
|
+
getClipboardInfo(): Promise<{
|
|
1854
|
+
/**
|
|
1855
|
+
* 错误信息,如果为null,则表示读取成功
|
|
1856
|
+
*/
|
|
1857
|
+
error: Error | null;
|
|
1858
|
+
/**
|
|
1859
|
+
* 剪贴板内容
|
|
1860
|
+
*/
|
|
1861
|
+
content: string;
|
|
1862
|
+
}>;
|
|
1828
1863
|
}
|
|
1829
1864
|
declare let utils: Utils;
|
|
1830
1865
|
export { utils as Utils };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@whitesev/utils",
|
|
3
|
-
"version": "2.6.
|
|
3
|
+
"version": "2.6.8",
|
|
4
4
|
"description": "一个常用的工具库",
|
|
5
5
|
"main": "dist/index.cjs.js",
|
|
6
6
|
"module": "dist/index.esm.js",
|
|
@@ -38,7 +38,8 @@
|
|
|
38
38
|
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
39
39
|
"@rollup/plugin-typescript": "^11.1.6",
|
|
40
40
|
"rollup-plugin-clear": "^2.0.7",
|
|
41
|
-
"tslib": "^2.6.3"
|
|
41
|
+
"tslib": "^2.6.3",
|
|
42
|
+
"worker-timers": "^8.0.21"
|
|
42
43
|
},
|
|
43
44
|
"devDependencies": {
|
|
44
45
|
"typescript": "^5.5.4"
|
package/src/LockFunction.ts
CHANGED
package/src/Utils.ts
CHANGED
|
@@ -23,6 +23,12 @@ import {
|
|
|
23
23
|
type UtilsOwnObject,
|
|
24
24
|
} from "./types/global";
|
|
25
25
|
import type { WindowApiOption } from "./types/WindowApi";
|
|
26
|
+
import {
|
|
27
|
+
clearInterval as WorkerClearInterval,
|
|
28
|
+
clearTimeout as WorkerClearTimeout,
|
|
29
|
+
setInterval as WorkerSetInterval,
|
|
30
|
+
setTimeout as WorkerSetTimeout,
|
|
31
|
+
} from "worker-timers";
|
|
26
32
|
import { ModuleRaid } from "./ModuleRaid";
|
|
27
33
|
|
|
28
34
|
class Utils {
|
|
@@ -31,7 +37,7 @@ class Utils {
|
|
|
31
37
|
this.windowApi = new WindowApi(option);
|
|
32
38
|
}
|
|
33
39
|
/** 版本号 */
|
|
34
|
-
version = "2025.
|
|
40
|
+
version = "2025.5.28";
|
|
35
41
|
|
|
36
42
|
/**
|
|
37
43
|
* 在页面中增加style元素,如果html节点存在子节点,添加子节点第一个,反之,添加到html节点的子节点最后一个
|
|
@@ -421,11 +427,11 @@ class Utils {
|
|
|
421
427
|
): (...args: A) => void;
|
|
422
428
|
debounce<A extends any[], R>(fn: (...args: A) => R, delay = 0) {
|
|
423
429
|
let timer: any = null as any;
|
|
424
|
-
|
|
430
|
+
let UtilsContext = this;
|
|
425
431
|
return function (...args: A) {
|
|
426
|
-
|
|
427
|
-
timer =
|
|
428
|
-
fn.apply(
|
|
432
|
+
UtilsContext.workerClearTimeout(timer);
|
|
433
|
+
timer = UtilsContext.workerSetTimeout(function () {
|
|
434
|
+
fn.apply(UtilsContext, args);
|
|
429
435
|
}, delay);
|
|
430
436
|
};
|
|
431
437
|
}
|
|
@@ -519,6 +525,7 @@ class Utils {
|
|
|
519
525
|
eventName: DOMUtils_EventType | DOMUtils_EventType[] | string,
|
|
520
526
|
details?: any
|
|
521
527
|
) {
|
|
528
|
+
// let UtilsContext = this;
|
|
522
529
|
let eventNameList: string[] = [];
|
|
523
530
|
if (typeof eventName === "string") {
|
|
524
531
|
eventNameList = [eventName];
|
|
@@ -548,6 +555,7 @@ class Utils {
|
|
|
548
555
|
isIFrame?: boolean
|
|
549
556
|
): void;
|
|
550
557
|
downloadBase64(base64Data: string, fileName: string, isIFrame = false) {
|
|
558
|
+
let UtilsContext = this;
|
|
551
559
|
if (typeof base64Data !== "string") {
|
|
552
560
|
throw new Error(
|
|
553
561
|
"Utils.downloadBase64 参数 base64Data 必须为 string 类型"
|
|
@@ -562,7 +570,7 @@ class Utils {
|
|
|
562
570
|
iframeElement.style.display = "none";
|
|
563
571
|
iframeElement.src = base64Data;
|
|
564
572
|
this.windowApi.document.body.appendChild(iframeElement);
|
|
565
|
-
|
|
573
|
+
UtilsContext.workerSetTimeout(() => {
|
|
566
574
|
iframeElement!.contentWindow!.document.execCommand(
|
|
567
575
|
"SaveAs",
|
|
568
576
|
true,
|
|
@@ -3507,7 +3515,7 @@ class Utils {
|
|
|
3507
3515
|
throw new TypeError("Utils.setTimeout 参数 delayTime 必须为 number 类型");
|
|
3508
3516
|
}
|
|
3509
3517
|
return new Promise((resolve) => {
|
|
3510
|
-
|
|
3518
|
+
UtilsContext.workerSetTimeout(() => {
|
|
3511
3519
|
resolve(UtilsContext.tryCatch().run(callback));
|
|
3512
3520
|
}, delayTime);
|
|
3513
3521
|
});
|
|
@@ -3520,11 +3528,12 @@ class Utils {
|
|
|
3520
3528
|
**/
|
|
3521
3529
|
sleep(delayTime?: number): Promise<void>;
|
|
3522
3530
|
sleep(delayTime: number = 0): Promise<void> {
|
|
3531
|
+
let UtilsContext = this;
|
|
3523
3532
|
if (typeof delayTime !== "number") {
|
|
3524
3533
|
throw new Error("Utils.sleep 参数 delayTime 必须为 number 类型");
|
|
3525
3534
|
}
|
|
3526
3535
|
return new Promise((resolve) => {
|
|
3527
|
-
|
|
3536
|
+
UtilsContext.workerSetTimeout(() => {
|
|
3528
3537
|
resolve(void 0);
|
|
3529
3538
|
}, delayTime);
|
|
3530
3539
|
});
|
|
@@ -4188,7 +4197,7 @@ class Utils {
|
|
|
4188
4197
|
}
|
|
4189
4198
|
);
|
|
4190
4199
|
if (__timeout__ > 0) {
|
|
4191
|
-
|
|
4200
|
+
UtilsContext.workerSetTimeout(() => {
|
|
4192
4201
|
// 取消观察器
|
|
4193
4202
|
if (typeof observer?.disconnect === "function") {
|
|
4194
4203
|
observer.disconnect();
|
|
@@ -4913,12 +4922,13 @@ class Utils {
|
|
|
4913
4922
|
intervalTimer: number = 250,
|
|
4914
4923
|
maxTime: number = -1
|
|
4915
4924
|
): Promise<T> {
|
|
4925
|
+
let UtilsContext = this;
|
|
4916
4926
|
if (checkObj == null) {
|
|
4917
4927
|
throw new TypeError("checkObj 不能为空对象 ");
|
|
4918
4928
|
}
|
|
4919
4929
|
let isResolve = false;
|
|
4920
4930
|
return new Promise((resolve, reject) => {
|
|
4921
|
-
let interval =
|
|
4931
|
+
let interval = UtilsContext.workerSetInterval(() => {
|
|
4922
4932
|
let obj = checkObj;
|
|
4923
4933
|
if (typeof checkObj === "function") {
|
|
4924
4934
|
obj = checkObj();
|
|
@@ -4934,14 +4944,14 @@ class Utils {
|
|
|
4934
4944
|
Reflect.has(obj, checkPropertyName as string)
|
|
4935
4945
|
) {
|
|
4936
4946
|
isResolve = true;
|
|
4937
|
-
|
|
4947
|
+
UtilsContext.workerClearInterval(interval);
|
|
4938
4948
|
resolve((obj as any)[checkPropertyName as string]);
|
|
4939
4949
|
}
|
|
4940
4950
|
}, intervalTimer);
|
|
4941
4951
|
if (maxTime !== -1) {
|
|
4942
|
-
|
|
4952
|
+
UtilsContext.workerSetTimeout(() => {
|
|
4943
4953
|
if (!isResolve) {
|
|
4944
|
-
|
|
4954
|
+
UtilsContext.workerClearInterval(interval);
|
|
4945
4955
|
reject();
|
|
4946
4956
|
}
|
|
4947
4957
|
}, maxTime);
|
|
@@ -5233,6 +5243,138 @@ class Utils {
|
|
|
5233
5243
|
*/
|
|
5234
5244
|
Vue = Vue;
|
|
5235
5245
|
ModuleRaid = ModuleRaid;
|
|
5246
|
+
/**
|
|
5247
|
+
* 自动使用 Worker 执行 setTimeout
|
|
5248
|
+
* @param callback 回调函数
|
|
5249
|
+
* @param [timeout=0] 延迟时间,默认为0
|
|
5250
|
+
*/
|
|
5251
|
+
workerSetTimeout(callback: Function, timeout: number = 0) {
|
|
5252
|
+
try {
|
|
5253
|
+
return WorkerSetTimeout(callback, timeout);
|
|
5254
|
+
} catch (error) {
|
|
5255
|
+
return globalThis.setTimeout(callback, timeout);
|
|
5256
|
+
}
|
|
5257
|
+
}
|
|
5258
|
+
/**
|
|
5259
|
+
* 配合 .setTimeout 使用
|
|
5260
|
+
* @param timeId setTimeout 返回的`id`
|
|
5261
|
+
*/
|
|
5262
|
+
workerClearTimeout(timeId: number | undefined) {
|
|
5263
|
+
try {
|
|
5264
|
+
if (timeId != null) {
|
|
5265
|
+
WorkerClearTimeout(timeId);
|
|
5266
|
+
}
|
|
5267
|
+
} catch (error) {
|
|
5268
|
+
} finally {
|
|
5269
|
+
globalThis.clearTimeout(timeId);
|
|
5270
|
+
}
|
|
5271
|
+
}
|
|
5272
|
+
/**
|
|
5273
|
+
* 自动使用 Worker 执行 setInterval
|
|
5274
|
+
* @param callback 回调函数
|
|
5275
|
+
* @param timeout 间隔时间,默认为0
|
|
5276
|
+
*/
|
|
5277
|
+
workerSetInterval(callback: Function, timeout: number = 0) {
|
|
5278
|
+
try {
|
|
5279
|
+
return WorkerSetInterval(callback, timeout);
|
|
5280
|
+
} catch (error) {
|
|
5281
|
+
return globalThis.setInterval(callback, timeout);
|
|
5282
|
+
}
|
|
5283
|
+
}
|
|
5284
|
+
/**
|
|
5285
|
+
* 配合 .setInterval 使用
|
|
5286
|
+
* @param timeId setInterval 返回的`id`
|
|
5287
|
+
*/
|
|
5288
|
+
workerClearInterval(timeId: number | undefined) {
|
|
5289
|
+
try {
|
|
5290
|
+
if (timeId != null) {
|
|
5291
|
+
WorkerClearInterval(timeId);
|
|
5292
|
+
}
|
|
5293
|
+
} catch (error) {
|
|
5294
|
+
} finally {
|
|
5295
|
+
globalThis.clearInterval(timeId);
|
|
5296
|
+
}
|
|
5297
|
+
}
|
|
5298
|
+
/**
|
|
5299
|
+
* 获取剪贴板信息
|
|
5300
|
+
*/
|
|
5301
|
+
async getClipboardInfo() {
|
|
5302
|
+
return new Promise<{
|
|
5303
|
+
/**
|
|
5304
|
+
* 错误信息,如果为null,则表示读取成功
|
|
5305
|
+
*/
|
|
5306
|
+
error: Error | null;
|
|
5307
|
+
/**
|
|
5308
|
+
* 剪贴板内容
|
|
5309
|
+
*/
|
|
5310
|
+
content: string;
|
|
5311
|
+
}>((resolve) => {
|
|
5312
|
+
/** 读取剪贴板 */
|
|
5313
|
+
function readClipboardText() {
|
|
5314
|
+
navigator.clipboard
|
|
5315
|
+
.readText()
|
|
5316
|
+
.then((clipboardText) => {
|
|
5317
|
+
resolve({
|
|
5318
|
+
error: null,
|
|
5319
|
+
content: clipboardText,
|
|
5320
|
+
});
|
|
5321
|
+
})
|
|
5322
|
+
.catch((error: TypeError) => {
|
|
5323
|
+
resolve({
|
|
5324
|
+
error: error,
|
|
5325
|
+
content: "",
|
|
5326
|
+
});
|
|
5327
|
+
});
|
|
5328
|
+
}
|
|
5329
|
+
/** 申请读取剪贴板的权限 */
|
|
5330
|
+
function requestPermissionsWithClipboard() {
|
|
5331
|
+
navigator.permissions
|
|
5332
|
+
.query({
|
|
5333
|
+
// @ts-ignore
|
|
5334
|
+
name: "clipboard-read",
|
|
5335
|
+
})
|
|
5336
|
+
.then((permissionStatus) => {
|
|
5337
|
+
readClipboardText();
|
|
5338
|
+
})
|
|
5339
|
+
.catch((error: TypeError) => {
|
|
5340
|
+
/* 该权限申请Api可能在该环境下不生效,尝试直接读取剪贴板 */
|
|
5341
|
+
readClipboardText();
|
|
5342
|
+
});
|
|
5343
|
+
}
|
|
5344
|
+
/**
|
|
5345
|
+
* 检查当前环境是否支持读取剪贴板Api
|
|
5346
|
+
*/
|
|
5347
|
+
function checkClipboardApi() {
|
|
5348
|
+
if (typeof navigator?.clipboard?.readText !== "function") {
|
|
5349
|
+
return false;
|
|
5350
|
+
}
|
|
5351
|
+
if (typeof navigator?.permissions?.query !== "function") {
|
|
5352
|
+
return false;
|
|
5353
|
+
}
|
|
5354
|
+
return true;
|
|
5355
|
+
}
|
|
5356
|
+
if (!checkClipboardApi()) {
|
|
5357
|
+
resolve({
|
|
5358
|
+
error: new Error("当前环境不支持读取剪贴板Api"),
|
|
5359
|
+
content: "",
|
|
5360
|
+
});
|
|
5361
|
+
return;
|
|
5362
|
+
}
|
|
5363
|
+
if (document.hasFocus()) {
|
|
5364
|
+
requestPermissionsWithClipboard();
|
|
5365
|
+
} else {
|
|
5366
|
+
window.addEventListener(
|
|
5367
|
+
"focus",
|
|
5368
|
+
() => {
|
|
5369
|
+
requestPermissionsWithClipboard();
|
|
5370
|
+
},
|
|
5371
|
+
{
|
|
5372
|
+
once: true,
|
|
5373
|
+
}
|
|
5374
|
+
);
|
|
5375
|
+
}
|
|
5376
|
+
});
|
|
5377
|
+
}
|
|
5236
5378
|
}
|
|
5237
5379
|
|
|
5238
5380
|
let utils = new Utils();
|