@cloudcome/utils-browser 1.1.1 → 1.1.2
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/base64.cjs +15 -0
- package/dist/base64.cjs.map +1 -0
- package/dist/base64.d.ts +14 -0
- package/dist/base64.mjs +15 -0
- package/dist/base64.mjs.map +1 -0
- package/{src/cache.ts → dist/cache.cjs} +20 -48
- package/dist/cache.cjs.map +1 -0
- package/dist/cache.d.ts +49 -0
- package/dist/cache.mjs +87 -0
- package/dist/cache.mjs.map +1 -0
- package/dist/canvas.cjs +58 -0
- package/dist/canvas.cjs.map +1 -0
- package/dist/canvas.d.ts +92 -0
- package/dist/canvas.mjs +58 -0
- package/dist/canvas.mjs.map +1 -0
- package/dist/clipboard.cjs +16 -0
- package/dist/clipboard.cjs.map +1 -0
- package/{src/clipboard.ts → dist/clipboard.d.ts} +1 -12
- package/dist/clipboard.mjs +16 -0
- package/dist/clipboard.mjs.map +1 -0
- package/dist/cookie.cjs +50 -0
- package/dist/cookie.cjs.map +1 -0
- package/dist/cookie.d.ts +55 -0
- package/dist/cookie.mjs +50 -0
- package/dist/cookie.mjs.map +1 -0
- package/dist/dom.cjs +18 -0
- package/dist/dom.cjs.map +1 -0
- package/{src/dom.ts → dist/dom.d.ts} +3 -21
- package/dist/dom.mjs +18 -0
- package/dist/dom.mjs.map +1 -0
- package/dist/download.cjs +16 -0
- package/dist/download.cjs.map +1 -0
- package/dist/download.d.ts +12 -0
- package/dist/download.mjs +16 -0
- package/dist/download.mjs.map +1 -0
- package/dist/image.cjs +35 -0
- package/dist/image.cjs.map +1 -0
- package/dist/image.d.ts +9 -0
- package/dist/image.mjs +35 -0
- package/dist/image.mjs.map +1 -0
- package/dist/index.cjs +5 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +5 -0
- package/dist/index.mjs.map +1 -0
- package/dist/timer.cjs +41 -0
- package/dist/timer.cjs.map +1 -0
- package/dist/timer.d.ts +9 -0
- package/dist/timer.mjs +41 -0
- package/dist/timer.mjs.map +1 -0
- package/dist/video.cjs +14 -0
- package/dist/video.cjs.map +1 -0
- package/dist/video.d.ts +9 -0
- package/dist/video.mjs +14 -0
- package/dist/video.mjs.map +1 -0
- package/package.json +9 -3
- package/CHANGELOG.md +0 -27
- package/src/base64.ts +0 -25
- package/src/canvas.ts +0 -147
- package/src/cookie.ts +0 -118
- package/src/download.ts +0 -22
- package/src/dts/global.d.ts +0 -27
- package/src/image.ts +0 -45
- package/src/index.ts +0 -1
- package/src/timer.ts +0 -56
- package/src/video.ts +0 -19
- package/test/base64.test.ts +0 -47
- package/test/cache.test.ts +0 -199
- package/test/canvas.test.ts +0 -124
- package/test/clipboard.test.ts +0 -18
- package/test/cookie.test.ts +0 -52
- package/test/dom.test.ts +0 -67
- package/test/download.test.ts +0 -15
- package/test/image.test.ts +0 -75
- package/test/index.test.ts +0 -6
- package/test/timer.test.ts +0 -109
- package/test/video.test.ts +0 -36
- package/tsconfig.json +0 -31
- package/vite.config.mts +0 -100
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudcome/utils-browser",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "cloudcome utils for browser",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=22"
|
|
@@ -102,10 +102,16 @@
|
|
|
102
102
|
"main": "./dist/index.cjs",
|
|
103
103
|
"module": "./dist/index.mjs",
|
|
104
104
|
"types": "./dist/index.d.ts",
|
|
105
|
+
"files": [
|
|
106
|
+
"dist"
|
|
107
|
+
],
|
|
105
108
|
"dependencies": {
|
|
106
|
-
"@cloudcome/utils-core": "1.
|
|
109
|
+
"@cloudcome/utils-core": "1.2.0"
|
|
110
|
+
},
|
|
111
|
+
"repository": {
|
|
112
|
+
"type": "git",
|
|
113
|
+
"url": "git+https://github.com/cloudcome/utils.git"
|
|
107
114
|
},
|
|
108
|
-
"repository": "https://github.com/cloudcome/utils",
|
|
109
115
|
"keywords": [
|
|
110
116
|
"utils",
|
|
111
117
|
"cloudcome",
|
package/CHANGELOG.md
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
# Change Log
|
|
2
|
-
|
|
3
|
-
All notable changes to this project will be documented in this file.
|
|
4
|
-
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
-
|
|
6
|
-
## [1.1.1](https://github.com/cloudcome/utils/compare/@cloudcome/utils-browser@1.1.0...@cloudcome/utils-browser@1.1.1) (2025-08-30)
|
|
7
|
-
|
|
8
|
-
**Note:** Version bump only for package @cloudcome/utils-browser
|
|
9
|
-
|
|
10
|
-
# 1.1.0 (2025-08-27)
|
|
11
|
-
|
|
12
|
-
### Bug Fixes
|
|
13
|
-
|
|
14
|
-
* **utils-browser:** 修复 ios 拍照产生图片的尺寸问题 ([1e9b47d](https://github.com/cloudcome/utils/commit/1e9b47df68018cef80c050be48cdba206ec5e177))
|
|
15
|
-
* **utils-browser:** 修复图片加载逻辑 ([16e5a41](https://github.com/cloudcome/utils/commit/16e5a4152f29619d8abf767e7f97889216ebe1ae))
|
|
16
|
-
* **utils-browser:** 修正 cookie 过期时间格式 ([160c1c1](https://github.com/cloudcome/utils/commit/160c1c1107428f4c160b2e116454085a7fb3b83a))
|
|
17
|
-
|
|
18
|
-
### Features
|
|
19
|
-
|
|
20
|
-
* **utils-browser:** 优化 canvas 工具函数并添加新功能 ([fb89067](https://github.com/cloudcome/utils/commit/fb890674a0a97c0f09df480592236273ff3b5172))
|
|
21
|
-
* **utils-browser:** 新增多个实用工具模块 ([6958f7a](https://github.com/cloudcome/utils/commit/6958f7a13e416b3330464ef9493957db605c4eb1))
|
|
22
|
-
* **utils-browser:** 添加 canvas 相关工具函数 ([536f9d7](https://github.com/cloudcome/utils/commit/536f9d72b071402af3c1d38952d5a0ffdb1906a2))
|
|
23
|
-
* **utils-browser:** 添加 Cookie 操作工具函数 ([cc7c014](https://github.com/cloudcome/utils/commit/cc7c01493141ecd4ef270192e87e51eac8ca2008))
|
|
24
|
-
* **utils-browser:** 添加下载功能 ([7fd2441](https://github.com/cloudcome/utils/commit/7fd24411e357aa6cbfb9e0ca9961d4fcc3cde6e7))
|
|
25
|
-
* **utils-browser:** 添加图片加载函数 ([f51210f](https://github.com/cloudcome/utils/commit/f51210f0af9c47a3033af19ce901a1b70f3882fc))
|
|
26
|
-
* **utils-browser:** 添加视频加载功能 ([d4ef7a5](https://github.com/cloudcome/utils/commit/d4ef7a59a379156dbba8737d18582288ae306900))
|
|
27
|
-
* **utils-browser:** 添加设置和获取元素样式功能 ([71e0b56](https://github.com/cloudcome/utils/commit/71e0b564b09b38020fd6aa9c85bc6b3e2e137d72))
|
package/src/base64.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
// @ref https://blog.csdn.net/m0_72642319/article/details/139743196
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 【浏览器环境】将字符串编码为 Base64 格式
|
|
5
|
-
* @param input 要编码的字符串
|
|
6
|
-
* @returns 编码后的 Base64 字符串
|
|
7
|
-
* @remarks
|
|
8
|
-
* 在 Node.js 环境中使用 Buffer 实现,在浏览器环境中使用 TextEncoder 和 btoa 实现
|
|
9
|
-
*/
|
|
10
|
-
export function encodeBase64(input: string) {
|
|
11
|
-
const encoder = new TextEncoder();
|
|
12
|
-
const unit8Array = encoder.encode(input);
|
|
13
|
-
return btoa(String.fromCharCode(...unit8Array));
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* 【浏览器环境】 将 Base64 字符串解码为原始字符串
|
|
18
|
-
* @param input 要解码的 Base64 字符串
|
|
19
|
-
* @returns 解码后的原始字符串
|
|
20
|
-
*/
|
|
21
|
-
export function decodeBase64(input: string) {
|
|
22
|
-
const decoder = new TextDecoder();
|
|
23
|
-
const unit8Array = Uint8Array.from(atob(input), (c) => c.charCodeAt(0));
|
|
24
|
-
return decoder.decode(unit8Array);
|
|
25
|
-
}
|
package/src/canvas.ts
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import { objectDefaults } from '@cloudcome/utils-core/object';
|
|
2
|
-
import { imageLoad } from './image';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* 将 Canvas 转换为 Base64 编码的字符串
|
|
6
|
-
* @param {HTMLCanvasElement} canvas - 需要转换的 Canvas 元素
|
|
7
|
-
* @param {string} [type] - 图片格式,默认为 'image/png'
|
|
8
|
-
* @param {number} [quality] - 图片质量,范围 0 到 1,仅适用于 'image/jpeg' 或 'image/webp'
|
|
9
|
-
* @returns {string} Base64 编码的图片数据
|
|
10
|
-
* @example
|
|
11
|
-
* const base64 = canvasToBase64(canvas, 'image/jpeg', 0.8);
|
|
12
|
-
*/
|
|
13
|
-
export function canvasToBase64(canvas: HTMLCanvasElement, type?: string, quality?: number) {
|
|
14
|
-
return canvas.toDataURL(type, quality);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* 将 Canvas 转换为 Blob 对象
|
|
19
|
-
* @param {HTMLCanvasElement} canvas - 需要转换的 Canvas 元素
|
|
20
|
-
* @param {string} [type] - 图片格式,默认为 'image/png'
|
|
21
|
-
* @param {number} [quality] - 图片质量,范围 0 到 1,仅适用于 'image/jpeg' 或 'image/webp'
|
|
22
|
-
* @returns {Promise<Blob>} 返回一个包含 Blob 对象的 Promise
|
|
23
|
-
* @example
|
|
24
|
-
* const blob = await canvasToBlob(canvas, 'image/jpeg', 0.8);
|
|
25
|
-
*/
|
|
26
|
-
export async function canvasToBlob(canvas: HTMLCanvasElement, type?: string, quality?: number) {
|
|
27
|
-
return new Promise<Blob>((resolve, reject) => {
|
|
28
|
-
canvas.toBlob(
|
|
29
|
-
(blob) => {
|
|
30
|
-
if (blob) {
|
|
31
|
-
resolve(blob);
|
|
32
|
-
} else {
|
|
33
|
-
reject(new Error('canvas 导出二进制对象失败'));
|
|
34
|
-
}
|
|
35
|
-
},
|
|
36
|
-
type,
|
|
37
|
-
quality,
|
|
38
|
-
);
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Canvas 绘制图片的配置选项
|
|
44
|
-
*/
|
|
45
|
-
export type CanvasDrawImageOptions = {
|
|
46
|
-
/**
|
|
47
|
-
* 源图片的左上角 x 坐标
|
|
48
|
-
* @type {number}
|
|
49
|
-
* @default 0
|
|
50
|
-
*/
|
|
51
|
-
srcLeft?: number;
|
|
52
|
-
/**
|
|
53
|
-
* 源图片的左上角 y 坐标
|
|
54
|
-
* @type {number}
|
|
55
|
-
* @default 0
|
|
56
|
-
*/
|
|
57
|
-
srcTop?: number;
|
|
58
|
-
/**
|
|
59
|
-
* 源图片的宽度
|
|
60
|
-
* @type {number}
|
|
61
|
-
* @default 图片的原始宽度
|
|
62
|
-
*/
|
|
63
|
-
srcWidth?: number;
|
|
64
|
-
/**
|
|
65
|
-
* 源图片的高度
|
|
66
|
-
* @type {number}
|
|
67
|
-
* @default 图片的原始高度
|
|
68
|
-
*/
|
|
69
|
-
srcHeight?: number;
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* 目标 Canvas 的左上角 x 坐标
|
|
73
|
-
* @type {number}
|
|
74
|
-
* @default 0
|
|
75
|
-
*/
|
|
76
|
-
destLeft?: number;
|
|
77
|
-
/**
|
|
78
|
-
* 目标 Canvas 的左上角 y 坐标
|
|
79
|
-
* @type {number}
|
|
80
|
-
* @default 0
|
|
81
|
-
*/
|
|
82
|
-
destTop?: number;
|
|
83
|
-
/**
|
|
84
|
-
* 目标 Canvas 的宽度
|
|
85
|
-
* @type {number}
|
|
86
|
-
* @default Canvas 的宽度
|
|
87
|
-
*/
|
|
88
|
-
destWidth?: number;
|
|
89
|
-
/**
|
|
90
|
-
* 目标 Canvas 的高度
|
|
91
|
-
* @type {number}
|
|
92
|
-
* @default Canvas 的高度
|
|
93
|
-
*/
|
|
94
|
-
destHeight?: number;
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* 在 Canvas 上绘制图片
|
|
99
|
-
* @param {HTMLCanvasElement} canvas - 目标 Canvas 元素
|
|
100
|
-
* @param {string} url - 图片的 URL
|
|
101
|
-
* @param {CanvasDrawImageOptions} [options] - 绘制图片的配置选项
|
|
102
|
-
* @returns {Promise<void>} 返回一个 Promise,表示绘制完成
|
|
103
|
-
* @example
|
|
104
|
-
* await canvasDrawImage(canvas, 'https://example.com/image.png', {
|
|
105
|
-
* srcLeft: 10,
|
|
106
|
-
* srcTop: 10,
|
|
107
|
-
* srcWidth: 100,
|
|
108
|
-
* srcHeight: 100,
|
|
109
|
-
* destLeft: 0,
|
|
110
|
-
* destTop: 0,
|
|
111
|
-
* destWidth: 200,
|
|
112
|
-
* destHeight: 200
|
|
113
|
-
* });
|
|
114
|
-
*/
|
|
115
|
-
export async function canvasDrawImage(canvas: HTMLCanvasElement, url: string, options?: CanvasDrawImageOptions) {
|
|
116
|
-
const ctx = canvas.getContext('2d');
|
|
117
|
-
if (!ctx) throw new Error('canvas context is null');
|
|
118
|
-
|
|
119
|
-
const img = await imageLoad(url);
|
|
120
|
-
const defaults: CanvasDrawImageOptions = {
|
|
121
|
-
srcLeft: 0,
|
|
122
|
-
srcTop: 0,
|
|
123
|
-
srcWidth: img.width,
|
|
124
|
-
srcHeight: img.height,
|
|
125
|
-
destLeft: 0,
|
|
126
|
-
destTop: 0,
|
|
127
|
-
destWidth: canvas.width,
|
|
128
|
-
destHeight: canvas.height,
|
|
129
|
-
};
|
|
130
|
-
const { srcLeft, srcTop, srcWidth, srcHeight, destLeft, destTop, destWidth, destHeight } = objectDefaults(
|
|
131
|
-
options || {},
|
|
132
|
-
defaults,
|
|
133
|
-
) as Required<CanvasDrawImageOptions>;
|
|
134
|
-
ctx.drawImage(
|
|
135
|
-
img,
|
|
136
|
-
// source
|
|
137
|
-
srcLeft,
|
|
138
|
-
srcTop,
|
|
139
|
-
srcWidth,
|
|
140
|
-
srcHeight,
|
|
141
|
-
// dest
|
|
142
|
-
destLeft,
|
|
143
|
-
destTop,
|
|
144
|
-
destWidth,
|
|
145
|
-
destHeight,
|
|
146
|
-
);
|
|
147
|
-
}
|
package/src/cookie.ts
DELETED
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
import type { TDateValue } from '@cloudcome/utils-core/date';
|
|
2
|
-
import { dateParse } from '@cloudcome/utils-core/date';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* 获取指定名称的 Cookie 值。
|
|
6
|
-
* @param {string} name - Cookie 的名称。
|
|
7
|
-
* @returns {string} - 返回对应的 Cookie 值,如果不存在则返回空字符串。
|
|
8
|
-
*/
|
|
9
|
-
export function cookieGet(name: string) {
|
|
10
|
-
const cookies = document.cookie.split(';');
|
|
11
|
-
|
|
12
|
-
for (let i = 0; i < cookies.length; i++) {
|
|
13
|
-
const cookie = cookies[i].trim();
|
|
14
|
-
|
|
15
|
-
if (cookie.startsWith(`${name}=`)) {
|
|
16
|
-
return decodeURIComponent(cookie.slice(name.length + 1));
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return '';
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* 设置 Cookie。
|
|
25
|
-
* @param {string} name - Cookie 的名称。
|
|
26
|
-
* @param {string} value - Cookie 的值。
|
|
27
|
-
* @param {CookieOptions} [options] - 可选的 Cookie 配置项。
|
|
28
|
-
*/
|
|
29
|
-
export type CookieOptions = {
|
|
30
|
-
/**
|
|
31
|
-
* Cookie 的过期时间,可以是日期字符串、时间戳或 `Date` 对象。
|
|
32
|
-
*/
|
|
33
|
-
expires?: TDateValue;
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Cookie 的路径,默认为当前路径。
|
|
37
|
-
*/
|
|
38
|
-
path?: string;
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Cookie 的域名,默认为当前域名。
|
|
42
|
-
*/
|
|
43
|
-
domain?: string;
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* 是否启用安全传输(HTTPS)。
|
|
47
|
-
*/
|
|
48
|
-
secure?: boolean;
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* SameSite 属性,可选值为 `'strict'`、`'lax'` 或 `'none'`。
|
|
52
|
-
*/
|
|
53
|
-
sameSite?: 'strict' | 'lax' | 'none';
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* 是否启用 HttpOnly 标志,防止 JavaScript 访问。
|
|
57
|
-
*/
|
|
58
|
-
httpOnly?: boolean;
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Cookie 的最大存活时间(秒)。
|
|
62
|
-
*/
|
|
63
|
-
maxAge?: number;
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* 设置 Cookie。
|
|
68
|
-
* @param {string} name - Cookie 的名称。
|
|
69
|
-
* @param {string} value - Cookie 的值。
|
|
70
|
-
* @param {CookieOptions} [options] - 可选的 Cookie 配置项。
|
|
71
|
-
*/
|
|
72
|
-
export function cookieSet(name: string, value: string, options?: CookieOptions) {
|
|
73
|
-
const { expires, maxAge, path, domain, httpOnly, sameSite, secure } = options || {};
|
|
74
|
-
let cookie = `${name}=${encodeURIComponent(value)}`;
|
|
75
|
-
|
|
76
|
-
const expiresAt = expires ? dateParse(expires) : maxAge ? dateParse(Date.now() + maxAge) : null;
|
|
77
|
-
const metas: [string, string][] = [];
|
|
78
|
-
|
|
79
|
-
if (expiresAt) {
|
|
80
|
-
metas.push(['expires', expiresAt.toISOString()]);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (path) {
|
|
84
|
-
metas.push(['path', path]);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
if (domain) {
|
|
88
|
-
metas.push(['domain', domain]);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
if (httpOnly) {
|
|
92
|
-
metas.push(['httpOnly', 'true']);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (sameSite) {
|
|
96
|
-
metas.push(['sameSite', sameSite]);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
if (secure) {
|
|
100
|
-
metas.push(['secure', 'true']);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
for (const [key, value] of metas) {
|
|
104
|
-
cookie += `; ${key}=${value}`;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
document.cookie = cookie;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* 删除指定名称的 Cookie。
|
|
112
|
-
* @param {string} name - 要删除的 Cookie 名称。
|
|
113
|
-
*/
|
|
114
|
-
export function cookieDel(name: string) {
|
|
115
|
-
cookieSet(name, '', {
|
|
116
|
-
expires: 0,
|
|
117
|
-
});
|
|
118
|
-
}
|
package/src/download.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 下载 URL
|
|
3
|
-
* @param {string} url
|
|
4
|
-
* @param {string} filename
|
|
5
|
-
*/
|
|
6
|
-
export function downloadURL(url: string, filename?: string) {
|
|
7
|
-
const a = document.createElement('a');
|
|
8
|
-
a.href = url;
|
|
9
|
-
a.download = filename || '';
|
|
10
|
-
a.click();
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* 下载 Blob 对象
|
|
15
|
-
* @param blob Blob 对象
|
|
16
|
-
* @param filename 文件名
|
|
17
|
-
*/
|
|
18
|
-
export function downloadBlob(blob: Blob, filename?: string) {
|
|
19
|
-
const url = URL.createObjectURL(blob);
|
|
20
|
-
downloadURL(url, filename);
|
|
21
|
-
URL.revokeObjectURL(url);
|
|
22
|
-
}
|
package/src/dts/global.d.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @file global.d.ts
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* package.json name
|
|
7
|
-
*/
|
|
8
|
-
declare const PKG_NAME: string;
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* package.json version
|
|
12
|
-
*/
|
|
13
|
-
declare const PKG_VERSION: string;
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* package.json description
|
|
17
|
-
*/
|
|
18
|
-
declare const PKG_DESCRIPTION: string;
|
|
19
|
-
|
|
20
|
-
declare const IS_TEST: string;
|
|
21
|
-
|
|
22
|
-
interface TEST_MOCK {
|
|
23
|
-
IS_BROWSER: boolean;
|
|
24
|
-
IS_NODE: boolean;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
declare const TEST_MOCK: TEST_MOCK;
|
package/src/image.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { setStyle } from './dom';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 加载图片并返回一个包含 HTMLImageElement 的 Promise
|
|
5
|
-
* @param {string} url - 图片的 URL 地址
|
|
6
|
-
* @returns {Promise<HTMLImageElement>} 返回一个包含 HTMLImageElement 的 Promise
|
|
7
|
-
* @example
|
|
8
|
-
* const img = await imageLoad('https://example.com/image.png');
|
|
9
|
-
* @throws {Error} 如果图片加载失败,抛出错误
|
|
10
|
-
*/
|
|
11
|
-
export async function imageLoad(url: string) {
|
|
12
|
-
return new Promise<HTMLImageElement>((resolve, reject) => {
|
|
13
|
-
const image = new Image();
|
|
14
|
-
const onFinish = (isError?: boolean) => {
|
|
15
|
-
image.onload = image.onerror = null;
|
|
16
|
-
document.body.removeChild(image);
|
|
17
|
-
isError ? reject(new Error('图片加载失败')) : resolve(image);
|
|
18
|
-
};
|
|
19
|
-
image.onload = () => onFinish();
|
|
20
|
-
image.onerror = () => onFinish(true);
|
|
21
|
-
image.crossOrigin = 'anonymous';
|
|
22
|
-
image.src = url;
|
|
23
|
-
|
|
24
|
-
// ios 拍照产生的图片,如果没有插入的 DOM 中获取到的图片尺寸是相反的
|
|
25
|
-
setStyle(image, {
|
|
26
|
-
visibility: 'hidden',
|
|
27
|
-
position: 'absolute',
|
|
28
|
-
top: '-99999%',
|
|
29
|
-
left: '-99999%',
|
|
30
|
-
maxWidth: 'none',
|
|
31
|
-
maxHeight: 'none',
|
|
32
|
-
border: '0',
|
|
33
|
-
width: 'auto',
|
|
34
|
-
height: 'auto',
|
|
35
|
-
margin: '0',
|
|
36
|
-
padding: '0',
|
|
37
|
-
transform: '',
|
|
38
|
-
});
|
|
39
|
-
document.body.appendChild(image);
|
|
40
|
-
|
|
41
|
-
if (image.complete && image.width > 0) onFinish();
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// 图片缩放函数
|
package/src/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const VERSION = PKG_VERSION;
|
package/src/timer.ts
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { type TTimerHandler, type TTimerOptions, type TTimerState, makeInterval } from '@cloudcome/utils-core/timer';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 创建一个基于 `requestAnimationFrame` 的间隔定时器
|
|
5
|
-
*
|
|
6
|
-
* @param callback - 每次间隔执行的回调函数,接收定时器状态和可选的 `next` 函数
|
|
7
|
-
* @param options - 配置选项
|
|
8
|
-
* @returns {TTimerHandler} 返回包含控制方法的对象
|
|
9
|
-
*/
|
|
10
|
-
export function frameInterval(
|
|
11
|
-
callback: (state: TTimerState, next?: () => void) => unknown,
|
|
12
|
-
options?: TTimerOptions,
|
|
13
|
-
): TTimerHandler {
|
|
14
|
-
let rafId: number;
|
|
15
|
-
const { canStart, start, canStop, stop, canPause, pause, canResume, resume, execute } = makeInterval((call) => {
|
|
16
|
-
rafId = requestAnimationFrame(call);
|
|
17
|
-
}, callback);
|
|
18
|
-
|
|
19
|
-
return {
|
|
20
|
-
start() {
|
|
21
|
-
if (!canStart()) return;
|
|
22
|
-
|
|
23
|
-
if (options?.leading) {
|
|
24
|
-
start();
|
|
25
|
-
} else {
|
|
26
|
-
rafId = requestAnimationFrame(start);
|
|
27
|
-
}
|
|
28
|
-
},
|
|
29
|
-
|
|
30
|
-
stop() {
|
|
31
|
-
if (!canStop()) return;
|
|
32
|
-
if (options?.trailing) execute();
|
|
33
|
-
|
|
34
|
-
cancelAnimationFrame(rafId);
|
|
35
|
-
stop();
|
|
36
|
-
},
|
|
37
|
-
|
|
38
|
-
pause() {
|
|
39
|
-
if (!canPause()) return;
|
|
40
|
-
if (options?.trailing) execute();
|
|
41
|
-
|
|
42
|
-
cancelAnimationFrame(rafId);
|
|
43
|
-
pause();
|
|
44
|
-
},
|
|
45
|
-
|
|
46
|
-
resume(immediate?: boolean) {
|
|
47
|
-
if (!canResume()) return;
|
|
48
|
-
|
|
49
|
-
if (immediate || options?.leading) {
|
|
50
|
-
resume();
|
|
51
|
-
} else {
|
|
52
|
-
rafId = requestAnimationFrame(resume);
|
|
53
|
-
}
|
|
54
|
-
},
|
|
55
|
-
};
|
|
56
|
-
}
|
package/src/video.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 加载视频并返回一个包含 HTMLVideoElement 的 Promise
|
|
3
|
-
* @param {string} url - 视频的 URL 地址
|
|
4
|
-
* @returns {Promise<HTMLVideoElement>} 返回一个包含 HTMLVideoElement 的 Promise
|
|
5
|
-
* @example
|
|
6
|
-
* const video = await videoLoad('https://example.com/video.mp4');
|
|
7
|
-
* @throws {Error} 如果视频加载失败,抛出错误
|
|
8
|
-
*/
|
|
9
|
-
export async function videoLoad(url: string) {
|
|
10
|
-
return new Promise<HTMLVideoElement>((resolve, reject) => {
|
|
11
|
-
const video = document.createElement('video');
|
|
12
|
-
|
|
13
|
-
video.src = url;
|
|
14
|
-
video.crossOrigin = 'anonymous';
|
|
15
|
-
video.currentTime = 1;
|
|
16
|
-
video.onloadedmetadata = () => resolve(video);
|
|
17
|
-
video.onerror = () => reject(new Error('视频加载失败'));
|
|
18
|
-
});
|
|
19
|
-
}
|
package/test/base64.test.ts
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { decodeBase64, encodeBase64 } from '@/base64';
|
|
2
|
-
import { describe, expect, it } from 'vitest';
|
|
3
|
-
|
|
4
|
-
describe('encodeBase64', () => {
|
|
5
|
-
it('应正确编码空字符串', () => {
|
|
6
|
-
expect(encodeBase64('')).toBe('');
|
|
7
|
-
});
|
|
8
|
-
|
|
9
|
-
it('应正确编码普通字符串', () => {
|
|
10
|
-
expect(encodeBase64('hello')).toBe('aGVsbG8=');
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
it('应正确编码特殊字符', () => {
|
|
14
|
-
expect(encodeBase64('!@#$%^&*()')).toBe('IUAjJCVeJiooKQ==');
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
it('应正确编码中文字符', () => {
|
|
18
|
-
expect(encodeBase64('你好')).toBe('5L2g5aW9');
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
describe('decodeBase64', () => {
|
|
23
|
-
it('应正确解码空字符串', () => {
|
|
24
|
-
expect(decodeBase64('')).toBe('');
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it('应正确解码普通字符串', () => {
|
|
28
|
-
expect(decodeBase64('aGVsbG8=')).toBe('hello');
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it('应正确解码特殊字符', () => {
|
|
32
|
-
expect(decodeBase64('IUAjJCVeJiooKQ==')).toBe('!@#$%^&*()');
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
it('应正确解码中文字符', () => {
|
|
36
|
-
expect(decodeBase64('5L2g5aW9')).toBe('你好');
|
|
37
|
-
});
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
describe('encode/decode 互操作', () => {
|
|
41
|
-
it('应能正确编码并解码回原始字符串', () => {
|
|
42
|
-
const original = 'Hello, 世界!';
|
|
43
|
-
const encoded = encodeBase64(original);
|
|
44
|
-
const decoded = decodeBase64(encoded);
|
|
45
|
-
expect(decoded).toBe(original);
|
|
46
|
-
});
|
|
47
|
-
});
|