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

List: fix pasting #62428

Merged
merged 3 commits into from
Jun 10, 2024
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 @@ -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
Loading