-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
fix: [#21810] Allow default pasting behaviour in FontSizePicker #21812
Merged
youknowriad
merged 1 commit into
WordPress:master
from
kirilzh:fix/native-paste-behavior
Apr 27, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
120 changes: 63 additions & 57 deletions
120
packages/block-editor/src/components/copy-handler/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,83 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useRef } from '@wordpress/element'; | ||
import { serialize, pasteHandler } from '@wordpress/blocks'; | ||
import { documentHasSelection } from '@wordpress/dom'; | ||
import { withDispatch } from '@wordpress/data'; | ||
import { compose } from '@wordpress/compose'; | ||
import { useDispatch, useSelect } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getPasteEventData } from '../../utils/get-paste-event-data'; | ||
|
||
function CopyHandler( { children, handler } ) { | ||
return ( | ||
<div onCopy={ handler } onCut={ handler } onPaste={ handler }> | ||
{ children } | ||
</div> | ||
); | ||
} | ||
function CopyHandler( { children } ) { | ||
const containerRef = useRef(); | ||
|
||
const { | ||
getBlocksByClientId, | ||
getSelectedBlockClientIds, | ||
hasMultiSelection, | ||
getSettings, | ||
} = useSelect( ( select ) => select( 'core/block-editor' ), [] ); | ||
|
||
const { removeBlocks, replaceBlocks } = useDispatch( 'core/block-editor' ); | ||
|
||
export default compose( [ | ||
withDispatch( ( dispatch, ownProps, { select } ) => { | ||
const { | ||
getBlocksByClientId, | ||
getSelectedBlockClientIds, | ||
hasMultiSelection, | ||
getSettings, | ||
} = select( 'core/block-editor' ); | ||
const { removeBlocks, replaceBlocks } = dispatch( 'core/block-editor' ); | ||
const { | ||
__experimentalCanUserUseUnfilteredHTML: canUserUseUnfilteredHTML, | ||
} = getSettings(); | ||
const { | ||
__experimentalCanUserUseUnfilteredHTML: canUserUseUnfilteredHTML, | ||
} = getSettings(); | ||
|
||
return { | ||
handler( event ) { | ||
const selectedBlockClientIds = getSelectedBlockClientIds(); | ||
const handler = ( event ) => { | ||
const selectedBlockClientIds = getSelectedBlockClientIds(); | ||
|
||
if ( selectedBlockClientIds.length === 0 ) { | ||
return; | ||
} | ||
if ( selectedBlockClientIds.length === 0 ) { | ||
return; | ||
} | ||
|
||
// Always handle multiple selected blocks. | ||
// Let native copy behaviour take over in input fields. | ||
if ( ! hasMultiSelection() && documentHasSelection() ) { | ||
return; | ||
} | ||
// Always handle multiple selected blocks. | ||
// Let native copy behaviour take over in input fields. | ||
if ( ! hasMultiSelection() && documentHasSelection() ) { | ||
return; | ||
} | ||
|
||
event.preventDefault(); | ||
if ( ! containerRef.current.contains( event.target ) ) { | ||
return; | ||
} | ||
event.preventDefault(); | ||
|
||
if ( event.type === 'copy' || event.type === 'cut' ) { | ||
const blocks = getBlocksByClientId( | ||
selectedBlockClientIds | ||
); | ||
const serialized = serialize( blocks ); | ||
if ( event.type === 'copy' || event.type === 'cut' ) { | ||
const blocks = getBlocksByClientId( selectedBlockClientIds ); | ||
const serialized = serialize( blocks ); | ||
|
||
event.clipboardData.setData( 'text/plain', serialized ); | ||
event.clipboardData.setData( 'text/html', serialized ); | ||
} | ||
event.clipboardData.setData( 'text/plain', serialized ); | ||
event.clipboardData.setData( 'text/html', serialized ); | ||
} | ||
|
||
if ( event.type === 'cut' ) { | ||
removeBlocks( selectedBlockClientIds ); | ||
} else if ( event.type === 'paste' ) { | ||
const { plainText, html } = getPasteEventData( event ); | ||
const blocks = pasteHandler( { | ||
HTML: html, | ||
plainText, | ||
mode: 'BLOCKS', | ||
canUserUseUnfilteredHTML, | ||
} ); | ||
if ( event.type === 'cut' ) { | ||
removeBlocks( selectedBlockClientIds ); | ||
} else if ( event.type === 'paste' ) { | ||
const { plainText, html } = getPasteEventData( event ); | ||
const blocks = pasteHandler( { | ||
HTML: html, | ||
plainText, | ||
mode: 'BLOCKS', | ||
canUserUseUnfilteredHTML, | ||
} ); | ||
|
||
replaceBlocks( selectedBlockClientIds, blocks ); | ||
} | ||
}; | ||
|
||
return ( | ||
<div | ||
ref={ containerRef } | ||
onCopy={ handler } | ||
onCut={ handler } | ||
onPaste={ handler } | ||
> | ||
{ children } | ||
</div> | ||
); | ||
} | ||
|
||
replaceBlocks( selectedBlockClientIds, blocks ); | ||
} | ||
}, | ||
}; | ||
} ), | ||
] )( CopyHandler ); | ||
export default CopyHandler; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's one small subtle difference that potentially can have performance impact. Since this is an inline function, potentially, this could rerender children often. I think it's good to start like you did (without any memoization) but we just need to check that the component is not being rerendered on each "store change".
For instance you can add a "console log" inside the component and type on a paragraph and see if the logging is repeated or not as you type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While typing console.log is not executed. When copying/pasting there are two statements in console.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fine :) Thanks for the tests