From 4f056285f78e0ed2fc6ed34586f204c601fe8d29 Mon Sep 17 00:00:00 2001 From: Tyler Johnson Date: Tue, 20 Sep 2016 16:28:32 -0600 Subject: [PATCH] wrapBlockAtRange using top-most parent logic --- src/transforms/at-range.js | 47 +++++++------------------------------- 1 file changed, 8 insertions(+), 39 deletions(-) diff --git a/src/transforms/at-range.js b/src/transforms/at-range.js index 29b38fca0..8bd93fcd9 100644 --- a/src/transforms/at-range.js +++ b/src/transforms/at-range.js @@ -725,50 +725,19 @@ export function wrapBlockAtRange(transform, range, block) { const blocks = document.getBlocksAtRange(range) const firstblock = blocks.first() const lastblock = blocks.last() - let parent, siblings, index - // if there is only one block in the selection then we know the parent and siblings - if (blocks.length === 1) { - parent = document.getParent(firstblock) - siblings = blocks - } - - // determine closest shared parent to all blocks in selection - else { - parent = document.getClosest(firstblock, p1 => { - return !!document.getClosest(lastblock, p2 => p1 == p2) - }) - } - - // if no shared parent could be found then the parent is the document - if (parent == null) parent = document - - // create a list of direct children siblings of parent that fall in the selection - if (siblings == null) { - const indexes = parent.nodes.reduce((ind, node, i) => { - if (node == firstblock || node.hasDescendant(firstblock)) ind[0] = i - if (node == lastblock || node.hasDescendant(lastblock)) ind[1] = i - return ind - }, []) - - index = indexes[0] - siblings = parent.nodes.slice(indexes[0], indexes[1] + 1) - } - - // get the index to place the new wrapped node at - if (index == null) { - index = parent.nodes.indexOf(siblings.first()) - } + // get first and last index of root nodes in the selection + const indexes = [] + document.nodes.some((node, i) => { + if (node == firstblock || node.hasDescendant(firstblock)) indexes[0] = i + if (node == lastblock || node.hasDescendant(lastblock)) indexes[1] = i + }) // inject the new block node into the parent - if (parent != document) { - transform.insertNodeByKey(parent.key, index, block) - } else { - transform.insertNodeOperation([], index, block) - } + transform.insertNodeOperation([], indexes[0], block) // move the sibling nodes into the new block node - siblings.forEach((node, i) => { + document.nodes.slice(indexes[0], indexes[1] + 1).forEach((node, i) => { transform.moveNodeByKey(node.key, block.key, i) })