@hzab/utils 1.1.5 → 1.1.6
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/changelog.md +4 -0
- package/package.json +1 -1
- package/src/sizeAdapter.ts +108 -0
- package/src/upload/ossUpload.ts +1 -1
- package/src/upload/uploadUtils.ts +6 -1
package/changelog.md
CHANGED
package/package.json
CHANGED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 获取视窗宽高
|
|
3
|
+
* @returns
|
|
4
|
+
*/
|
|
5
|
+
export function getClientStyle() {
|
|
6
|
+
return {
|
|
7
|
+
width: document.documentElement.clientWidth || document.body.clientWidth,
|
|
8
|
+
height: document.documentElement.clientHeight || document.body.clientHeight,
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 视窗宽高
|
|
14
|
+
*/
|
|
15
|
+
export const clientStyle = getClientStyle();
|
|
16
|
+
|
|
17
|
+
window.addEventListener("resize", () => {
|
|
18
|
+
const { width, height } = getClientStyle();
|
|
19
|
+
clientStyle.width = width;
|
|
20
|
+
clientStyle.height = height;
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 获取当前视窗 1vw、1vh 对应的 px
|
|
25
|
+
*/
|
|
26
|
+
export function getViewportPX() {
|
|
27
|
+
const { width, height } = clientStyle;
|
|
28
|
+
return {
|
|
29
|
+
wPx: width / 100,
|
|
30
|
+
hPx: height / 100,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// 视觉稿默认的宽高 px
|
|
35
|
+
// TODO: 按需修改
|
|
36
|
+
export const DEFAULT_SIZE = {
|
|
37
|
+
width: 750,
|
|
38
|
+
height: 1334,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export function setDefaultSize(width, height) {
|
|
42
|
+
DEFAULT_SIZE.width = width || 750;
|
|
43
|
+
DEFAULT_SIZE.height = height || 1334;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 获取视窗与视觉稿的比例
|
|
48
|
+
* @param current
|
|
49
|
+
* @param defaultVal
|
|
50
|
+
* @returns
|
|
51
|
+
*/
|
|
52
|
+
export function getClientRate(current, defaultVal) {
|
|
53
|
+
return parseFloat(current) / defaultVal;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 获取默认宽度对应的 vw 值
|
|
58
|
+
*/
|
|
59
|
+
export function getVW(pxNum = 0) {
|
|
60
|
+
return `${getClientRate(pxNum, DEFAULT_SIZE.width) * 100}vw`;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 获取默认高度对应的 vh 值
|
|
65
|
+
*/
|
|
66
|
+
export function getVH(pxNum = 0) {
|
|
67
|
+
return `${getClientRate(pxNum, DEFAULT_SIZE.height) * 100}vh`;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* 获取默认宽度对应的 px 值
|
|
72
|
+
* 适用于必须传入 px 值的情况,如 ECharts
|
|
73
|
+
*/
|
|
74
|
+
export function getWpx(pxNum = 0) {
|
|
75
|
+
return getClientRate(pxNum, DEFAULT_SIZE.width) * clientStyle.width;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 获取默认高度对应的 px 值
|
|
80
|
+
* 适用于必须传入 px 值的情况,如 ECharts
|
|
81
|
+
*/
|
|
82
|
+
export function getHpx(pxNum = 0) {
|
|
83
|
+
return getClientRate(pxNum, DEFAULT_SIZE.height) * clientStyle.height;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 通过 vw 获取对应的 px
|
|
88
|
+
* @param {number} vw
|
|
89
|
+
*/
|
|
90
|
+
export function getHPxByVW(vw = 0) {
|
|
91
|
+
let _vw = vw;
|
|
92
|
+
if (typeof _vw === "string") {
|
|
93
|
+
_vw = parseFloat(_vw);
|
|
94
|
+
}
|
|
95
|
+
return clientStyle.width * (_vw / 100);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 通过 vh 获取对应的 px
|
|
100
|
+
* @param {number} vh
|
|
101
|
+
*/
|
|
102
|
+
export function getWPxByVH(vh = 0) {
|
|
103
|
+
let _vh = vh;
|
|
104
|
+
if (typeof _vh === "string") {
|
|
105
|
+
_vh = parseFloat(_vh);
|
|
106
|
+
}
|
|
107
|
+
return clientStyle.height * (_vh / 100);
|
|
108
|
+
}
|
package/src/upload/ossUpload.ts
CHANGED
|
@@ -14,7 +14,7 @@ class OssUpload extends OssUploadUtil {
|
|
|
14
14
|
|
|
15
15
|
upload(file, opt: IUploadOpt = {}): Promise<{ fileUrl: string }> {
|
|
16
16
|
const fileName = getFileNameByFileObj(file, opt);
|
|
17
|
-
return this._upload(file, { ...opt
|
|
17
|
+
return this._upload(file, { fileName, ...opt });
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
|
|
@@ -117,15 +117,20 @@ export const getFileNameByFileObj = (file: IFileNameSource, opt?: IGetFileNameBy
|
|
|
117
117
|
} = opt || {};
|
|
118
118
|
|
|
119
119
|
const _fullFileName = file.name || fileName || "";
|
|
120
|
+
|
|
120
121
|
const { mainPart, ext } = getFileNameExt(_fullFileName);
|
|
121
122
|
|
|
122
123
|
console.log("getFileTypeExt(file.type)", file.type, getFileTypeExt(file.type));
|
|
123
124
|
|
|
124
125
|
const _fileName = mainPart || _fullFileName || getFileName(file.url) || "file";
|
|
126
|
+
|
|
125
127
|
const _ext = ext || getFileTypeExt(file.type);
|
|
126
128
|
|
|
127
129
|
const hashStr = useHashName ? nanoidNumALetters() : "";
|
|
128
|
-
const nm = mergeFileName(
|
|
130
|
+
const nm = mergeFileName(
|
|
131
|
+
_fileName + "." + _ext,
|
|
132
|
+
_fileName.indexOf("~kid-") < 0 ? `~kid-${hashStr}-${file.createTime || dayjs().valueOf()}-${ext}` : "",
|
|
133
|
+
);
|
|
129
134
|
|
|
130
135
|
return nm;
|
|
131
136
|
};
|