Skip to content
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 @@ -11,6 +11,7 @@ import {
import {
documentHasSelection,
documentHasUncollapsedSelection,
isEntirelySelected,
} from '@wordpress/dom';
import { useDispatch, useRegistry, useSelect } from '@wordpress/data';
import { useRefEffect } from '@wordpress/compose';
Expand All @@ -22,6 +23,31 @@ import { store as blockEditorStore } from '../../store';
import { useNotifyCopy } from '../../utils/use-notify-copy';
import { setClipboardBlocks, setContentEditableWrapper } from './utils';
import { getPasteEventData } from '../../utils/pasting';
import { getBlockClientId } from '../../utils/dom';

/**
* Whether the DOM selection entirely spans the content of the given block,
* and the block's editable element is the block element itself, like a
* heading or a paragraph, not a field within a larger block, like a caption.
*
* @param {Document} ownerDocument The block's document.
* @param {string} clientId The block's client ID.
*
* @return {boolean} Whether the block is entirely selected.
*/
function isBlockEntirelySelected( ownerDocument, clientId ) {
const selection = ownerDocument.defaultView.getSelection();

if ( getBlockClientId( selection.anchorNode ) !== clientId ) {
return false;
}

const blockElement = ownerDocument.getElementById( `block-${ clientId }` );

return (
!! blockElement?.isContentEditable && isEntirelySelected( blockElement )
);
}

export default function useClipboardHandler() {
const registry = useRegistry();
Expand Down Expand Up @@ -61,11 +87,25 @@ export default function useClipboardHandler() {
return;
}

// Whether the entire content of a block whose editable element
// is the block itself is selected: copying all of a heading's
// text should copy the heading block. Fields within a larger
// block, like a caption, are not the block element and keep
// the native copy.
// Whether the entire text of a single selected block is
// copied, in which case the block itself is copied.
const isWholeSingleBlockCopy =
event.type === 'copy' &&
! hasMultiSelection() &&
isBlockEntirelySelected(
event.target.ownerDocument,
selectedBlockClientIds[ 0 ]
);

// Let native copy/paste behaviour take over in input fields.
// But always handle multiple selected blocks.
if ( ! hasMultiSelection() ) {
const { target } = event;
const { ownerDocument } = target;
const { ownerDocument } = event.target;
// If copying, only consider actual text selection as selection.
// Otherwise, any focus on an input field is considered.
const hasSelection =
Expand All @@ -75,7 +115,7 @@ export default function useClipboardHandler() {
! ownerDocument.activeElement.isContentEditable;

// Let native copy behaviour take over in input fields.
if ( hasSelection ) {
if ( hasSelection && ! isWholeSingleBlockCopy ) {
return;
}
}
Expand All @@ -88,7 +128,9 @@ export default function useClipboardHandler() {

const isSelectionMergeable = __unstableIsSelectionMergeable();
const shouldHandleWholeBlocks =
__unstableIsSelectionCollapsed() || __unstableIsFullySelected();
__unstableIsSelectionCollapsed() ||
__unstableIsFullySelected() ||
isWholeSingleBlockCopy;
const expandSelectionIsNeeded =
! shouldHandleWholeBlocks && ! isSelectionMergeable;
if ( event.type === 'copy' || event.type === 'cut' ) {
Expand Down Expand Up @@ -199,6 +241,25 @@ export default function useClipboardHandler() {
return;
}

// Pasting over an entirely selected block replaces it, the
// equivalent of pasting into an empty block.
if (
! hasMultiSelection() &&
isBlockEntirelySelected(
event.target.ownerDocument,
selectedBlockClientIds[ 0 ]
)
) {
replaceBlocks(
selectedBlockClientIds,
blocks,
blocks.length - 1,
-1
);
event.preventDefault();
return;
}

// If a block doesn't support splitting, let rich text paste
// inline.
if (
Expand Down
4 changes: 4 additions & 0 deletions packages/rich-text/src/hook/event-listeners/copy-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export default ( props ) => ( element ) => {
const { record, handleChange } = props.current;
const { ownerDocument } = element;
if (
// Another handler may have already claimed the clipboard, e.g.
// the block editor copying the whole block when its entire
// text is selected.
event.defaultPrevented ||
isCollapsed( record.current ) ||
( ! element.contains( ownerDocument.activeElement ) &&
! ownsSelection( element ) )
Expand Down
67 changes: 67 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 @@ -864,4 +864,71 @@ test.describe( 'Copy/cut/paste', () => {
},
] );
} );

test( 'should copy the block when its entire text is selected', async ( {
editor,
page,
pageUtils,
} ) => {
await editor.insertBlock( {
name: 'core/heading',
attributes: { content: 'A heading', level: 3 },
} );
await editor.insertBlock( { name: 'core/paragraph' } );

await editor.canvas.getByText( 'A heading' ).click();
await pageUtils.pressKeys( 'primary+a' );
await pageUtils.pressKeys( 'primary+c' );

// Pasting into an empty paragraph inserts the heading, proving
// the clipboard holds the block, not bare text.
await editor.canvas
.getByRole( 'document', { name: 'Empty block' } )
.click();
await pageUtils.pressKeys( 'primary+v' );

await expect.poll( editor.getBlocks ).toMatchObject( [
{
name: 'core/heading',
attributes: { content: 'A heading', level: 3 },
},
{
name: 'core/heading',
attributes: { content: 'A heading', level: 3 },
},
] );

// Pasting into existing text inserts the text inline instead of
// splitting the paragraph.
await editor.insertBlock( {
name: 'core/paragraph',
attributes: { content: 'existing ' },
} );
await editor.canvas.getByText( 'existing' ).click();
await page.keyboard.press( 'ArrowDown' );
await pageUtils.pressKeys( 'primary+v' );

await expect.poll( editor.getBlocks ).toMatchObject( [
{ name: 'core/heading' },
{ name: 'core/heading' },
{
name: 'core/paragraph',
attributes: { content: 'existing A heading' },
},
] );

// Pasting over an entirely selected block replaces it, like
// pasting into an empty block.
await pageUtils.pressKeys( 'primary+a' );
await pageUtils.pressKeys( 'primary+v' );

await expect.poll( editor.getBlocks ).toMatchObject( [
{ name: 'core/heading' },
{ name: 'core/heading' },
{
name: 'core/heading',
attributes: { content: 'A heading', level: 3 },
},
] );
} );
} );
9 changes: 8 additions & 1 deletion test/e2e/specs/editor/various/rich-text.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,9 @@ test.describe( 'RichText (@firefox, @webkit)', () => {
// Paste paragraph contents.
await pageUtils.pressKeys( 'primary+v' );

// The soft line break becomes an item break: line breaks entering
// a list follow the paragraph-to-list conversion, like other
// editors pasting text with line breaks over list items.
expect( await editor.getBlocks() ).toMatchObject( [
{
name: 'core/paragraph',
Expand All @@ -704,7 +707,11 @@ test.describe( 'RichText (@firefox, @webkit)', () => {
innerBlocks: [
{
name: 'core/list-item',
attributes: { content: '1<br>2' },
attributes: { content: '1' },
},
{
name: 'core/list-item',
attributes: { content: '2' },
},
],
},
Expand Down
Loading