Skip to content

Commit

Permalink
Drag and Drop: Skip sibling insertion check if block is moving within…
Browse files Browse the repository at this point in the history
… same parent
  • Loading branch information
yogeshbhutkar committed Jan 7, 2025
1 parent 4b39807 commit 3d84e6e
Showing 1 changed file with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,19 @@ const ALLOWED_DROP_EDGES = [ 'top', 'bottom' ];
/**
* Given blocks data and the cursor position, compute the drop target.
*
* @param {WPListViewDropZoneBlocks} blocksData Data about the blocks in list view.
* @param {WPPoint} position The point representing the cursor position when dragging.
* @param {boolean} rtl Whether the editor is in RTL mode.
* @param {WPListViewDropZoneBlocks} blocksData Data about the blocks in list view.
* @param {WPPoint} position The point representing the cursor position when dragging.
* @param {boolean} rtl Whether the editor is in RTL mode.
* @param {string[]} draggedBlockClientIds The client ids of the dragged blocks.
*
* @return {WPListViewDropZoneTarget | undefined} An object containing data about the drop target.
*/
export function getListViewDropTarget( blocksData, position, rtl = false ) {
export function getListViewDropTarget(
blocksData,
position,
rtl = false,
draggedBlockClientIds = []
) {
let candidateEdge;
let candidateBlockData;
let candidateDistance;
Expand Down Expand Up @@ -383,9 +389,26 @@ export function getListViewDropTarget( blocksData, position, rtl = false ) {
}
}

const draggedBlocksData = draggedBlockClientIds?.map( ( clientId ) =>
blocksData.find( ( blockData ) => blockData.clientId === clientId )
);

// Skip sibling insertion check if block is moving within same parent.
const hasDifferentParent = draggedBlocksData?.some(
( draggedBlock ) =>
! candidateBlockParents?.find(
( blockData ) =>
blockData.clientId === draggedBlock.rootClientId &&
blockData.nestingLevel === draggedBlock.nestingLevel - 1
)
);

// If dropping as a sibling, but block cannot be inserted in
// this context, return early.
if ( ! candidateBlockData.canInsertDraggedBlocksAsSibling ) {
if (
hasDifferentParent &&
! candidateBlockData.canInsertDraggedBlocksAsSibling
) {
return;
}

Expand Down Expand Up @@ -534,7 +557,8 @@ export default function useListViewDropZone( {
const newTarget = getListViewDropTarget(
blocksData,
position,
rtl
rtl,
draggedBlockClientIds
);

if ( newTarget ) {
Expand Down

0 comments on commit 3d84e6e

Please sign in to comment.