1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { RefObject, useEffect, useLayoutEffect, useState } from 'react';
import { RGBColor } from 'react-color';
// 生成uuid
function uuid(len: number, radix: number) {
let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split(
'',
);
let uuid = [];
let i;
radix = radix || chars.length;
if (len) {
for (i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)];
} else {
let r;
uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
uuid[14] = '4';
for (i = 0; i < 36; i++) {
if (!uuid[i]) {
r = 0 | (Math.random() * 16);
uuid[i] = chars[i === 19 ? (r & 0x3) | 0x8 : r];
}
}
}
return uuid.join('');
}
// 将rgba字符串对象转化为rgba对象
function rgba2Obj(rgba = '') {
let reg = /rgba\((\d+),(\d+),(\d+),(\d+)\)/g;
let rgbaObj: RGBColor = { r: 0, g: 0, b: 0, a: 0 };
rgba.replace(reg, (_m, r, g, b, a) => {
rgbaObj = { r, g, b, a };
return rgba;
});
return rgbaObj;
}
export { uuid, rgba2Obj };
export const isDev = process.env.NODE_ENV === 'development';
export function useGetRect() {
const [rect, setRect] = useState({ width: 0, height: 0 });
useEffect(() => {
setRect({
width: window.innerWidth,
height: window.innerHeight,
});
}, []);
return rect;
}
export function useGetScrollBarWidth(ref: RefObject<HTMLElement>) {
const [width, setWidth] = useState(0);
useLayoutEffect(() => {
if (ref.current) {
const diff = ref.current.offsetWidth - ref.current.clientWidth;
setWidth(diff);
}
}, [ref]);
return width;
}
export function useAnimation(state: boolean, delay: number) {
const [display, setDisplay] = useState(false);
useEffect(() => {
let timer: number;
if (state && display === true) {
setDisplay(false);
} else if (!state && display === false) {
timer = window.setTimeout(() => {
setDisplay(true);
}, delay);
}
return () => {
window.clearTimeout(timer);
};
}, [delay, display, state]);
return [display, setDisplay];
}
export function unParams(params = '?a=1&b=2&c=3') {
let obj: any = {};
params &&
// eslint-disable-next-line no-useless-escape
params.replace(
/((\w*)=([\.a-z0-9A-Z]*)?)?/g,
(m, a, b, c): any => {
if (b || c) obj[b] = c;
},
);
return obj;
}
export function throttle(fn: Function, delay: number) {
let flag = true;
return (...args: any) => {
if (flag) {
flag = false;
fn(...args);
setTimeout(() => {
flag = true;
}, delay);
}
};
}