Skip to content

Commit

Permalink
Reselect block correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Oct 4, 2019
1 parent f1b08ac commit 98dae74
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions packages/block-editor/src/components/block-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down

0 comments on commit 98dae74

Please sign in to comment.