Skip to content

Commit 2958043

Browse files
ellatrixmcsfyouknowriadannezazu
committed
Rich text: fix link paste for internal paste (#59063)
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>
1 parent 81115e7 commit 2958043

File tree

2 files changed

+50
-25
lines changed

2 files changed

+50
-25
lines changed

packages/block-editor/src/components/rich-text/use-paste-handler.js

+26-25
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,35 @@ export function usePasteHandler( props ) {
5858
const isInternal =
5959
event.clipboardData.getData( 'rich-text' ) === 'true';
6060

61+
function pasteInline( content ) {
62+
const transformed = formatTypes.reduce(
63+
( accumulator, { __unstablePasteRule } ) => {
64+
// Only allow one transform.
65+
if ( __unstablePasteRule && accumulator === value ) {
66+
accumulator = __unstablePasteRule( value, {
67+
html,
68+
plainText,
69+
} );
70+
}
71+
72+
return accumulator;
73+
},
74+
value
75+
);
76+
if ( transformed !== value ) {
77+
onChange( transformed );
78+
} else {
79+
const valueToInsert = create( { html: content } );
80+
addActiveFormats( valueToInsert, value.activeFormats );
81+
onChange( insert( value, valueToInsert ) );
82+
}
83+
}
84+
6185
// If the data comes from a rich text instance, we can directly use it
6286
// without filtering the data. The filters are only meant for externally
6387
// pasted content and remove inline styles.
6488
if ( isInternal ) {
65-
const pastedValue = create( { html } );
66-
addActiveFormats( pastedValue, value.activeFormats );
67-
onChange( insert( value, pastedValue ) );
89+
pasteInline( html );
6890
return;
6991
}
7092

@@ -135,28 +157,7 @@ export function usePasteHandler( props ) {
135157
} );
136158

137159
if ( typeof content === 'string' ) {
138-
const transformed = formatTypes.reduce(
139-
( accumlator, { __unstablePasteRule } ) => {
140-
// Only allow one transform.
141-
if ( __unstablePasteRule && accumlator === value ) {
142-
accumlator = __unstablePasteRule( value, {
143-
html,
144-
plainText,
145-
} );
146-
}
147-
148-
return accumlator;
149-
},
150-
value
151-
);
152-
153-
if ( transformed !== value ) {
154-
onChange( transformed );
155-
} else {
156-
const valueToInsert = create( { html: content } );
157-
addActiveFormats( valueToInsert, value.activeFormats );
158-
onChange( insert( value, valueToInsert ) );
159-
}
160+
pasteInline( content );
160161
} else if ( content.length > 0 ) {
161162
if ( onReplace && isEmpty( value ) ) {
162163
onReplace( content, content.length - 1, -1 );

test/e2e/specs/editor/various/copy-cut-paste.spec.js

+24
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,30 @@ test.describe( 'Copy/cut/paste', () => {
518518
] );
519519
} );
520520

521+
test( 'should link selection on internal paste', async ( {
522+
pageUtils,
523+
editor,
524+
page,
525+
} ) => {
526+
await editor.insertBlock( {
527+
name: 'core/paragraph',
528+
attributes: { content: 'https://w.org' },
529+
} );
530+
await pageUtils.pressKeys( 'primary+a' );
531+
await pageUtils.pressKeys( 'primary+x' );
532+
await page.keyboard.type( 'a' );
533+
await pageUtils.pressKeys( 'shift+ArrowLeft' );
534+
await pageUtils.pressKeys( 'primary+v' );
535+
expect( await editor.getBlocks() ).toMatchObject( [
536+
{
537+
name: 'core/paragraph',
538+
attributes: {
539+
content: '<a href="https://w.org">a</a>',
540+
},
541+
},
542+
] );
543+
} );
544+
521545
test( 'should auto-link', async ( { pageUtils, editor } ) => {
522546
await editor.insertBlock( {
523547
name: 'core/paragraph',

0 commit comments

Comments
 (0)