Skip to content

Commit

Permalink
Handle cut keyboard requests. (#3231)
Browse files Browse the repository at this point in the history
  • Loading branch information
spacedmonkey authored and swissspidy committed Sep 12, 2019
1 parent 33f3d7c commit 7ba9f26
Showing 1 changed file with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { withDispatch, useSelect, useDispatch } from '@wordpress/data';
*/
import { ensureAllowedBlocksOnPaste } from '../../helpers';

function CopyPasteHandler( { children, onCopy, clientId, isSelected } ) {
function CopyPasteHandler( { children, onCopy, onCut, clientId, isSelected } ) {
const {
isFirstPage,
canUserUseUnfilteredHTML,
Expand Down Expand Up @@ -79,7 +79,7 @@ function CopyPasteHandler( { children, onCopy, clientId, isSelected } ) {
};

return (
<div onCopy={ onCopy } onPaste={ onPaste }>
<div onCopy={ onCopy } onPaste={ onPaste } onCut={ onCut }>
{ children }
</div>
);
Expand All @@ -90,6 +90,7 @@ CopyPasteHandler.propTypes = {
clientId: PropTypes.string.isRequired,
isSelected: PropTypes.bool.isRequired,
onCopy: PropTypes.func.isRequired,
onCut: PropTypes.func.isRequired,
};

export default withDispatch( ( dispatch, ownProps, { select } ) => {
Expand All @@ -98,6 +99,7 @@ export default withDispatch( ( dispatch, ownProps, { select } ) => {
getSelectedBlockClientIds,
hasMultiSelection,
} = select( 'core/block-editor' );
const { removeBlock } = dispatch( 'core/block-editor' );
const { clearCopiedMarkup, setCopiedMarkup } = dispatch( 'amp/story' );

/**
Expand All @@ -122,7 +124,27 @@ export default withDispatch( ( dispatch, ownProps, { select } ) => {
setCopiedMarkup( serialized );
};

/**
* Cut handler for ensuring that the store's cutMarkup is in sync with what's actually in clipBoard.
* If it's not a block that's being cut, let's clear the cutMarkup.
* Otherwise, let's set the cut markup.
*/
const onCut = () => {
const selectedBlockClientIds = getSelectedBlockClientIds();

if ( selectedBlockClientIds.length === 0 ) {
return;
}
// Reuse code in onCode.
onCopy();
// Remove selected Blocks.
for ( const clientId of selectedBlockClientIds ) {
removeBlock( clientId );
}
};

return {
onCopy,
onCut,
};
} )( CopyPasteHandler );

0 comments on commit 7ba9f26

Please sign in to comment.