1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-07-31 20:40:19 +02:00

add getHighestChild

This commit is contained in:
Ian Storm Taylor
2016-06-23 15:42:46 -07:00
parent 1c45670ff8
commit 00410742c6

View File

@@ -95,19 +95,12 @@ const Node = {
// Then remove any nodes in between the top-most start and end nodes...
let startParent = node.getParent(startKey)
let endParent = node.getParent(endKey)
const startGrandestParent = node.nodes.find((child) => {
return child == startParent || child.hasDescendant(startParent)
})
const endGrandestParent = node.nodes.find((child) => {
return child == endParent || child.hasDescendant(endParent)
})
const startAncestor = node.getHighestChild(startParent)
const endAncestor = node.getHighestChild(endParent)
const nodes = node.nodes
.takeUntil(child => child == startGrandestParent)
.push(startGrandestParent)
.concat(node.nodes.skipUntil(child => child == endGrandestParent))
.takeUntil(child => child == startAncestor)
.push(startAncestor)
.concat(node.nodes.skipUntil(child => child == endAncestor))
node = node.merge({ nodes })
@@ -320,6 +313,22 @@ const Node = {
return this.nodes.find(node => node.key == key)
},
/**
* Get the highest child ancestor of a node by `key`.
*
* @param {String or Node} key
* @return {Node or Null} node
*/
getHighestChild(key) {
key = normalizeKey(key)
return this.nodes.find(node => {
if (node.key == key) return true
if (node.kind == 'text') return false
return node.hasDescendant(key)
})
},
/**
* Get a descendant node by `key`.
*