diff --git a/packages/block-editor/src/components/writing-flow/use-clipboard-handler.js b/packages/block-editor/src/components/writing-flow/use-clipboard-handler.js index c7f15a538c595d..6bfd05c3d7d5f0 100644 --- a/packages/block-editor/src/components/writing-flow/use-clipboard-handler.js +++ b/packages/block-editor/src/components/writing-flow/use-clipboard-handler.js @@ -11,6 +11,7 @@ import { import { documentHasSelection, documentHasUncollapsedSelection, + isEntirelySelected, } from '@wordpress/dom'; import { useDispatch, useRegistry, useSelect } from '@wordpress/data'; import { useRefEffect } from '@wordpress/compose'; @@ -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(); @@ -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 = @@ -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; } } @@ -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' ) { @@ -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 ( diff --git a/packages/rich-text/src/hook/event-listeners/copy-handler.js b/packages/rich-text/src/hook/event-listeners/copy-handler.js index 80d9f536f39269..9ad203aeb1bab9 100644 --- a/packages/rich-text/src/hook/event-listeners/copy-handler.js +++ b/packages/rich-text/src/hook/event-listeners/copy-handler.js @@ -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 ) ) 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 938fabf8ee1918..726b9212b3f5ee 100644 --- a/test/e2e/specs/editor/various/copy-cut-paste.spec.js +++ b/test/e2e/specs/editor/various/copy-cut-paste.spec.js @@ -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 }, + }, + ] ); + } ); } ); diff --git a/test/e2e/specs/editor/various/rich-text.spec.js b/test/e2e/specs/editor/various/rich-text.spec.js index 26443d3e2d9caf..5ba7de488bec9a 100644 --- a/test/e2e/specs/editor/various/rich-text.spec.js +++ b/test/e2e/specs/editor/various/rich-text.spec.js @@ -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', @@ -704,7 +707,11 @@ test.describe( 'RichText (@firefox, @webkit)', () => { innerBlocks: [ { name: 'core/list-item', - attributes: { content: '1
2' }, + attributes: { content: '1' }, + }, + { + name: 'core/list-item', + attributes: { content: '2' }, }, ], },