-
Notifications
You must be signed in to change notification settings - Fork 360
Expand file tree
/
Copy pathdom.ts
More file actions
274 lines (245 loc) · 7.26 KB
/
dom.ts
File metadata and controls
274 lines (245 loc) · 7.26 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import { Styles } from './types';
/**
* 观察元素是否进入视口
*/
export function observe(
element: HTMLElement,
root: HTMLElement,
callback: Function,
marginBottom: number
): IntersectionObserver {
if (typeof window === 'undefined') return null;
if (!window || !window.IntersectionObserver) {
callback();
return null;
}
let io: IntersectionObserver = null;
try {
io = new window.IntersectionObserver(
(entries) => {
const entry = entries[0];
if (entry.isIntersecting) {
callback();
io.unobserve(element);
}
},
{
rootMargin: `0px 0px ${marginBottom}px 0px`,
root,
}
);
io.observe(element);
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
callback();
}
return io;
}
/**
* 用于为节点增加 styles
*/
export function setStyle(el: HTMLElement, styles: Styles): void {
const keys = Object.keys(styles);
keys.forEach((key) => {
// @ts-ignore
// eslint-disable-next-line no-param-reassign
el.style[key] = styles[key];
});
}
/**
* 注入样式到 document
*/
export function injectStyle(style: string): void {
const styleElement = document.createElement('style');
let styleSheet = null;
document.head.appendChild(styleElement);
styleSheet = styleElement.sheet;
styleSheet.insertRule(style, styleSheet.cssRules.length);
}
/**
* 获取 IE 浏览器版本
*/
export function getIEVersion(): number {
if (typeof navigator === 'undefined' || !navigator) return Number.MAX_SAFE_INTEGER;
const { userAgent } = navigator;
const isIE = userAgent.indexOf('compatible') > -1 && userAgent.indexOf('MSIE') > -1;
const isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf('rv:11.0') > -1;
if (isIE) {
const reIE = /MSIE (\d+\.\d+);/;
const match = userAgent.match(reIE);
if (!match) return -1;
const fIEVersion = parseFloat(match[1]);
return fIEVersion < 7 ? 6 : fIEVersion;
}
if (isIE11) {
return 11;
}
return Number.MAX_SAFE_INTEGER;
}
/**
* 计算滚动条宽度(基于 CSS 设置)
*/
export function getScrollbarWidthWithCSS(): number {
const defaultScrollbarWidth = 6;
if (typeof navigator === 'undefined' || !navigator) return defaultScrollbarWidth;
if (/(Chrome|Safari)/i.test(navigator.userAgent)) return defaultScrollbarWidth;
const scrollDiv = document.createElement('div');
scrollDiv.style.cssText = 'width: 99px; height: 99px; overflow: scroll; position: absolute; top: -9999px;';
document.body.appendChild(scrollDiv);
let scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
document.body.removeChild(scrollDiv);
if (/Firefox/.test(navigator.userAgent)) {
scrollbarWidth -= 4;
}
if (getIEVersion() <= 11) {
scrollbarWidth = 12;
}
return scrollbarWidth;
}
/**
* 计算滚动条宽度
*/
export function getScrollbarWidth(container: HTMLElement = document.body): number {
if (container === document.body) {
return window.innerWidth - document.documentElement.clientWidth;
}
return container.offsetWidth - container.clientWidth;
}
/**
* 检测是否需要 flex gap polyfill
*/
export function getFlexGapPolyFill(): boolean {
if (typeof navigator === 'undefined' || !navigator) return false;
const ua = navigator.userAgent;
const chromeMatch = ua.match(/AppleWebKit.+Chrome\/(.+) Safari\/.+/i);
if (Number(chromeMatch?.[1]?.split('.')[0]) < 100) return true;
const safariMatch = ua.match(/AppleWebKit.+Version\/(.+) Safari\/.+/i);
if (Number(safariMatch?.[1]?.split('.')[0]) < 12) return true;
const ieVersion = getIEVersion();
if (ieVersion <= 11) return true;
const fireFoxMatch = ua.match(/Firefox\/(.+)/i);
if (Number(fireFoxMatch?.[1]?.split('.')[0]) < 100) return true;
return false;
}
const DOM_STYLE_PROPS = [
'padding-top',
'padding-bottom',
'padding-left',
'padding-right',
'font-family',
'font-weight',
'font-size',
'font-variant',
'text-rendering',
'text-transform',
'width',
'text-indent',
'border-width',
'box-sizing',
'line-height',
'letter-spacing',
];
/**
* 计算 dom 元素盒模型尺寸
*/
export function calculateNodeSize(targetElement: HTMLElement) {
if (typeof window === 'undefined') {
return {
paddingSize: 0,
borderSize: 0,
boxSizing: 0,
sizingStyle: '',
};
}
const style = window.getComputedStyle(targetElement);
const boxSizing =
style.getPropertyValue('box-sizing') ||
style.getPropertyValue('-moz-box-sizing') ||
style.getPropertyValue('-webkit-box-sizing');
const paddingSize =
parseFloat(style.getPropertyValue('padding-bottom')) + parseFloat(style.getPropertyValue('padding-top'));
const borderSize =
parseFloat(style.getPropertyValue('border-bottom-width')) + parseFloat(style.getPropertyValue('border-top-width'));
const sizingStyle = DOM_STYLE_PROPS.map((name) => `${name}:${style.getPropertyValue(name)}`).join(';');
return {
paddingSize,
borderSize,
boxSizing,
sizingStyle,
};
}
type CalculateStyleType = {
height?: string;
minHeight?: string;
};
type LimitType = number | null;
const TEXTAREA_STYLE = `
min-height:0 !important;
max-height:none !important;
height:0 !important;
visibility:hidden !important;
overflow:hidden !important;
position:absolute !important;
z-index:-1000 !important;
top:0 !important;
right:0 !important
`;
let hiddenTextarea: HTMLTextAreaElement;
/**
* 计算 textarea 高度
*/
export function calcTextareaHeight(
targetElement: HTMLTextAreaElement,
minRows: LimitType = 1,
maxRows: LimitType = null
): CalculateStyleType {
if (!hiddenTextarea) {
hiddenTextarea = document.createElement('textarea');
document.body.appendChild(hiddenTextarea);
}
const { paddingSize, borderSize, boxSizing, sizingStyle } = calculateNodeSize(targetElement);
hiddenTextarea.setAttribute('style', `${sizingStyle};${TEXTAREA_STYLE}`);
hiddenTextarea.value = targetElement.value || targetElement.placeholder || '';
let height = hiddenTextarea.scrollHeight;
const result: CalculateStyleType = {};
const isBorderbox = boxSizing === 'border-box';
const isContentbox = boxSizing === 'content-box';
if (isBorderbox) {
height += borderSize;
} else if (isContentbox) {
height -= paddingSize;
}
hiddenTextarea.value = '';
const singleRowHeight = hiddenTextarea.scrollHeight - paddingSize;
hiddenTextarea?.parentNode?.removeChild(hiddenTextarea);
// @ts-ignore
hiddenTextarea = null;
const calcHeight = (rows: number) => {
let rowsHeight = singleRowHeight * rows;
if (isBorderbox) {
rowsHeight = rowsHeight + paddingSize + borderSize;
}
return rowsHeight;
};
if (minRows !== null) {
const minHeight = calcHeight(minRows);
height = Math.max(minHeight, height);
result.minHeight = `${minHeight}px`;
}
if (maxRows !== null) {
height = Math.min(calcHeight(maxRows), height);
}
result.height = `${height}px`;
return result;
}
/**
* 获取颜色 token 的色值
* @example getColorTokenColor('--td-brand-color')
*/
export function getColorTokenColor(token: string): string {
if (typeof window === 'undefined') return '';
const targetElement = document?.documentElement;
const styles = getComputedStyle(targetElement);
return styles.getPropertyValue(token).trim() ?? '';
}