Skip to content

Commit

Permalink
Rich text: fix link paste for internal paste (#59063)
Browse files Browse the repository at this point in the history
Co-authored-by: ellatrix <ellatrix@git.wordpress.org>
Co-authored-by: mcsf <mcsf@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: annezazu <annezazu@git.wordpress.org>
  • Loading branch information
5 people authored Feb 15, 2024
1 parent 68afcb0 commit f8ea61f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 25 deletions.
51 changes: 26 additions & 25 deletions packages/block-editor/src/components/rich-text/use-paste-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,35 @@ export function usePasteHandler( props ) {
const isInternal =
event.clipboardData.getData( 'rich-text' ) === 'true';

function pasteInline( content ) {
const transformed = formatTypes.reduce(
( accumulator, { __unstablePasteRule } ) => {
// Only allow one transform.
if ( __unstablePasteRule && accumulator === value ) {
accumulator = __unstablePasteRule( value, {
html,
plainText,
} );
}

return accumulator;
},
value
);
if ( transformed !== value ) {
onChange( transformed );
} else {
const valueToInsert = create( { html: content } );
addActiveFormats( valueToInsert, value.activeFormats );
onChange( insert( value, valueToInsert ) );
}
}

// If the data comes from a rich text instance, we can directly use it
// without filtering the data. The filters are only meant for externally
// pasted content and remove inline styles.
if ( isInternal ) {
const pastedValue = create( { html } );
addActiveFormats( pastedValue, value.activeFormats );
onChange( insert( value, pastedValue ) );
pasteInline( html );
return;
}

Expand Down Expand Up @@ -135,28 +157,7 @@ export function usePasteHandler( props ) {
} );

if ( typeof content === 'string' ) {
const transformed = formatTypes.reduce(
( accumlator, { __unstablePasteRule } ) => {
// Only allow one transform.
if ( __unstablePasteRule && accumlator === value ) {
accumlator = __unstablePasteRule( value, {
html,
plainText,
} );
}

return accumlator;
},
value
);

if ( transformed !== value ) {
onChange( transformed );
} else {
const valueToInsert = create( { html: content } );
addActiveFormats( valueToInsert, value.activeFormats );
onChange( insert( value, valueToInsert ) );
}
pasteInline( content );
} else if ( content.length > 0 ) {
if ( onReplace && isEmpty( value ) ) {
onReplace( content, content.length - 1, -1 );
Expand Down
24 changes: 24 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 @@ -518,6 +518,30 @@ test.describe( 'Copy/cut/paste', () => {
] );
} );

test( 'should link selection on internal paste', async ( {
pageUtils,
editor,
page,
} ) => {
await editor.insertBlock( {
name: 'core/paragraph',
attributes: { content: 'https://w.org' },
} );
await pageUtils.pressKeys( 'primary+a' );
await pageUtils.pressKeys( 'primary+x' );
await page.keyboard.type( 'a' );
await pageUtils.pressKeys( 'shift+ArrowLeft' );
await pageUtils.pressKeys( 'primary+v' );
expect( await editor.getBlocks() ).toMatchObject( [
{
name: 'core/paragraph',
attributes: {
content: '<a href="https://w.org">a</a>',
},
},
] );
} );

test( 'should auto-link', async ( { pageUtils, editor } ) => {
await editor.insertBlock( {
name: 'core/paragraph',
Expand Down

0 comments on commit f8ea61f

Please sign in to comment.