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: allow pasting pre/code #45016

Merged
merged 2 commits into from
Nov 30, 2022
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
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();
} );
} );