Skip to content

Commit

Permalink
Improve drop zone detection
Browse files Browse the repository at this point in the history
  • Loading branch information
talldan committed Aug 7, 2020
1 parent e9fe910 commit f1ca5dc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const BlockNavigationBlockContents = forwardRef(
( select ) =>
select( 'core/block-editor' ).getBlockRootClientId(
block.clientId
),
) || '',
[ block.rootClientId ]
);

Expand All @@ -48,15 +48,27 @@ const BlockNavigationBlockContents = forwardRef(
blockIndex: dropTargetBlockIndex,
} = blockDropTarget;

const isDropTarget =
( dropTargetRootClientId === rootClientId ||
( dropTargetRootClientId === '' &&
rootClientId === undefined ) ) &&
position === dropTargetBlockIndex + 1;
const blockIndex = position - 1;

const isDroppingBefore =
dropTargetRootClientId === rootClientId &&
blockIndex === dropTargetBlockIndex;
const isDroppingAfter =
dropTargetRootClientId === rootClientId &&
position === siblingBlockCount &&
dropTargetBlockIndex === siblingBlockCount;
const isDroppingToInnerBlocks =
block.clientId === dropTargetRootClientId &&
! block.innerBlocks?.length &&
dropTargetBlockIndex === 0;

const className = classnames(
'block-editor-block-navigation-block-contents',
{ 'is-drop-target': isDropTarget }
{
'is-dropping-before': isDroppingBefore,
'is-dropping-after': isDroppingAfter,
'is-dropping-to-inner-blocks': isDroppingToInnerBlocks,
}
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,18 @@ $tree-item-height: 36px;
color: $dark-gray-600;
border-radius: 2px;

&.is-drop-target {
&.is-dropping-before {
border-top: 2px solid blue;
}

&.is-dropping-after {
border-bottom: 2px solid green;
}

&.is-dropping-to-inner-blocks {
border-bottom: 2px solid red;
}

.components-modal__content & {
padding-left: 0;
padding-right: 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ function isPointContainedByRect( point, rect ) {
);
}

function isDroppingToInnerBlocks( point, rect ) {
const blockCenterX = rect.left + rect.width / 2;
return point.x > blockCenterX;
}

// Block navigation is always a vertical list, so only allow dropping
// to the above or below a block.
const ALLOWED_DROP_EDGES = [ 'top', 'bottom' ];
Expand All @@ -72,6 +77,7 @@ function getBlockNavigationDropTarget( blocksData, position ) {
let candidateEdge;
let candidateBlockData;
let candidateDistance;
let candidateRect;

for ( const blockData of blocksData ) {
const rect = blockData.element.getBoundingClientRect();
Expand All @@ -85,6 +91,7 @@ function getBlockNavigationDropTarget( blocksData, position ) {
candidateDistance = distance;
candidateBlockData = blockData;
candidateEdge = edge;
candidateRect = rect;
}

// If the mouse position is within the block, break early
Expand All @@ -104,17 +111,20 @@ function getBlockNavigationDropTarget( blocksData, position ) {

const isDraggingBelow = candidateEdge === 'bottom';

// If the user is dragging towards the bottom of the block interpret that
// they're trying to next the dragged block.
if ( isDraggingBelow && candidateBlockData.canInsertDraggedBlocksAsChild ) {
// If the user is dragging towards the bottom of the block check whether
// they might be inserting as a child.
if (
isDraggingBelow &&
candidateBlockData.canInsertDraggedBlocksAsChild &&
isDroppingToInnerBlocks( position, candidateRect )
) {
return {
rootClientId: candidateBlockData.clientId,
blockIndex: 0,
};
}

const offset = isDraggingBelow ? 1 : 0;

return {
rootClientId: candidateBlockData.rootClientId,
blockIndex: candidateBlockData.blockIndex + offset,
Expand Down

0 comments on commit f1ca5dc

Please sign in to comment.