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

Function getEditableChildAndIndex

packages/editor/src/utils/dom.ts:58–96  ·  view source on GitHub ↗
(
  parent: DOMElement,
  index: number,
  direction: 'forward' | 'backward',
)

Source from the content-addressed store, hash-verified

56 */
57
58export const getEditableChildAndIndex = (
59 parent: DOMElement,
60 index: number,
61 direction: 'forward' | 'backward',
62): [DOMNode, number] => {
63 const { childNodes } = parent
64 let child = childNodes[index]
65 let i = index
66 let triedForward = false
67 let triedBackward = false
68
69 // While the child is a comment node, or an element node with no children,
70 // keep iterating to find a sibling non-void, non-comment node.
71 while (isDOMComment(child) || (isDOMElement(child) && child.childNodes.length === 0)) {
72 if (triedForward && triedBackward) {
73 break
74 }
75
76 if (i >= childNodes.length) {
77 triedForward = true
78 i = index - 1
79 direction = 'backward'
80 continue
81 }
82
83 if (i < 0) {
84 triedBackward = true
85 i = index + 1
86 direction = 'forward'
87 continue
88 }
89
90 child = childNodes[i]
91 index = i
92 i += direction === 'forward' ? 1 : -1
93 }
94
95 return [child, index]
96}
97
98/**
99 * Get the nearest editable child at `index` in a `parent`, preferring

Callers 2

normalizeDOMPointFunction · 0.85
getEditableChildFunction · 0.85

Calls 2

isDOMCommentFunction · 0.90
isDOMElementFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…