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

Writing flow: fix paste for input fields #61389

Merged
merged 5 commits into from
May 24, 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 @@ -60,6 +60,25 @@ export default function useClipboardHandler() {
return;
}

// Let native copy/paste behaviour take over in input fields.
// But always handle multiple selected blocks.
if ( ! hasMultiSelection() ) {
const { target } = event;
const { ownerDocument } = target;
// If copying, only consider actual text selection as selection.
// Otherwise, any focus on an input field is considered.
const hasSelection =
event.type === 'copy' || event.type === 'cut'
? documentHasUncollapsedSelection( ownerDocument )
: documentHasSelection( ownerDocument ) &&
! ownerDocument.activeElement.isContentEditable;

// Let native copy behaviour take over in input fields.
if ( hasSelection ) {
return;
}
}

const { activeElement } = event.target.ownerDocument;

if ( ! node.contains( activeElement ) ) {
Expand All @@ -72,22 +91,6 @@ export default function useClipboardHandler() {
const expandSelectionIsNeeded =
! shouldHandleWholeBlocks && ! isSelectionMergeable;
if ( event.type === 'copy' || event.type === 'cut' ) {
if ( ! hasMultiSelection() ) {
const { target } = event;
const { ownerDocument } = target;
// If copying, only consider actual text selection as selection.
// Otherwise, any focus on an input field is considered.
const hasSelection =
event.type === 'copy' || event.type === 'cut'
? documentHasUncollapsedSelection( ownerDocument )
: documentHasSelection( ownerDocument );
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this line to documentHasSelection( ownerDocument ) && ! ownerDocument.activeElement.isContentEditable while also running it for the paste event as we did before #54543.


// Let native copy behaviour take over in input fields.
if ( hasSelection ) {
return;
}
}

event.preventDefault();

if ( selectedBlockClientIds.length === 1 ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function setClipboardData(
}

async function emulateClipboard( page: Page, type: 'copy' | 'cut' | 'paste' ) {
clipboardDataHolder = await page.evaluate(
const output = await page.evaluate(
( [ _type, _clipboardData ] ) => {
const canvasDoc =
// @ts-ignore
Expand Down Expand Up @@ -99,6 +99,10 @@ async function emulateClipboard( page: Page, type: 'copy' | 'cut' | 'paste' ) {

canvasDoc.activeElement.dispatchEvent( event );

if ( _type === 'paste' ) {
return event.defaultPrevented;
}

return {
'text/plain': event.clipboardData.getData( 'text/plain' ),
'text/html': event.clipboardData.getData( 'text/html' ),
Expand All @@ -107,6 +111,17 @@ async function emulateClipboard( page: Page, type: 'copy' | 'cut' | 'paste' ) {
},
[ type, clipboardDataHolder ] as const
);

if ( typeof output === 'object' ) {
clipboardDataHolder = output;
}

if ( output === false ) {
// Emulate paste by typing the clipboard content, which works across all
// elements and documents (keyboard.type does uses the nested active
// element automatically).
await page.keyboard.type( clipboardDataHolder[ 'text/plain' ] );
}
}

const isAppleOS = () => process.platform === 'darwin';
Expand Down
31 changes: 31 additions & 0 deletions test/e2e/specs/editor/blocks/rss.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* WordPress dependencies
*/
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );

test.describe( 'RSS', () => {
test.beforeEach( async ( { admin } ) => {
await admin.createNewPost();
} );

// See: https://github.com/WordPress/gutenberg/pull/61389.
test( 'should retain native copy/paste behavior for input fields', async ( {
editor,
pageUtils,
} ) => {
await editor.insertBlock( { name: 'core/rss' } );
pageUtils.setClipboardData( {
plainText: 'https://developer.wordpress.org/news/feed/',
} );
await pageUtils.pressKeys( 'primary+v' );

await expect.poll( editor.getBlocks ).toMatchObject( [
{
name: 'core/rss',
attributes: {
feedURL: 'https://developer.wordpress.org/news/feed/',
},
},
] );
} );
} );
Loading