|
1 | | -import { ENRICHED_TEXT_CLASSNAME } from '../constants/classNames'; |
2 | | - |
3 | | -const BLOCK_TAGS = new Set([ |
4 | | - 'P', |
5 | | - 'H1', |
6 | | - 'H2', |
7 | | - 'H3', |
8 | | - 'H4', |
9 | | - 'H5', |
10 | | - 'H6', |
11 | | - 'LI', |
12 | | - 'BLOCKQUOTE', |
13 | | - 'CODEBLOCK', |
14 | | -]); |
15 | | - |
| 1 | +import { |
| 2 | + createSandbox, |
| 3 | + eatBackwardUntilFits, |
| 4 | + removeAfterTarget, |
| 5 | + scanLines, |
| 6 | +} from './utils'; |
| 7 | + |
| 8 | +// Truncate the content to `numberOfLines` by hard-cutting the overflow, with no |
| 9 | +// ellipsis. We render the full HTML into a hidden sandbox and do a single |
| 10 | +// forward line scan to learn where each rendered line begins. If the content |
| 11 | +// fits, we keep it as it is. Otherwise we take the first node on the first |
| 12 | +// forbidden line (line N+1) as the target, drop everything "to the right" of it |
| 13 | +// in the document context, then trim backwards one unit at a time |
| 14 | +// (character / img / br / empty block) until what remains fits within line N. |
| 15 | +// This works almost the same as tail, only without re-anchoring an ellipsis as we shrink. |
16 | 16 | export function clip( |
17 | 17 | container: HTMLDivElement, |
18 | 18 | finalHtml: string, |
19 | 19 | numberOfLines: number, |
20 | 20 | setClampedHtml: (clampedHtml: string) => void |
21 | 21 | ) { |
22 | | - const NUMBER_OF_LINES = numberOfLines; |
23 | | - |
24 | | - // setup the hidden sandbox |
25 | | - const sandbox = document.createElement('div'); |
26 | | - const computedStyle = window.getComputedStyle(container); |
27 | | - |
28 | | - sandbox.style.position = 'absolute'; |
29 | | - sandbox.style.visibility = 'hidden'; |
30 | | - sandbox.style.top = '-9999px'; |
31 | | - |
32 | | - // copy exact CSS properties that affect text wrapping |
33 | | - sandbox.style.cssText = container.style.cssText; |
34 | | - sandbox.style.width = computedStyle.width; |
35 | | - sandbox.style.boxSizing = computedStyle.boxSizing; |
36 | | - sandbox.style.fontFamily = computedStyle.fontFamily; |
37 | | - sandbox.style.fontSize = computedStyle.fontSize; |
38 | | - sandbox.style.lineHeight = computedStyle.lineHeight; |
39 | | - sandbox.style.letterSpacing = computedStyle.letterSpacing; |
40 | | - sandbox.style.padding = computedStyle.padding; |
41 | | - |
42 | | - sandbox.className = ENRICHED_TEXT_CLASSNAME; |
43 | | - sandbox.innerHTML = finalHtml; |
44 | | - document.body.appendChild(sandbox); |
45 | | - |
46 | | - const walkerFilter = { |
47 | | - acceptNode: (n: Node) => { |
48 | | - if (n.nodeType === Node.TEXT_NODE) return NodeFilter.FILTER_ACCEPT; |
49 | | - if (n.nodeName === 'IMG' || n.nodeName === 'BR') |
50 | | - return NodeFilter.FILTER_ACCEPT; |
51 | | - |
52 | | - // let the walker see the empty blocks |
53 | | - if (n.nodeType === Node.ELEMENT_NODE && BLOCK_TAGS.has(n.nodeName)) { |
54 | | - const el = n as HTMLElement; |
55 | | - const textEmpty = !el.textContent?.trim(); |
56 | | - const hasImg = !!el.querySelector('img'); |
57 | | - if (textEmpty && !hasImg) return NodeFilter.FILTER_ACCEPT; |
58 | | - } |
| 22 | + const { sandbox, lineTolerance } = createSandbox(container, finalHtml); |
59 | 23 |
|
60 | | - return NodeFilter.FILTER_SKIP; |
61 | | - }, |
62 | | - }; |
63 | | - |
64 | | - const walker = document.createTreeWalker( |
| 24 | + // forward scan - we find the first element that overflows to the forbidden line |
| 25 | + const { lineStarts, lineBottoms, lastLine } = scanLines( |
65 | 26 | sandbox, |
66 | | - NodeFilter.SHOW_ALL, |
67 | | - walkerFilter |
| 27 | + lineTolerance |
68 | 28 | ); |
69 | | - const range = document.createRange(); |
70 | 29 |
|
71 | | - let currentLine = 1; |
72 | | - let lastBottom: number | null = null; |
73 | 30 | // the node that starts the overflow |
74 | | - let targetNode: Node | null = null; |
75 | | - |
76 | | - // forward scan - we find the first element that overflows to the forbidden line |
77 | | - let node: Node | null; |
78 | | - while ((node = walker.nextNode())) { |
79 | | - if (node.nodeType === Node.TEXT_NODE) { |
80 | | - const textNode = node as Text; |
81 | | - const text = textNode.nodeValue || ''; |
82 | | - |
83 | | - for (let i = 0; i < text.length; i++) { |
84 | | - range.setStart(textNode, i); |
85 | | - range.setEnd(textNode, i + 1); |
86 | | - const rect = range.getBoundingClientRect(); |
87 | | - |
88 | | - if (rect.height === 0) continue; |
89 | | - |
90 | | - if (lastBottom !== null && rect.bottom > lastBottom + 4) { |
91 | | - currentLine++; |
92 | | - } |
93 | | - |
94 | | - if (currentLine > NUMBER_OF_LINES) { |
95 | | - targetNode = textNode; |
96 | | - break; |
97 | | - } |
98 | | - lastBottom = rect.bottom; |
99 | | - } |
100 | | - } else if ( |
101 | | - node.nodeName === 'IMG' || |
102 | | - node.nodeName === 'BR' || |
103 | | - (node.nodeType === Node.ELEMENT_NODE && BLOCK_TAGS.has(node.nodeName)) |
104 | | - ) { |
105 | | - const el = node as HTMLElement; |
106 | | - const rect = el.getBoundingClientRect(); |
107 | | - |
108 | | - if (rect.height === 0) continue; |
109 | | - |
110 | | - if (lastBottom !== null && rect.bottom > lastBottom + 4) { |
111 | | - currentLine++; |
112 | | - } |
113 | | - |
114 | | - if (currentLine > NUMBER_OF_LINES) { |
115 | | - targetNode = el; |
116 | | - break; |
117 | | - } |
118 | | - lastBottom = rect.bottom; |
119 | | - } |
120 | | - |
121 | | - if (targetNode) break; |
122 | | - } |
| 31 | + const targetNode = |
| 32 | + lastLine > numberOfLines ? lineStarts[numberOfLines + 1] : undefined; |
123 | 33 |
|
124 | 34 | // we remove content until everything fits within the given number of lines |
125 | 35 | if (targetNode) { |
126 | 36 | // remove all content on "the right" of the targetNode |
127 | | - let current: Node | null = targetNode; |
128 | | - while (current && current !== sandbox) { |
129 | | - let sibling = current.nextSibling; |
130 | | - while (sibling) { |
131 | | - const next = sibling.nextSibling; |
132 | | - sibling.parentNode?.removeChild(sibling); |
133 | | - sibling = next; |
134 | | - } |
135 | | - current = current.parentNode; |
136 | | - } |
137 | | - |
138 | | - if (targetNode.nodeName === 'IMG') { |
139 | | - targetNode.parentNode?.removeChild(targetNode); |
140 | | - } |
| 37 | + removeAfterTarget(targetNode.node, sandbox); |
141 | 38 |
|
142 | 39 | // remove content backwards until it fits - unlike tail, clip adds no ellipsis |
143 | | - let isOverflowing = true; |
144 | | - while (isOverflowing) { |
145 | | - const backwardWalker = document.createTreeWalker( |
146 | | - sandbox, |
147 | | - NodeFilter.SHOW_ALL, |
148 | | - walkerFilter |
149 | | - ); |
150 | | - |
151 | | - let lastNode: Node | null = null; |
152 | | - while (backwardWalker.nextNode()) { |
153 | | - lastNode = backwardWalker.currentNode; |
154 | | - } |
155 | | - |
156 | | - if (!lastNode) break; |
157 | | - |
158 | | - // we have to handle inline images and <br> separately |
159 | | - if (lastNode.nodeName === 'IMG' || lastNode.nodeName === 'BR') { |
160 | | - range.selectNodeContents(lastNode); |
161 | | - const rect = range.getBoundingClientRect(); |
162 | | - |
163 | | - // check if the image or <br> pushed us onto the forbidden line |
164 | | - if (lastBottom !== null && rect.bottom > lastBottom + 4) { |
165 | | - // it overflowed |
166 | | - lastNode.parentNode?.removeChild(lastNode); |
167 | | - } else { |
168 | | - // it fits |
169 | | - isOverflowing = false; |
170 | | - } |
171 | | - } |
172 | | - // handle empty blocks (like an empty <li>) |
173 | | - else if ( |
174 | | - lastNode.nodeType === Node.ELEMENT_NODE && |
175 | | - BLOCK_TAGS.has(lastNode.nodeName) |
176 | | - ) { |
177 | | - const rect = (lastNode as HTMLElement).getBoundingClientRect(); |
178 | | - |
179 | | - if (lastBottom !== null && rect.bottom > lastBottom + 4) { |
180 | | - lastNode.parentNode?.removeChild(lastNode); |
181 | | - } else { |
182 | | - isOverflowing = false; |
183 | | - } |
184 | | - } |
185 | | - // handling normal text nodes |
186 | | - else { |
187 | | - const lastTextNode = lastNode as Text; |
188 | | - let text = lastTextNode.nodeValue || ''; |
189 | | - |
190 | | - if (text.trim().length === 0) { |
191 | | - let parent = lastTextNode.parentNode; |
192 | | - lastTextNode.parentNode?.removeChild(lastTextNode); |
193 | | - |
194 | | - // if text removal resulted in an empty block, we have to remove it |
195 | | - while (parent && parent !== sandbox) { |
196 | | - const el = parent as HTMLElement; |
197 | | - const textEmpty = !el.textContent?.trim(); |
198 | | - const hasImg = !!el.querySelector('img'); |
199 | | - const hasBr = !!el.querySelector('br'); |
200 | | - |
201 | | - if ( |
202 | | - parent.childNodes.length === 0 || |
203 | | - (textEmpty && !hasImg && !hasBr) |
204 | | - ) { |
205 | | - const p = parent.parentNode; |
206 | | - parent.parentNode?.removeChild(parent); |
207 | | - parent = p; |
208 | | - } else { |
209 | | - break; |
210 | | - } |
211 | | - } |
212 | | - continue; |
213 | | - } |
214 | | - |
215 | | - // Measure the current text node |
216 | | - range.setStart(lastTextNode, 0); |
217 | | - range.setEnd(lastTextNode, lastTextNode.nodeValue!.length); |
218 | | - const rect = range.getBoundingClientRect(); |
219 | | - |
220 | | - // check if it still overflows |
221 | | - if (lastBottom !== null && rect.bottom > lastBottom + 4) { |
222 | | - // it overflows, delete one character from the end |
223 | | - lastTextNode.nodeValue = text.slice(0, -1); |
224 | | - } else { |
225 | | - // it fits perfectly |
226 | | - isOverflowing = false; |
227 | | - } |
228 | | - } |
229 | | - } |
| 40 | + eatBackwardUntilFits( |
| 41 | + sandbox, |
| 42 | + lineBottoms[numberOfLines] ?? null, |
| 43 | + lineTolerance, |
| 44 | + false |
| 45 | + ); |
230 | 46 |
|
231 | 47 | setClampedHtml(sandbox.innerHTML); |
232 | 48 | } else { |
233 | 49 | setClampedHtml(finalHtml); |
234 | 50 | } |
235 | 51 |
|
236 | | - document.body.removeChild(sandbox); |
| 52 | + container.removeChild(sandbox); |
237 | 53 | } |
0 commit comments