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

Rich text: fix link paste for internal paste #59063

Merged
merged 2 commits into from
Feb 15, 2024
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
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
Loading