Skip to content

Commit f416fa2

Browse files
committed
List: fix pasting (#62428)
1 parent 3c97445 commit f416fa2

File tree

4 files changed

+73
-7
lines changed

4 files changed

+73
-7
lines changed

packages/block-editor/src/components/writing-flow/use-clipboard-handler.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
findTransform,
77
getBlockTransforms,
88
hasBlockSupport,
9+
switchToBlockType,
910
} from '@wordpress/blocks';
1011
import {
1112
documentHasSelection,
@@ -208,15 +209,36 @@ export default function useClipboardHandler() {
208209
firstSelectedClientId
209210
);
210211

211-
if (
212-
! blocks.every( ( block ) =>
213-
canInsertBlockType( block.name, rootClientId )
214-
)
215-
) {
216-
return;
212+
const newBlocks = [];
213+
214+
for ( const block of blocks ) {
215+
if ( canInsertBlockType( block.name, rootClientId ) ) {
216+
newBlocks.push( block );
217+
} else {
218+
// If a block cannot be inserted in a root block, try
219+
// converting it to that root block type and insert the
220+
// inner blocks.
221+
// Example: paragraphs cannot be inserted into a list,
222+
// so convert the paragraphs to a list for list items.
223+
const rootBlockName = getBlockName( rootClientId );
224+
const switchedBlocks =
225+
block.name !== rootBlockName
226+
? switchToBlockType( block, rootBlockName )
227+
: [ block ];
228+
229+
if ( ! switchedBlocks ) {
230+
return;
231+
}
232+
233+
for ( const switchedBlock of switchedBlocks ) {
234+
for ( const innerBlock of switchedBlock.innerBlocks ) {
235+
newBlocks.push( innerBlock );
236+
}
237+
}
238+
}
217239
}
218240

219-
__unstableSplitSelection( blocks );
241+
__unstableSplitSelection( newBlocks );
220242
event.preventDefault();
221243
}
222244
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!-- wp:list -->
2+
<ul class="wp-block-list"><!-- wp:list-item -->
3+
<li>x</li>
4+
<!-- /wp:list-item -->
5+
6+
<!-- wp:list-item -->
7+
<li>y‸</li>
8+
<!-- /wp:list-item --></ul>
9+
<!-- /wp:list -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!-- wp:list -->
2+
<ul class="wp-block-list"><!-- wp:list-item -->
3+
<li>x</li>
4+
<!-- /wp:list-item -->
5+
6+
<!-- wp:list-item -->
7+
<li>y‸</li>
8+
<!-- /wp:list-item --></ul>
9+
<!-- /wp:list -->

test/e2e/specs/editor/various/copy-cut-paste.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,32 @@ test.describe( 'Copy/cut/paste', () => {
494494
expect( await editor.getEditedPostContent() ).toMatchSnapshot();
495495
} );
496496

497+
test( 'should paste list in list', async ( {
498+
page,
499+
pageUtils,
500+
editor,
501+
} ) => {
502+
pageUtils.setClipboardData( { html: '<ul><li>x</li><li>y</li></ul>' } );
503+
await editor.insertBlock( { name: 'core/list' } );
504+
await pageUtils.pressKeys( 'primary+v' );
505+
// Ensure the selection is correct.
506+
await page.keyboard.type( '‸' );
507+
expect( await editor.getEditedPostContent() ).toMatchSnapshot();
508+
} );
509+
510+
test( 'should paste paragraphs in list', async ( {
511+
page,
512+
pageUtils,
513+
editor,
514+
} ) => {
515+
pageUtils.setClipboardData( { html: '<p>x</p><p>y</p>' } );
516+
await editor.insertBlock( { name: 'core/list' } );
517+
await pageUtils.pressKeys( 'primary+v' );
518+
// Ensure the selection is correct.
519+
await page.keyboard.type( '‸' );
520+
expect( await editor.getEditedPostContent() ).toMatchSnapshot();
521+
} );
522+
497523
test( 'should link selection', async ( { pageUtils, editor } ) => {
498524
await editor.insertBlock( {
499525
name: 'core/paragraph',

0 commit comments

Comments
 (0)