Skip to content

Commit

Permalink
Fix selection setting
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Oct 4, 2019
1 parent 9182fd3 commit 4eade53
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions packages/block-editor/src/components/block-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@ const forceSyncUpdates = ( WrappedComponent ) => ( props ) => {
);
};

function getDeepestNode( node, type ) {
const child = type === 'start' ? 'firstChild' : 'lastChild';
const sibling = type === 'start' ? 'nextSibling' : 'previousSibling';

while ( node[ child ] ) {
node = node[ child ];

while (
node.nodeType === node.TEXT_NODE &&
/^\s*$/.test( node.data ) &&
node[ sibling ]
) {
node = node[ sibling ];
}
}

return node;
}

class BlockList extends Component {
constructor( props ) {
super( props );
Expand Down Expand Up @@ -70,13 +89,8 @@ class BlockList extends Component {
const selection = window.getSelection();
const range = document.createRange();

while ( startNode.firstChild ) {
startNode = startNode.firstChild;
}

while ( endNode.lastChild ) {
endNode = endNode.lastChild;
}
startNode = getDeepestNode( startNode, 'start' );
endNode = getDeepestNode( endNode, 'end' );

range.setStartBefore( startNode );
range.setEndAfter( endNode );
Expand Down

0 comments on commit 4eade53

Please sign in to comment.