1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-30 10:29:48 +02:00
This commit is contained in:
Ian Storm Taylor
2016-06-23 15:51:17 -07:00
parent 00410742c6
commit e5ab110a0a

View File

@@ -70,7 +70,7 @@ const Node = {
}) })
startNode = startNode.merge({ characters }) startNode = startNode.merge({ characters })
node = node.updateDeep(startNode) node = node.updateDescendant(startNode)
return node return node
} }
@@ -107,16 +107,13 @@ const Node = {
// Then add the end parent's nodes to the start parent node. // Then add the end parent's nodes to the start parent node.
const newNodes = startParent.nodes.concat(endParent.nodes) const newNodes = startParent.nodes.concat(endParent.nodes)
startParent = startParent.merge({ nodes: newNodes }) startParent = startParent.merge({ nodes: newNodes })
node = node.updateDeep(startParent) node = node.updateDescendant(startParent)
// Then remove the end parent. // Then remove the end parent.
let endGrandparent = node.getParent(endParent) let endGrandparent = node.getParent(endParent)
if (endGrandparent == node) { node = endGrandparent == node
node = node.removeDescendant(endParent) ? node.removeDescendant(endParent)
} else { : node.updateDescendant(endGrandparent.removeDescendant(endParent))
endGrandparent = endGrandparent.removeDescendant(endParent)
node = node.updateDeep(endGrandparent)
}
// Normalize the node. // Normalize the node.
return node.normalize() return node.normalize()
@@ -352,19 +349,10 @@ const Node = {
getDepth(key, startAt = 1) { getDepth(key, startAt = 1) {
key = normalizeKey(key) key = normalizeKey(key)
this.assertHasDescendant(key) this.assertHasDescendant(key)
if (this.hasChild(key)) return startAt
const shallow = this.nodes.find(node => node.key == key) return this
if (shallow) return startAt .getHighestChild(key)
.getDepth(key, startAt + 1)
const child = this.nodes.find(node => {
return node.kind == 'text'
? null
: node.hasDescendant(key)
})
return child
? child.getDepth(key, startAt + 1)
: null
}, },
/** /**
@@ -412,8 +400,6 @@ const Node = {
getInlinesAtRange(range) { getInlinesAtRange(range) {
range = range.normalize(this) range = range.normalize(this)
// If the range isn't set, return an empty list.
if (range.isUnset) return Inline.createList() if (range.isUnset) return Inline.createList()
return this return this
@@ -502,19 +488,14 @@ const Node = {
key = normalizeKey(key) key = normalizeKey(key)
this.assertHasDescendant(key) this.assertHasDescendant(key)
// Find the shallow matching child. // Calculate the offset of the nodes before the highest child.
const isChild = this.hasChild(key) const child = this.getHighestChild(key)
const child = isChild
? this.getChild(key)
: this.nodes.find(node => node.hasDescendant && node.hasDescendant(key))
// Calculate the offset of the nodes before the child.
const offset = this.nodes const offset = this.nodes
.takeUntil(node => node == child) .takeUntil(node => node == child)
.reduce((offset, child) => offset + child.length, 0) .reduce((offset, child) => offset + child.length, 0)
// Recurse if need be. // Recurse if need be.
return isChild return this.hasChild(key)
? offset ? offset
: offset + child.getOffset(key) : offset + child.getOffset(key)
}, },
@@ -692,7 +673,7 @@ const Node = {
// Update the existing text node. // Update the existing text node.
startNode = startNode.merge({ characters }) startNode = startNode.merge({ characters })
node = node.updateDeep(startNode) node = node.updateDescendant(startNode)
// Normalize the node. // Normalize the node.
return node.normalize() return node.normalize()
@@ -736,7 +717,7 @@ const Node = {
// Update each of the text nodes. // Update each of the text nodes.
texts.forEach((text) => { texts.forEach((text) => {
node = node.updateDeep(text) node = node.updateDescendant(text)
}) })
return node return node
@@ -767,14 +748,14 @@ const Node = {
const second = parent.getNextSibling(firstAdjacent) const second = parent.getNextSibling(firstAdjacent)
const characters = firstAdjacent.characters.concat(second.characters) const characters = firstAdjacent.characters.concat(second.characters)
firstAdjacent = firstAdjacent.merge({ characters }) firstAdjacent = firstAdjacent.merge({ characters })
parent = parent.updateDeep(firstAdjacent) parent = parent.updateDescendant(firstAdjacent)
// Then remove the second node. // Then remove the second node.
parent = parent.removeDescendant(second) parent = parent.removeDescendant(second)
// If the parent isn't this node, it needs to be updated. // If the parent isn't this node, it needs to be updated.
if (parent != node) { if (parent != node) {
node = node.updateDeep(parent) node = node.updateDescendant(parent)
} else { } else {
node = parent node = parent
} }
@@ -826,7 +807,7 @@ const Node = {
if (type) obj.type = type if (type) obj.type = type
if (data) obj.data = data if (data) obj.data = data
block = block.merge(obj) block = block.merge(obj)
node = node.updateDeep(block) node = node.updateDescendant(block)
}) })
return node return node
@@ -861,7 +842,7 @@ const Node = {
if (type) obj.type = type if (type) obj.type = type
if (data) obj.data = data if (data) obj.data = data
inline = inline.merge(obj) inline = inline.merge(obj)
node = node.updateDeep(inline) node = node.updateDescendant(inline)
}) })
return node return node
@@ -917,7 +898,7 @@ const Node = {
node = node.merge({ nodes }) node = node.merge({ nodes })
} else { } else {
parent = parent.merge({ nodes }) parent = parent.merge({ nodes })
node = node.updateDeep(parent) node = node.updateDescendant(parent)
} }
// Normalize the node. // Normalize the node.
@@ -961,7 +942,7 @@ const Node = {
// Update the grandparent. // Update the grandparent.
node = grandparent == node node = grandparent == node
? node.merge({ nodes }) ? node.merge({ nodes })
: node.updateDeep(grandparent.merge({ nodes })) : node.updateDescendant(grandparent.merge({ nodes }))
} }
return node return node
@@ -1003,7 +984,7 @@ const Node = {
// Update the nodes. // Update the nodes.
parent = parent.merge({ nodes }) parent = parent.merge({ nodes })
node = node.updateDeep(parent) node = node.updateDescendant(parent)
return node return node
}, },
@@ -1044,7 +1025,7 @@ const Node = {
// Update each of the text nodes. // Update each of the text nodes.
texts.forEach((text) => { texts.forEach((text) => {
node = node.updateDeep(text) node = node.updateDescendant(text)
}) })
return node return node
@@ -1057,7 +1038,7 @@ const Node = {
* @return {Node} node * @return {Node} node
*/ */
updateDeep(node) { updateDescendant(node) {
// this.assertHasDescendant(key) // this.assertHasDescendant(key)
const shallow = this.nodes.find(child => child.key == node.key) const shallow = this.nodes.find(child => child.key == node.key)
@@ -1067,7 +1048,7 @@ const Node = {
} }
const nodes = this.nodes.map((child) => { const nodes = this.nodes.map((child) => {
return child.kind == 'text' ? child : child.updateDeep(node) return child.kind == 'text' ? child : child.updateDescendant(node)
}) })
return this.merge({ nodes }) return this.merge({ nodes })
@@ -1125,7 +1106,7 @@ const Node = {
// Update the parent. // Update the parent.
node = parent == node node = parent == node
? node.merge({ nodes }) ? node.merge({ nodes })
: node.updateDeep(parent.merge({ nodes })) : node.updateDescendant(parent.merge({ nodes }))
return node return node
}, },
@@ -1178,7 +1159,7 @@ const Node = {
// Update the parent. // Update the parent.
node = parent == node node = parent == node
? node.merge({ nodes }) ? node.merge({ nodes })
: node.updateDeep(parent.merge({ nodes })) : node.updateDescendant(parent.merge({ nodes }))
}) })
return node.normalize() return node.normalize()
@@ -1254,7 +1235,7 @@ const Node = {
// Update the parent. // Update the parent.
node = parent == node node = parent == node
? node.merge({ nodes }) ? node.merge({ nodes })
: node.updateDeep(parent.merge({ nodes })) : node.updateDescendant(parent.merge({ nodes }))
}) })
return node return node
@@ -1309,7 +1290,7 @@ const Node = {
// Update the parent. // Update the parent.
node = parent == node node = parent == node
? node.merge({ nodes }) ? node.merge({ nodes })
: node.updateDeep(parent.merge({ nodes })) : node.updateDescendant(parent.merge({ nodes }))
}) })
return node.normalize() return node.normalize()