Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix drag and drop for aligned blocks #23916

Merged
merged 3 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ export function getNearestBlockIndex( elements, position, orientation ) {
let candidateDistance;

elements.forEach( ( element, index ) => {
// Ensure the element is a block. It should have the `data-block` attribute.
if ( ! element.dataset.block ) {
// Ensure the element is a block. It should have the `wp-block` class.
if ( ! element.classList.contains( 'wp-block' ) ) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ const elementData = [
},
];

const createMockClassList = ( isDragging ) => {
const createMockClassList = ( classes ) => {
return {
contains() {
return isDragging;
contains( textToMatch ) {
return classes.includes( textToMatch );
},
};
};
Expand All @@ -60,7 +60,7 @@ const mapElements = ( orientation ) => (
right: bottom,
};
},
classList: createMockClassList( false ),
classList: createMockClassList( 'wp-block' ),
};
};

Expand All @@ -83,8 +83,10 @@ describe( 'getNearestBlockIndex', () => {
expect( result ).toBeUndefined();
} );

it( 'returns `undefined` if the elements do not have the `data-block` attribute', () => {
const nonBlockElements = [ { dataset: {} } ];
it( 'returns `undefined` if the elements do not have the `wp-block` class', () => {
const nonBlockElements = [
{ classList: createMockClassList( 'some-other-class' ) },
];
const position = { x: 0, y: 0 };
const orientation = 'horizontal';

Expand Down Expand Up @@ -208,14 +210,14 @@ describe( 'getNearestBlockIndex', () => {
expect( result ).toBe( 4 );
} );

it( 'skips the block being dragged', () => {
it( 'skips the block being dragged by checking for the `is-dragging` classname', () => {
const position = { x: 0, y: 450 };

const verticalElementsWithDraggedBlock = [
...verticalElements.slice( 0, 2 ),
{
...verticalElements[ 2 ],
classList: createMockClassList( true ),
classList: createMockClassList( 'wp-block is-dragging' ),
},
...verticalElements.slice( 3, 4 ),
];
Expand Down Expand Up @@ -341,14 +343,14 @@ describe( 'getNearestBlockIndex', () => {
expect( result ).toBe( 4 );
} );

it( 'skips the block being dragged', () => {
it( 'skips the block being dragged by checking for the `is-dragging` classname', () => {
const position = { x: 450, y: 0 };

const horizontalElementsWithDraggedBlock = [
...horizontalElements.slice( 0, 2 ),
{
...horizontalElements[ 2 ],
classList: createMockClassList( true ),
classList: createMockClassList( 'wp-block is-dragging' ),
},
...horizontalElements.slice( 3, 4 ),
];
Expand Down