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