-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtruncateHandler.ts
More file actions
144 lines (122 loc) · 4.46 KB
/
truncateHandler.ts
File metadata and controls
144 lines (122 loc) · 4.46 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
/**
* Utility module for handling -webkit-line-clamp, overflow, and white-space properties
* when elements are focused/unfocused in the visual builder
*/
interface LineClampStyles {
webkitLineClamp?: string;
overflow?: string;
display?: string;
webkitBoxOrient?: string;
whiteSpace?: string;
}
// WeakMap to store original styles for each element
const originalStyles = new WeakMap<Element, LineClampStyles>();
/**
* Removes truncate related styles from an element and stores the original values
* @param element - The element to remove line-clamp and white-space styles from
*/
export function removeTruncateStyles(element: Element): void {
if (!element || !(element instanceof HTMLElement)) return;
const computedStyles = window.getComputedStyle(element);
const styles: LineClampStyles = {};
// Store original styles if they exist
if (
computedStyles.webkitLineClamp &&
computedStyles.webkitLineClamp !== "none"
) {
styles.webkitLineClamp = computedStyles.webkitLineClamp;
}
if (computedStyles.overflow && computedStyles.overflow !== "visible") {
styles.overflow = computedStyles.overflow;
}
if (computedStyles.display && computedStyles.display.includes("box")) {
styles.display = computedStyles.display;
}
if (
computedStyles.webkitBoxOrient &&
computedStyles.webkitBoxOrient !== "horizontal"
) {
styles.webkitBoxOrient = computedStyles.webkitBoxOrient;
}
if (computedStyles.whiteSpace && computedStyles.whiteSpace === "nowrap") {
styles.whiteSpace = computedStyles.whiteSpace;
}
// Only store if we found line-clamp related styles
if (Object.keys(styles).length > 0) {
originalStyles.set(element, styles);
// Remove the styles
element.style.webkitLineClamp = "none";
element.style.overflow = "visible";
// Reset display if it was -webkit-box
if (styles.display?.includes("box")) {
element.style.display = "block";
}
if (styles.webkitBoxOrient) {
element.style.webkitBoxOrient = "horizontal";
}
if (styles.whiteSpace) {
element.style.whiteSpace = "normal";
}
}
}
/**
* Restores the original truncate related styles to an element
* @param element - The element to restore line-clamp and white-space styles to
*/
export function restoreTruncateStyles(element: Element): void {
if (!element || !(element instanceof HTMLElement)) return;
const storedStyles = originalStyles.get(element);
if (!storedStyles) return;
// Restore original styles
if (storedStyles.webkitLineClamp) {
element.style.webkitLineClamp = storedStyles.webkitLineClamp;
} else {
element.style.removeProperty("-webkit-line-clamp");
}
if (storedStyles.overflow) {
element.style.overflow = storedStyles.overflow;
} else {
element.style.removeProperty("overflow");
}
if (storedStyles.display) {
element.style.display = storedStyles.display;
} else {
element.style.removeProperty("display");
}
if (storedStyles.webkitBoxOrient) {
element.style.webkitBoxOrient = storedStyles.webkitBoxOrient;
} else {
element.style.removeProperty("-webkit-box-orient");
}
if (storedStyles.whiteSpace) {
element.style.whiteSpace = storedStyles.whiteSpace;
} else {
element.style.removeProperty("white-space");
}
// Clean up stored styles
originalStyles.delete(element);
}
/**
* Checks if an element has truncate or white-space styles applied
* @param element - The element to check
* @returns boolean indicating if the element has line-clamp or white-space styles
*/
export function hasTruncateStyles(element: Element): boolean {
if (!element || !(element instanceof HTMLElement)) return false;
const computedStyles = window.getComputedStyle(element);
return (
computedStyles.webkitLineClamp !== "none" ||
(computedStyles.overflow === "hidden" &&
computedStyles.display?.includes("box") &&
computedStyles.webkitBoxOrient === "vertical") ||
computedStyles.whiteSpace === "nowrap"
);
}
/**
* Checks if an element currently has stored truncate styles
* @param element - The element to check
* @returns boolean indicating if the element has stored styles
*/
export function hasStoredLineClampStyles(element: Element): boolean {
return originalStyles.has(element);
}