forked from ianstormtaylor/slate
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfind-dom-point.js
59 lines (46 loc) · 1.43 KB
/
find-dom-point.js
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
import findDOMNode from './find-dom-node'
import warning from 'tiny-warning'
import DATA_ATTRS from '../constants/data-attributes'
import SELECTORS from '../constants/selectors'
/**
* Find a native DOM selection point from a Slate `point`.
*
* @param {Point} point
* @param {Window} win (optional)
* @return {Object|Null}
*/
function findDOMPoint(point, win = window) {
warning(
false,
'As of [email protected] the `findDOMPoint(point)` helper is deprecated in favor of `editor.findDOMPoint(point)`.'
)
const el = findDOMNode(point.key, win)
let start = 0
// For each leaf, we need to isolate its content, which means filtering to its
// direct text and zero-width spans. (We have to filter out any other siblings
// that may have been rendered alongside them.)
const texts = Array.from(
el.querySelectorAll(`${SELECTORS.STRING}, ${SELECTORS.ZERO_WIDTH}`)
)
for (const text of texts) {
const node = text.childNodes[0]
const domLength = node.textContent.length
let slateLength = domLength
if (text.hasAttribute(DATA_ATTRS.LENGTH)) {
slateLength = parseInt(text.getAttribute(DATA_ATTRS.LENGTH), 10)
}
const end = start + slateLength
if (point.offset <= end) {
const offset = Math.min(domLength, Math.max(0, point.offset - start))
return { node, offset }
}
start = end
}
return null
}
/**
* Export.
*
* @type {Function}
*/
export default findDOMPoint