diff --git a/packages/block-editor/src/components/rich-text/use-paste-handler.js b/packages/block-editor/src/components/rich-text/use-paste-handler.js index 1302e2d0dce46..fc75caa43615f 100644 --- a/packages/block-editor/src/components/rich-text/use-paste-handler.js +++ b/packages/block-editor/src/components/rich-text/use-paste-handler.js @@ -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; } @@ -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 ); 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 04113e013930b..ec0cbab9ca809 100644 --- a/test/e2e/specs/editor/various/copy-cut-paste.spec.js +++ b/test/e2e/specs/editor/various/copy-cut-paste.spec.js @@ -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', + }, + }, + ] ); + } ); + test( 'should auto-link', async ( { pageUtils, editor } ) => { await editor.insertBlock( { name: 'core/paragraph',