1 1
const getFirstChild = (elem: HTMLElement): Node | null => elem.firstChild
2

3 1
export const getElementPaddingLeft = (elem: HTMLElement): number => {
4 1
    const firstChild = getFirstChild(elem)
5 1
    if (!firstChild) {
6 0
        return 0
7
    }
8

9 1
    const range = document.createRange()
10 1
    range.selectNodeContents(firstChild)
11

12 1
    return range.getBoundingClientRect().left - elem.getBoundingClientRect().left
13
}
14

15 1
export const getElementOffset = (elem: HTMLElement, useElemOffset = true): number =>
16 1
    (useElemOffset ? elem.getBoundingClientRect().left : 0) + getElementPaddingLeft(elem)
17

18 1
export const getTextNodes = (node: Node): Node[] => {
19 1
    if (node.childNodes.length === 0 && node.TEXT_NODE === node.nodeType && node.nodeValue) {
20 1
        return [node]
21
    }
22

23 1
    const nodes: Node[] = []
24

25 1
    for (const child of Array.from(node.childNodes)) {
26 1
        nodes.push(...getTextNodes(child))
27
    }
28

29 1
    return nodes
30
}

Read our documentation on viewing source code .

Loading