Skip to content

Commit

Permalink
Editor: Fix regression introduced in CopyHandler (#12543)
Browse files Browse the repository at this point in the history
* Edior: Fix regression introduced in CopyHandler

* Refactor CopyHandler to be less dependent on withData implementation
  • Loading branch information
gziolo authored and youknowriad committed Dec 5, 2018
1 parent 46f789a commit 20edbf2
Showing 1 changed file with 28 additions and 29 deletions.
57 changes: 28 additions & 29 deletions packages/editor/src/components/copy-handler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class CopyHandler extends Component {
constructor() {
super( ...arguments );

this.onCopy = this.onCopy.bind( this );
this.onCut = this.onCut.bind( this );
this.onCopy = ( event ) => this.props.onCopy( event );
this.onCut = ( event ) => this.props.onCut( event );
}

componentDidMount() {
Expand All @@ -25,16 +25,6 @@ class CopyHandler extends Component {
document.removeEventListener( 'cut', this.onCut );
}

onCopy( event ) {
this.props.onCopy( event.clipboardData );
event.preventDefault();
}

onCut( event ) {
this.props.onCut( event.clipboardData );
event.preventDefault();
}

render() {
return null;
}
Expand All @@ -50,29 +40,38 @@ export default compose( [
} = select( 'core/editor' );
const { removeBlocks } = dispatch( 'core/editor' );

const selectedBlockClientId = getSelectedBlockClientId();
const selectedBlockClientIds = selectedBlockClientId ? [ selectedBlockClientId ] : getMultiSelectedBlockClientIds();
const onCopy = ( event ) => {
const selectedBlockClientIds = getSelectedBlockClientId() ?
[ getSelectedBlockClientId() ] :
getMultiSelectedBlockClientIds();

return {
onCopy( dataTransfer ) {
if ( selectedBlockClientIds.length === 0 ) {
return;
}
if ( selectedBlockClientIds.length === 0 ) {
return;
}

// Let native copy behaviour take over in input fields.
if ( ! hasMultiSelection() && documentHasSelection() ) {
return;
}
// Let native copy behaviour take over in input fields.
if ( ! hasMultiSelection() && documentHasSelection() ) {
return;
}

const serialized = serialize( getBlocksByClientId( selectedBlockClientIds ) );
const serialized = serialize( getBlocksByClientId( selectedBlockClientIds ) );

dataTransfer.setData( 'text/plain', serialized );
dataTransfer.setData( 'text/html', serialized );
},
onCut( dataTransfer ) {
this.onCopy( dataTransfer );
event.clipboardData.setData( 'text/plain', serialized );
event.clipboardData.setData( 'text/html', serialized );

event.preventDefault();
};

return {
onCopy,
onCut( event ) {
onCopy( event );

if ( hasMultiSelection() ) {
const selectedBlockClientIds = getSelectedBlockClientId() ?
[ getSelectedBlockClientId() ] :
getMultiSelectedBlockClientIds();

removeBlocks( selectedBlockClientIds );
}
},
Expand Down

0 comments on commit 20edbf2

Please sign in to comment.