forked from ianstormtaylor/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-node.js
45 lines (36 loc) · 991 Bytes
/
find-node.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
import invariant from 'tiny-invariant'
import warning from 'tiny-warning'
import { Value } from 'slate'
import DATA_ATTRS from '../constants/data-attributes'
import SELECTORS from '../constants/selectors'
/**
* Find a Slate node from a DOM `element`.
*
* @param {Element} element
* @param {Editor} editor
* @return {Node|Null}
*/
function findNode(element, editor) {
warning(
false,
'As of [email protected] the `findNode(element)` helper is deprecated in favor of `editor.findNode(element)`.'
)
invariant(
!Value.isValue(editor),
'As of Slate 0.42.0, the `findNode` utility takes an `editor` instead of a `value`.'
)
const closest = element.closest(SELECTORS.KEY)
if (!closest) return null
const key = closest.getAttribute(DATA_ATTRS.KEY)
if (!key) return null
const { value } = editor
const { document } = value
const node = document.getNode(key)
return node || null
}
/**
* Export.
*
* @type {Function}
*/
export default findNode