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

Reduce keyboard cut handler to equal a copy handler to avoid bugs #3241

Merged
merged 2 commits into from
Sep 13, 2019
Merged
Changes from 1 commit
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 @@ -99,15 +99,18 @@ export default withDispatch( ( dispatch, ownProps, { select } ) => {
getSelectedBlockClientIds,
hasMultiSelection,
} = select( 'core/block-editor' );
const { removeBlock } = dispatch( 'core/block-editor' );
const { clearCopiedMarkup, setCopiedMarkup } = dispatch( 'amp/story' );

/**
* Copy handler for ensuring that the store's copiedMarkup is in sync with what's actually in clipBoard.
* Creates cut/copy handler for ensuring that the store's copiedMarkup is in sync with what's actually in clipBoard.
* If it's not a block that's being copied, let's clear the copiedMarkup.
* Otherwise, let's set the copied markup.
* If it's a cut handler, finally remove the currently selected block.
*
* @param {boolean} isCut Set to true if this is a cut handler, false if copy handler
* @return {Function} Returns an event handler for the desired action
*/
const onCopy = () => {
const createCutCopyHandler = ( isCut ) => () => {
const selectedBlockClientIds = getSelectedBlockClientIds();

if ( selectedBlockClientIds.length === 0 ) {
Expand All @@ -122,30 +125,20 @@ export default withDispatch( ( dispatch, ownProps, { select } ) => {
}
const serialized = serialize( getBlocksByClientId( selectedBlockClientIds ) );
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.
// But wait 1 render cycle to do it to allow the browser to correctly pick up the cut content.
setTimeout( () => {
for ( const clientId of selectedBlockClientIds ) {
if ( isCut ) {
// TODO: Remove selected Blocks.
// The code below works most of the time, but sometimes (unable to tell when and why) another element on page is selected when the block is removed, and then the browser copies this other element in stead of the previously selected element (that has now been removed).
/* for ( const clientId of selectedBlockClientIds ) {
removeBlock( clientId );
}
} );
} */
// wrapping the call in setTimeout fixes the case where another element is selected on cut, but throws an error in the cases, where the above code works fine.
}
};

const onCopy = createCutCopyHandler( false );
const onCut = createCutCopyHandler( true );

return {
onCopy,
onCut,
Expand Down