From 80887261935aa9d02730b1f3442b132f3a350a43 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Thu, 9 Jul 2020 14:02:25 +0800 Subject: [PATCH 1/3] Fix drag and drop for aligned blocks --- .../block-editor/src/components/use-block-drop-zone/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/block-editor/src/components/use-block-drop-zone/index.js b/packages/block-editor/src/components/use-block-drop-zone/index.js index 6f0f36550196f..b4b3ebd7194a5 100644 --- a/packages/block-editor/src/components/use-block-drop-zone/index.js +++ b/packages/block-editor/src/components/use-block-drop-zone/index.js @@ -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; } From ca7d1b9e3c6d10cf794265231539da338ef1662b Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Tue, 14 Jul 2020 11:59:46 +0800 Subject: [PATCH 2/3] Update the mock class list implementation to be more flexible --- .../components/use-block-drop-zone/test/index.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/block-editor/src/components/use-block-drop-zone/test/index.js b/packages/block-editor/src/components/use-block-drop-zone/test/index.js index ba95d4c7521eb..03fcf7f1d581e 100644 --- a/packages/block-editor/src/components/use-block-drop-zone/test/index.js +++ b/packages/block-editor/src/components/use-block-drop-zone/test/index.js @@ -31,10 +31,10 @@ const elementData = [ }, ]; -const createMockClassList = ( isDragging ) => { +const createMockClassList = ( classes ) => { return { - contains() { - return isDragging; + contains( textToMatch ) { + return classes.includes( textToMatch ); }, }; }; @@ -60,7 +60,7 @@ const mapElements = ( orientation ) => ( right: bottom, }; }, - classList: createMockClassList( false ), + classList: createMockClassList( 'wp-block' ), }; }; @@ -208,14 +208,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 ), ]; @@ -341,14 +341,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 ), ]; From 01705e0d5cf841c31e3177b483779893df062ba3 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Tue, 14 Jul 2020 12:06:57 +0800 Subject: [PATCH 3/3] Fix test --- .../src/components/use-block-drop-zone/test/index.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/block-editor/src/components/use-block-drop-zone/test/index.js b/packages/block-editor/src/components/use-block-drop-zone/test/index.js index 03fcf7f1d581e..c0aac7b5827d9 100644 --- a/packages/block-editor/src/components/use-block-drop-zone/test/index.js +++ b/packages/block-editor/src/components/use-block-drop-zone/test/index.js @@ -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';