Skip to content

Commit 8986eba

Browse files
authored
Merge pull request #79 from MichealWayne/feat-utils-npm
feat update web utils & regex.json
2 parents 855d35c + 598377b commit 8986eba

File tree

10 files changed

+112
-9
lines changed

10 files changed

+112
-9
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### 更新信息
22

3+
- 2024.10.13:utils 更新(增加web utils方法);更新regex.json;
34
- 2024.08.25:utils 优化(增加方法参数注释);
45
- 2024.07.21:utils 更新(canvas-utils、env);
56
- 2024.06.16:主页链接调整
File renamed without changes.
File renamed without changes.

docs/datas/regex.json datas/regex.json

+11-5
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
},
88
{
99
"id": "phone",
10-
"name": "手机号",
11-
"description": "",
10+
"name": "(中国大陆)手机号",
11+
"description": "11位数字的手机号",
1212
"regexStr": "^1(3|4|5|6|7|8|9)\\d{9}$"
1313
},
1414
{
1515
"id": "tel",
16-
"name": "电话",
16+
"name": "(中国大陆)电话",
1717
"description": "固定电话",
1818
"regexStr": "(\\(\\d{3,4}\\)|\\d{3,4}-|\\s)?\\d{8}"
1919
},
@@ -157,13 +157,13 @@
157157
},
158158
{
159159
"id": "english2",
160-
"name": "英文",
160+
"name": "大写英文",
161161
"description": "由26个大写英文字母组成的字符串",
162162
"regexStr": "^[A-Z]+$"
163163
},
164164
{
165165
"id": "english3",
166-
"name": "英文",
166+
"name": "小写英文",
167167
"description": "由26个小写英文字母组成的字符串",
168168
"regexStr": "^[a-z]+$"
169169
},
@@ -364,5 +364,11 @@
364364
"name": "中文姓名",
365365
"description": "仅包含中文字符",
366366
"regexStr": "^[\u4e00-\u9fa5·]{2,}$"
367+
},
368+
{
369+
"id": "emptyLine",
370+
"name": "空白行",
371+
"description": "是否包含空白行",
372+
"regexStr": "^[\\s\\S]*\\n"
367373
}
368374
]
File renamed without changes.

utils/packages/web-utils/README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# webAPI
1+
# web utils
22

33
浏览器相关封装。
44

@@ -10,6 +10,8 @@
1010
- getPrefix:获取浏览器前缀
1111
- getStyle:获取元素样式
1212
- getElementSize:获取元素的尺寸数据、如 width/height
13+
- [clipboard.ts](./src/clipboard.ts)
14+
- copyToClipboard:拷贝文案到剪贴板
1315
- [dom.ts](./src/dom.ts)
1416
- isBrowser:是否浏览器环境
1517
- isBrowserTab:当前页面是否为显示状态
@@ -31,6 +33,12 @@
3133
- animateScrollTo:平滑滚动(dom 动画)
3234
- smoothScroll:平滑滚动(依赖 webapi)
3335
- disableCopy:禁止复制
36+
- [file.ts](./src/file.ts)
37+
- readFile:读取文件内容,返回字符串
38+
- readFileAsDataURL:读取文件内容为 Data URL
39+
- downloadFile:保存/下载文件
40+
- downloadImageFileByUrl:根据 URL 下载图片文件
41+
- getFileExtension:根据文件名获取文件扩展名
3442
- [image.ts](./src/image.ts)
3543
- isImageLoaded:加载图片(通常用于预加载)
3644
- getImageSize:获取图片的原始尺寸大小
@@ -63,3 +71,5 @@
6371
- uniqueSlash:将路径中重复的正斜杆替换成单个斜杆隔开的字符串
6472
- [keyboard.ts](./src/keyboard.ts)
6573
- getKeyName:获取 pc 按键值
74+
- [others.ts](./src/others.ts)
75+
- isBase64:判断字符串是否是 base64
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @author Wayne
3+
* @Date 2024-10-13 13:39:56
4+
* @LastEditTime 2024-10-13 13:40:36
5+
*/
6+
7+
/**
8+
* @function copyToClipboard
9+
* @description 拷贝文案到剪贴板
10+
* @param {string} text 文案
11+
* @return {Promise<boolean>} 是否拷贝成功
12+
*/
13+
export function copyToClipboard(text: string) {
14+
return new Promise(resolve => {
15+
// 创建一个input框获取需要复制的文本内容
16+
const copyInput = document.createElement('input');
17+
copyInput.value = text;
18+
document.body.appendChild(copyInput);
19+
copyInput.select();
20+
document.execCommand('copy');
21+
copyInput.remove();
22+
resolve(true);
23+
});
24+
}

