-
Notifications
You must be signed in to change notification settings - Fork 735
/
Copy pathutils.tsx
129 lines (116 loc) · 3.91 KB
/
utils.tsx
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
export function renderSize(value: number | undefined, places?: number) {
if (undefined == value || value === 0) {
return "0 B";
}
const unitArr = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
let srcSize = value;
let index = Math.floor(Math.log(srcSize) / Math.log(1024));
let size = srcSize / Math.pow(1024, index);
if (places == undefined) {
places = 2
}
let sizeStr = size.toFixed(places);
return sizeStr + ' ' + unitArr[index];
}
export function toggleFullScreen() {
if (!document.fullscreenElement) {
// @ts-ignore
if (navigator.keyboard) {
// @ts-ignore
navigator.keyboard.lock();
}
document.documentElement.requestFullscreen();
} else {
if (document.exitFullscreen) {
document.exitFullscreen();
// @ts-ignore
if (navigator.keyboard) {
// @ts-ignore
navigator.keyboard?.unlock();
}
}
}
}
// @ts-ignore
export function requestFullScreen(element: HTMLElement) {
if (document.fullscreenElement) {
document.exitFullscreen();
// @ts-ignore
// if (navigator.keyboard) {
// // @ts-ignore
// navigator.keyboard?.unlock();
// }
} else {
// @ts-ignore
// if (navigator.keyboard) {
// // @ts-ignore
// navigator.keyboard.lock();
// }
element.requestFullscreen();
}
}
export function isFullScreen() {
return document.fullscreenElement != null;
}
export function findScroller(element: HTMLElement) {
element.onscroll = function () {
console.log(element)
}
Array.from(element.children).forEach(findScroller)
}
export const generateRandomId = (): string => {
const array = new Uint8Array(16); // 16 bytes = 128 bits
crypto.getRandomValues(array);
return Array.from(array, byte => byte.toString(16).padStart(2, '0')).join('');
};
export function openOrSwitchToPage(url: string, windowName: string) {
// 尝试切换到已打开的窗口
const win = window.open('', windowName);
if (win.location.href === 'about:blank') {
win.location.href = url;
win.focus();
} else if (win.location.href !== url) {
// 如果窗口 URL 不匹配目标 URL,则更新并切换
win.location.href = url;
win.focus();
} else {
win.focus();
}
}
export function isFontAvailable(fontName: string) {
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
const text = 'abcdefghijklmnopqrstuvwxyz0123456789';
context.font = '72px monospace';
const baselineSize = context.measureText(text).width;
context.font = '72px ' + fontName + ', monospace';
const newSize = context.measureText(text).width;
return newSize !== baselineSize;
}
export function isMobileByMediaQuery() {
const mediaQuery = window.matchMedia("(max-width: 768px)");
return mediaQuery.matches;
}
export function isFirefox() {
return navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
}
// 1、在IE中这个事件你只要去关闭窗口就触发。
// 2、谷歌、火狐等在F12调试模式中也会起效
// 3、谷歌、火狐、QQ等浏览器中被优化了,需要用户在页面有过任何操作才会出现提示!(坑)。
export const beforeUnload = function (event) {
// 一些浏览器可能需要设置 returnValue 属性
event.preventDefault();
event.returnValue = "你确定要离开这个页面吗?";
}
export const browserDownload = (url: string) => {
window.removeEventListener('beforeunload', beforeUnload, true);
const a = document.createElement('a');
a.href = url;
if (isFirefox()) {
a.target = '_blank';
}
document.body.appendChild(a);
a.click();
a.remove();
window.addEventListener('beforeunload', beforeUnload, true);
}