From 98dae74909a3d7b229cf845e025b3668226458af Mon Sep 17 00:00:00 2001 From: iseulde Date: Fri, 20 Sep 2019 17:26:55 +0300 Subject: [PATCH] Reselect block correctly --- .../src/components/block-list/index.js | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/block-editor/src/components/block-list/index.js b/packages/block-editor/src/components/block-list/index.js index 470620077ccc5e..0100f3463efb87 100644 --- a/packages/block-editor/src/components/block-list/index.js +++ b/packages/block-editor/src/components/block-list/index.js @@ -46,27 +46,37 @@ class BlockList extends Component { componentDidUpdate() { const { hasMultiSelection, - selectionStart, - selectionEnd, blockClientIds, + multiSelectedBlockClientIds, } = this.props; if ( ! hasMultiSelection ) { return; } - const startIndex = blockClientIds.indexOf( selectionStart ); + const { length } = multiSelectedBlockClientIds; + const start = multiSelectedBlockClientIds[ 0 ]; + const end = multiSelectedBlockClientIds[ length - 1 ]; + const startIndex = blockClientIds.indexOf( start ); // The selected block is not in this block list. if ( startIndex === -1 ) { return; } - const startNode = document.querySelector( `[data-block="${ selectionStart }"]` ); - const endNode = document.querySelector( `[data-block="${ selectionEnd }"]` ); + let startNode = document.querySelector( `[data-block="${ start }"]` ); + let endNode = document.querySelector( `[data-block="${ end }"]` ); const selection = window.getSelection(); const range = document.createRange(); + while ( startNode.firstChild ) { + startNode = startNode.firstChild; + } + + while ( endNode.lastChild ) { + endNode = endNode.lastChild; + } + range.setStartBefore( startNode ); range.setEndAfter( endNode );