Skip to content

Commit

Permalink
List: allow pasting pre/code (#45016)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored Nov 30, 2022
1 parent 796b800 commit 74a71e2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
4 changes: 3 additions & 1 deletion packages/block-library/src/code/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ const transforms = {
type: 'block',
blocks: [ 'core/paragraph' ],
transform: ( { content } ) => {
return createBlock( 'core/paragraph', { content } );
return createBlock( 'core/paragraph', {
content: content.replace( /\n/g, '<br>' ),
} );
},
},
],
Expand Down
11 changes: 10 additions & 1 deletion packages/block-library/src/list-item/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { createBlock, switchToBlockType } from '@wordpress/blocks';
*/
import { name as listItemName } from './block.json';
import { name as listName } from '../list/block.json';
import { name as paragraphName } from '../paragraph/block.json';

export function createListItem( listItemAttributes, listAttributes, children ) {
return createBlock(
Expand All @@ -19,6 +20,14 @@ export function createListItem( listItemAttributes, listAttributes, children ) {
);
}

function convertBlockToList( block ) {
const list = switchToBlockType( block, listName );
if ( list ) return list;
const paragraph = switchToBlockType( block, paragraphName );
if ( paragraph ) return switchToBlockType( paragraph, listName );
return null;
}

export function convertToListItems( blocks ) {
const listItems = [];

Expand All @@ -27,7 +36,7 @@ export function convertToListItems( blocks ) {
listItems.push( block );
} else if ( block.name === listName ) {
listItems.push( ...block.innerBlocks );
} else if ( ( block = switchToBlockType( block, listName ) ) ) {
} else if ( ( block = convertBlockToList( block ) ) ) {
for ( const { innerBlocks } of block ) {
listItems.push( ...innerBlocks );
}
Expand Down
5 changes: 4 additions & 1 deletion packages/block-library/src/preformatted/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ const transforms = {
type: 'block',
blocks: [ 'core/paragraph' ],
transform: ( attributes ) =>
createBlock( 'core/paragraph', attributes ),
createBlock( 'core/paragraph', {
...attributes,
content: attributes.content.replace( /\n/g, '<br>' ),
} ),
},
{
type: 'block',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>xy</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->
15 changes: 15 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 @@ -433,4 +433,19 @@ test.describe( 'Copy/cut/paste', () => {
await page.evaluate( () => document.activeElement.innerHTML )
).toBe( 'axyb' );
} );

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

0 comments on commit 74a71e2

Please sign in to comment.