utils/packages/web-utils/src/file.ts

+39-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/**
22
* @author Wayne
33
* @Date 2024-07-07 13:42:13
4-
* @LastEditTime 2024-08-25 10:14:32
4+
* @LastEditTime 2024-10-13 13:31:20
55
*/
66
/**
77
* @function readFile
8-
* @description 读取文件内容
8+
* @description 读取文件内容,返回字符串
99
* @param {File} file 文件对象
1010
* @returns {Promise<string>} 文件内容
1111
* @example
@@ -63,6 +63,43 @@ export function downloadFile(content: string, filename: string, contentType: str
6363
}, 0);
6464
}
6565

66+
/**
67+
* @function downloadImageFileByUrl
68+
* @description 根据URL下载图片文件
69+
* @param {string} url 图片URL
70+
* @param {string} filename 文件名
71+
* @param {string} imageType 图片类型
72+
* @returns {Promise<boolean>} 是否下载成功
73+
* @example
74+
* downloadImageFileByUrl('https://example.com/image.jpg', 'image.jpg');
75+
* downloadImageFileByUrl('https://example.com/image.png', 'image.png', 'image/png');
76+
*/
77+
export function downloadImageFileByUrl(url: string, filename: string, imageType = 'image/jpeg') {
78+
return new Promise((resolve, reject) => {
79+
const canvas = document.createElement('canvas');
80+
const img = document.createElement('img');
81+
img.setAttribute('crossOrigin', 'Anonymous');
82+
img.src = url;
83+
img.onload = _ => {
84+
canvas.width = img.width;
85+
canvas.height = img.height;
86+
87+
const context = canvas.getContext('2d')!;
88+
context.drawImage(img, 0, 0, img.width, img.height);
89+
90+
canvas.toBlob(blob => {
91+
const link = document.createElement('a');
92+
link.href = window.URL.createObjectURL(blob!);
93+
link.download = filename;
94+
link.click();
95+
URL.revokeObjectURL(link.href);
96+
resolve(true);
97+
}, imageType);
98+
};
99+
img.onerror = e => reject(e);
100+
});
101+
}
102+
66103
/**
67104
* @function getFileExtension
68105
* @description 根据文件名获取文件扩展名

utils/packages/web-utils/src/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/**
22
* @author Wayne
33
* @Date 2022-07-05 13:34:44
4-
* @LastEditTime 2024-07-07 13:58:25
4+
* @LastEditTime 2024-10-13 13:41:19
55
*/
66

77
export * from './cookies';
88
export * from './css';
9+
export * from './clipboard';
910
export * from './dom';
1011
export * from './file';
1112
export * from './image';
@@ -16,3 +17,5 @@ export * from './rem';
1617
export * from './screen';
1718
export * from './storage';
1819
export * from './url';
20+
21+
export * from './others';
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @author Wayne
3+
* @Date 2024-10-13 13:34:58
4+
* @LastEditTime 2024-10-13 13:35:16
5+
*/
6+
7+
/**
8+
* @function isBase64
9+
* @description 判断字符串是否是base64
10+
* @param {string} str 字符串
11+
* @return {boolean} 是否是base64
12+
*/
13+
export const isBase64 = (str: string): boolean => {
14+
if (str === '' || str.trim() === '') {
15+
return false;
16+
}
17+
try {
18+
return btoa(atob(str)) === str;
19+
} catch (err) {
20+
return false;
21+
}
22+
};

0 commit comments

Comments
 (0)