mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-11 17:53:59 +02:00
add getHighestChild
This commit is contained in:
@@ -95,19 +95,12 @@ const Node = {
|
|||||||
// Then remove any nodes in between the top-most start and end nodes...
|
// Then remove any nodes in between the top-most start and end nodes...
|
||||||
let startParent = node.getParent(startKey)
|
let startParent = node.getParent(startKey)
|
||||||
let endParent = node.getParent(endKey)
|
let endParent = node.getParent(endKey)
|
||||||
|
const startAncestor = node.getHighestChild(startParent)
|
||||||
const startGrandestParent = node.nodes.find((child) => {
|
const endAncestor = node.getHighestChild(endParent)
|
||||||
return child == startParent || child.hasDescendant(startParent)
|
|
||||||
})
|
|
||||||
|
|
||||||
const endGrandestParent = node.nodes.find((child) => {
|
|
||||||
return child == endParent || child.hasDescendant(endParent)
|
|
||||||
})
|
|
||||||
|
|
||||||
const nodes = node.nodes
|
const nodes = node.nodes
|
||||||
.takeUntil(child => child == startGrandestParent)
|
.takeUntil(child => child == startAncestor)
|
||||||
.push(startGrandestParent)
|
.push(startAncestor)
|
||||||
.concat(node.nodes.skipUntil(child => child == endGrandestParent))
|
.concat(node.nodes.skipUntil(child => child == endAncestor))
|
||||||
|
|
||||||
node = node.merge({ nodes })
|
node = node.merge({ nodes })
|
||||||
|
|
||||||
@@ -320,6 +313,22 @@ const Node = {
|
|||||||
return this.nodes.find(node => node.key == key)
|
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`.
|
* Get a descendant node by `key`.
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user