Skip to content

Commit

Permalink
Fix keyboard block duplication (#21317)
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad authored and whyisjake committed Apr 22, 2020
1 parent d87920f commit 7542cff
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ export default function useMultiSelection( ref ) {
);

if (
! blockNode.contains( startContainer ) ||
! blockNode.contains( endContainer )
!! blockNode &&
( ! blockNode.contains( startContainer ) ||
! blockNode.contains( endContainer ) )
) {
selection.removeAllRanges();
}
Expand Down Expand Up @@ -242,17 +243,6 @@ export default function useMultiSelection( ref ) {

startClientId.current = clientId;
anchorElement.current = document.activeElement;
if ( anchorElement.current ) {
const blockInspector = document.querySelector(
'.block-editor-block-inspector'
);
if (
blockInspector &&
blockInspector.contains( anchorElement.current )
) {
return;
}
}
startMultiSelect();

// `onSelectionStart` is called after `mousedown` and `mouseleave`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Duplicating blocks should duplicate blocks using the block settings menu 1`] = `
"<!-- wp:paragraph -->
<p>Clone me</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>Clone me</p>
<!-- /wp:paragraph -->"
`;

exports[`Duplicating blocks should duplicate blocks using the keyboard shortcut 1`] = `
"<!-- wp:paragraph -->
<p>Clone me</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>Clone me</p>
<!-- /wp:paragraph -->"
`;
49 changes: 49 additions & 0 deletions packages/e2e-tests/specs/editor/various/duplicating-blocks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* WordPress dependencies
*/
import {
createNewPost,
insertBlock,
getEditedPostContent,
clickBlockToolbarButton,
pressKeyWithModifier,
} from '@wordpress/e2e-test-utils';

describe( 'Duplicating blocks', () => {
beforeEach( async () => {
await createNewPost();
} );

it( 'should duplicate blocks using the block settings menu', async () => {
await insertBlock( 'Paragraph' );
await page.keyboard.type( 'Clone me' );

// Select the test we just typed
// This doesn't do anything but we previously had a duplicationi bug
// When the selection was not collapsed
await pressKeyWithModifier( 'primary', 'a' );

await clickBlockToolbarButton( 'More options' );
const duplicateButton = await page.waitForXPath(
'//button[text()="Duplicate"]'
);
await duplicateButton.click();

expect( await getEditedPostContent() ).toMatchSnapshot();
} );

it( 'should duplicate blocks using the keyboard shortcut', async () => {
await insertBlock( 'Paragraph' );
await page.keyboard.type( 'Clone me' );

// Select the test we just typed
// This doesn't do anything but we previously had a duplicationi bug
// When the selection was not collapsed
await pressKeyWithModifier( 'primary', 'a' );

// Duplicate using the keyboard shortccut
await pressKeyWithModifier( 'primaryShift', 'd' );

expect( await getEditedPostContent() ).toMatchSnapshot();
} );
} );

0 comments on commit 7542cff

Please sign in to comment.