Skip to content

Commit

Permalink
List: fix pasting (#62428)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Jun 11, 2024
1 parent 3c97445 commit f416fa2
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
findTransform,
getBlockTransforms,
hasBlockSupport,
switchToBlockType,
} from '@wordpress/blocks';
import {
documentHasSelection,
Expand Down Expand Up @@ -208,15 +209,36 @@ export default function useClipboardHandler() {
firstSelectedClientId
);

if (
! blocks.every( ( block ) =>
canInsertBlockType( block.name, rootClientId )
)
) {
return;
const newBlocks = [];

for ( const block of blocks ) {
if ( canInsertBlockType( block.name, rootClientId ) ) {
newBlocks.push( block );
} else {
// If a block cannot be inserted in a root block, try
// converting it to that root block type and insert the
// inner blocks.
// Example: paragraphs cannot be inserted into a list,
// so convert the paragraphs to a list for list items.
const rootBlockName = getBlockName( rootClientId );
const switchedBlocks =
block.name !== rootBlockName
? switchToBlockType( block, rootBlockName )
: [ block ];

if ( ! switchedBlocks ) {
return;
}

for ( const switchedBlock of switchedBlocks ) {
for ( const innerBlock of switchedBlock.innerBlocks ) {
newBlocks.push( innerBlock );
}
}
}
}

__unstableSplitSelection( blocks );
__unstableSplitSelection( newBlocks );
event.preventDefault();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!-- wp:list -->
<ul class="wp-block-list"><!-- wp:list-item -->
<li>x</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>y‸</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!-- wp:list -->
<ul class="wp-block-list"><!-- wp:list-item -->
<li>x</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>y‸</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->
26 changes: 26 additions & 0 deletions test/e2e/specs/editor/various/copy-cut-paste.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,32 @@ test.describe( 'Copy/cut/paste', () => {
expect( await editor.getEditedPostContent() ).toMatchSnapshot();
} );

test( 'should paste list in list', async ( {
page,
pageUtils,
editor,
} ) => {
pageUtils.setClipboardData( { html: '<ul><li>x</li><li>y</li></ul>' } );
await editor.insertBlock( { name: 'core/list' } );
await pageUtils.pressKeys( 'primary+v' );
// Ensure the selection is correct.
await page.keyboard.type( '‸' );
expect( await editor.getEditedPostContent() ).toMatchSnapshot();
} );

test( 'should paste paragraphs in list', async ( {
page,
pageUtils,
editor,
} ) => {
pageUtils.setClipboardData( { html: '<p>x</p><p>y</p>' } );
await editor.insertBlock( { name: 'core/list' } );
await pageUtils.pressKeys( 'primary+v' );
// Ensure the selection is correct.
await page.keyboard.type( '‸' );
expect( await editor.getEditedPostContent() ).toMatchSnapshot();
} );

test( 'should link selection', async ( { pageUtils, editor } ) => {
await editor.insertBlock( {
name: 'core/paragraph',
Expand Down

0 comments on commit f416fa2

Please sign in to comment.