From 92381acd45a4c9d43f3a0dacb8fc7b5d020ea7c1 Mon Sep 17 00:00:00 2001 From: Ella Date: Sat, 8 Jun 2024 15:17:23 +0300 Subject: [PATCH 1/3] List: fix pasting --- .../writing-flow/use-clipboard-handler.js | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/packages/block-editor/src/components/writing-flow/use-clipboard-handler.js b/packages/block-editor/src/components/writing-flow/use-clipboard-handler.js index 56583447d3f0e..3ea2f3cdc4326 100644 --- a/packages/block-editor/src/components/writing-flow/use-clipboard-handler.js +++ b/packages/block-editor/src/components/writing-flow/use-clipboard-handler.js @@ -6,6 +6,7 @@ import { findTransform, getBlockTransforms, hasBlockSupport, + switchToBlockType, } from '@wordpress/blocks'; import { documentHasSelection, @@ -208,15 +209,31 @@ 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 { + 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(); } } From 3bfbacdfa30702c0605ab645f49e9b91f3931497 Mon Sep 17 00:00:00 2001 From: Ella Date: Mon, 10 Jun 2024 07:53:09 +0300 Subject: [PATCH 2/3] Add e2e tests --- ...e-should-paste-list-in-list-1-chromium.txt | 9 +++++++ ...ld-paste-paragraphs-in-list-1-chromium.txt | 9 +++++++ .../editor/various/copy-cut-paste.spec.js | 26 +++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 test/e2e/specs/editor/various/__snapshots__/Copy-cut-paste-should-paste-list-in-list-1-chromium.txt create mode 100644 test/e2e/specs/editor/various/__snapshots__/Copy-cut-paste-should-paste-paragraphs-in-list-1-chromium.txt diff --git a/test/e2e/specs/editor/various/__snapshots__/Copy-cut-paste-should-paste-list-in-list-1-chromium.txt b/test/e2e/specs/editor/various/__snapshots__/Copy-cut-paste-should-paste-list-in-list-1-chromium.txt new file mode 100644 index 0000000000000..7f74d7fd8b3d0 --- /dev/null +++ b/test/e2e/specs/editor/various/__snapshots__/Copy-cut-paste-should-paste-list-in-list-1-chromium.txt @@ -0,0 +1,9 @@ + + + \ No newline at end of file diff --git a/test/e2e/specs/editor/various/__snapshots__/Copy-cut-paste-should-paste-paragraphs-in-list-1-chromium.txt b/test/e2e/specs/editor/various/__snapshots__/Copy-cut-paste-should-paste-paragraphs-in-list-1-chromium.txt new file mode 100644 index 0000000000000..7f74d7fd8b3d0 --- /dev/null +++ b/test/e2e/specs/editor/various/__snapshots__/Copy-cut-paste-should-paste-paragraphs-in-list-1-chromium.txt @@ -0,0 +1,9 @@ + + + \ No newline at end of file diff --git a/test/e2e/specs/editor/various/copy-cut-paste.spec.js b/test/e2e/specs/editor/various/copy-cut-paste.spec.js index 48392170e66dd..4951f99fb6b5b 100644 --- a/test/e2e/specs/editor/various/copy-cut-paste.spec.js +++ b/test/e2e/specs/editor/various/copy-cut-paste.spec.js @@ -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: '' } ); + 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: '

x

y

' } ); + 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', From 594b5b1184fcf7af2b9074be355dada88359e84e Mon Sep 17 00:00:00 2001 From: Ella Date: Mon, 10 Jun 2024 08:01:32 +0300 Subject: [PATCH 3/3] Add an inline comment --- .../src/components/writing-flow/use-clipboard-handler.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/block-editor/src/components/writing-flow/use-clipboard-handler.js b/packages/block-editor/src/components/writing-flow/use-clipboard-handler.js index 3ea2f3cdc4326..bed07b183cc0c 100644 --- a/packages/block-editor/src/components/writing-flow/use-clipboard-handler.js +++ b/packages/block-editor/src/components/writing-flow/use-clipboard-handler.js @@ -215,6 +215,11 @@ export default function useClipboardHandler() { 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