MCPcopy Create free account
hub / github.com/editablejs/editable / matchHighest

Function matchHighest

packages/editor/src/utils/selection.ts:184–255  ·  view source on GitHub ↗
(editor: Editor, element: DOMElement, top: number, bottom: number)

Source from the content-addressed store, hash-verified

182 * @returns - Object containing the line rect information
183 */
184const matchHighest = (editor: Editor, element: DOMElement, top: number, bottom: number) => {
185 const lineRect = {
186 top,
187 height: bottom - top,
188 bottom,
189 }
190
191 /**
192 * Compare the height of the current rect with the line rect
193 * and update the line rect with the highest values
194 * @param rect - The current rect
195 */
196 const compareHeight = (rect: DOMRect) => {
197 if (isRectInLine(rect, lineRect)) {
198 const newTop = lineRect.top < rect.top ? lineRect.top : rect.top
199 const newBottom = lineRect.bottom > rect.bottom ? lineRect.bottom : rect.bottom
200 lineRect.height = newBottom - newTop
201 lineRect.top = newTop
202 lineRect.bottom = newBottom
203 }
204 }
205
206 /**
207 * Recursively find the child nodes of the element and compare their rects
208 * @param element - The DOM element
209 */
210 const match = (element: DOMElement) => {
211 for (const child of element.childNodes) {
212 if (isDOMElement(child)) {
213 const hasNode = child.hasAttribute(DATA_EDITABLE_NODE)
214 const node = hasNode ? Editable.toEditorNode(editor, child) : null
215 if (node) {
216 if (Element.isElement(node)) {
217 if (editor.isVoid(node)) {
218 const rect = resetElementRect(
219 child.getBoundingClientRect(),
220 calculateElementHeight(child),
221 )
222 compareHeight(rect)
223 } else if (editor.isInline(node)) {
224 const height = calculateElementHeight(child)
225 const rects = child.getClientRects()
226 for (let r = 0; r < rects.length; r++) {
227 const rect = resetElementRect(rects[r], height)
228 compareHeight(rect)
229 }
230 } else {
231 match(child)
232 }
233 } else {
234 const nodes = child.querySelectorAll(
235 `[${DATA_EDITABLE_STRING}], [${DATA_EDITABLE_COMPOSITION}], [${DATA_EDITABLE_ZERO_WIDTH}]`,
236 )
237 nodes.forEach(node => {
238 const height = calculateElementHeight(node)
239 const rects = node.getClientRects()
240 for (let r = 0; r < rects.length; r++) {
241 const rect = resetElementRect(rects[r], height)

Callers 2

getLineRectsByNodeFunction · 0.85
getLineRectsByRangeFunction · 0.85

Calls 1

matchFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…