Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/browser/fastDomNode.ts
3292 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
export class FastDomNode<T extends HTMLElement> {
7
8
private _maxWidth: string = '';
9
private _width: string = '';
10
private _height: string = '';
11
private _top: string = '';
12
private _left: string = '';
13
private _bottom: string = '';
14
private _right: string = '';
15
private _paddingTop: string = '';
16
private _paddingLeft: string = '';
17
private _paddingBottom: string = '';
18
private _paddingRight: string = '';
19
private _fontFamily: string = '';
20
private _fontWeight: string = '';
21
private _fontSize: string = '';
22
private _fontStyle: string = '';
23
private _fontFeatureSettings: string = '';
24
private _fontVariationSettings: string = '';
25
private _textDecoration: string = '';
26
private _lineHeight: string = '';
27
private _letterSpacing: string = '';
28
private _className: string = '';
29
private _display: string = '';
30
private _position: string = '';
31
private _visibility: string = '';
32
private _color: string = '';
33
private _backgroundColor: string = '';
34
private _layerHint: boolean = false;
35
private _contain: 'none' | 'strict' | 'content' | 'size' | 'layout' | 'style' | 'paint' = 'none';
36
private _boxShadow: string = '';
37
38
constructor(
39
public readonly domNode: T
40
) { }
41
42
public focus(): void {
43
this.domNode.focus();
44
}
45
46
public setMaxWidth(_maxWidth: number | string): void {
47
const maxWidth = numberAsPixels(_maxWidth);
48
if (this._maxWidth === maxWidth) {
49
return;
50
}
51
this._maxWidth = maxWidth;
52
this.domNode.style.maxWidth = this._maxWidth;
53
}
54
55
public setWidth(_width: number | string): void {
56
const width = numberAsPixels(_width);
57
if (this._width === width) {
58
return;
59
}
60
this._width = width;
61
this.domNode.style.width = this._width;
62
}
63
64
public setHeight(_height: number | string): void {
65
const height = numberAsPixels(_height);
66
if (this._height === height) {
67
return;
68
}
69
this._height = height;
70
this.domNode.style.height = this._height;
71
}
72
73
public setTop(_top: number | string): void {
74
const top = numberAsPixels(_top);
75
if (this._top === top) {
76
return;
77
}
78
this._top = top;
79
this.domNode.style.top = this._top;
80
}
81
82
public setLeft(_left: number | string): void {
83
const left = numberAsPixels(_left);
84
if (this._left === left) {
85
return;
86
}
87
this._left = left;
88
this.domNode.style.left = this._left;
89
}
90
91
public setBottom(_bottom: number | string): void {
92
const bottom = numberAsPixels(_bottom);
93
if (this._bottom === bottom) {
94
return;
95
}
96
this._bottom = bottom;
97
this.domNode.style.bottom = this._bottom;
98
}
99
100
public setRight(_right: number | string): void {
101
const right = numberAsPixels(_right);
102
if (this._right === right) {
103
return;
104
}
105
this._right = right;
106
this.domNode.style.right = this._right;
107
}
108
109
public setPaddingTop(_paddingTop: number | string): void {
110
const paddingTop = numberAsPixels(_paddingTop);
111
if (this._paddingTop === paddingTop) {
112
return;
113
}
114
this._paddingTop = paddingTop;
115
this.domNode.style.paddingTop = this._paddingTop;
116
}
117
118
public setPaddingLeft(_paddingLeft: number | string): void {
119
const paddingLeft = numberAsPixels(_paddingLeft);
120
if (this._paddingLeft === paddingLeft) {
121
return;
122
}
123
this._paddingLeft = paddingLeft;
124
this.domNode.style.paddingLeft = this._paddingLeft;
125
}
126
127
public setPaddingBottom(_paddingBottom: number | string): void {
128
const paddingBottom = numberAsPixels(_paddingBottom);
129
if (this._paddingBottom === paddingBottom) {
130
return;
131
}
132
this._paddingBottom = paddingBottom;
133
this.domNode.style.paddingBottom = this._paddingBottom;
134
}
135
136
public setPaddingRight(_paddingRight: number | string): void {
137
const paddingRight = numberAsPixels(_paddingRight);
138
if (this._paddingRight === paddingRight) {
139
return;
140
}
141
this._paddingRight = paddingRight;
142
this.domNode.style.paddingRight = this._paddingRight;
143
}
144
145
public setFontFamily(fontFamily: string): void {
146
if (this._fontFamily === fontFamily) {
147
return;
148
}
149
this._fontFamily = fontFamily;
150
this.domNode.style.fontFamily = this._fontFamily;
151
}
152
153
public setFontWeight(fontWeight: string): void {
154
if (this._fontWeight === fontWeight) {
155
return;
156
}
157
this._fontWeight = fontWeight;
158
this.domNode.style.fontWeight = this._fontWeight;
159
}
160
161
public setFontSize(_fontSize: number | string): void {
162
const fontSize = numberAsPixels(_fontSize);
163
if (this._fontSize === fontSize) {
164
return;
165
}
166
this._fontSize = fontSize;
167
this.domNode.style.fontSize = this._fontSize;
168
}
169
170
public setFontStyle(fontStyle: string): void {
171
if (this._fontStyle === fontStyle) {
172
return;
173
}
174
this._fontStyle = fontStyle;
175
this.domNode.style.fontStyle = this._fontStyle;
176
}
177
178
public setFontFeatureSettings(fontFeatureSettings: string): void {
179
if (this._fontFeatureSettings === fontFeatureSettings) {
180
return;
181
}
182
this._fontFeatureSettings = fontFeatureSettings;
183
this.domNode.style.fontFeatureSettings = this._fontFeatureSettings;
184
}
185
186
public setFontVariationSettings(fontVariationSettings: string): void {
187
if (this._fontVariationSettings === fontVariationSettings) {
188
return;
189
}
190
this._fontVariationSettings = fontVariationSettings;
191
this.domNode.style.fontVariationSettings = this._fontVariationSettings;
192
}
193
194
public setTextDecoration(textDecoration: string): void {
195
if (this._textDecoration === textDecoration) {
196
return;
197
}
198
this._textDecoration = textDecoration;
199
this.domNode.style.textDecoration = this._textDecoration;
200
}
201
202
public setLineHeight(_lineHeight: number | string): void {
203
const lineHeight = numberAsPixels(_lineHeight);
204
if (this._lineHeight === lineHeight) {
205
return;
206
}
207
this._lineHeight = lineHeight;
208
this.domNode.style.lineHeight = this._lineHeight;
209
}
210
211
public setLetterSpacing(_letterSpacing: number | string): void {
212
const letterSpacing = numberAsPixels(_letterSpacing);
213
if (this._letterSpacing === letterSpacing) {
214
return;
215
}
216
this._letterSpacing = letterSpacing;
217
this.domNode.style.letterSpacing = this._letterSpacing;
218
}
219
220
public setClassName(className: string): void {
221
if (this._className === className) {
222
return;
223
}
224
this._className = className;
225
this.domNode.className = this._className;
226
}
227
228
public toggleClassName(className: string, shouldHaveIt?: boolean): void {
229
this.domNode.classList.toggle(className, shouldHaveIt);
230
this._className = this.domNode.className;
231
}
232
233
public setDisplay(display: string): void {
234
if (this._display === display) {
235
return;
236
}
237
this._display = display;
238
this.domNode.style.display = this._display;
239
}
240
241
public setPosition(position: string): void {
242
if (this._position === position) {
243
return;
244
}
245
this._position = position;
246
this.domNode.style.position = this._position;
247
}
248
249
public setVisibility(visibility: string): void {
250
if (this._visibility === visibility) {
251
return;
252
}
253
this._visibility = visibility;
254
this.domNode.style.visibility = this._visibility;
255
}
256
257
public setColor(color: string): void {
258
if (this._color === color) {
259
return;
260
}
261
this._color = color;
262
this.domNode.style.color = this._color;
263
}
264
265
public setBackgroundColor(backgroundColor: string): void {
266
if (this._backgroundColor === backgroundColor) {
267
return;
268
}
269
this._backgroundColor = backgroundColor;
270
this.domNode.style.backgroundColor = this._backgroundColor;
271
}
272
273
public setLayerHinting(layerHint: boolean): void {
274
if (this._layerHint === layerHint) {
275
return;
276
}
277
this._layerHint = layerHint;
278
this.domNode.style.transform = this._layerHint ? 'translate3d(0px, 0px, 0px)' : '';
279
}
280
281
public setBoxShadow(boxShadow: string): void {
282
if (this._boxShadow === boxShadow) {
283
return;
284
}
285
this._boxShadow = boxShadow;
286
this.domNode.style.boxShadow = boxShadow;
287
}
288
289
public setContain(contain: 'none' | 'strict' | 'content' | 'size' | 'layout' | 'style' | 'paint'): void {
290
if (this._contain === contain) {
291
return;
292
}
293
this._contain = contain;
294
(<any>this.domNode.style).contain = this._contain;
295
}
296
297
public setAttribute(name: string, value: string): void {
298
this.domNode.setAttribute(name, value);
299
}
300
301
public removeAttribute(name: string): void {
302
this.domNode.removeAttribute(name);
303
}
304
305
public appendChild(child: FastDomNode<T>): void {
306
this.domNode.appendChild(child.domNode);
307
}
308
309
public removeChild(child: FastDomNode<T>): void {
310
this.domNode.removeChild(child.domNode);
311
}
312
}
313
314
function numberAsPixels(value: number | string): string {
315
return (typeof value === 'number' ? `${value}px` : value);
316
}
317
318
export function createFastDomNode<T extends HTMLElement>(domNode: T): FastDomNode<T> {
319
return new FastDomNode(domNode);
320
}
321
322