Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
4 changes: 4 additions & 0 deletions src/block/text/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Internal dependencies
*/
import blockStyles from './style'
import { useOnPaste } from './util'

/**
* External dependencies
Expand Down Expand Up @@ -118,6 +119,8 @@ const Edit = props => {
version: VERSION,
} )

const onPaste = useOnPaste( clientId )

return (
<>
<InspectorControls
Expand All @@ -141,6 +144,7 @@ const Edit = props => {
onMerge={ mergeBlocks }
onRemove={ onRemove }
onReplace={ onReplace }
onPaste={ onPaste }
/>
</BlockDiv>
{ props.isHovered && <MarginBottom /> }
Expand Down
30 changes: 30 additions & 0 deletions src/block/text/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* WordPress dependencies
*/
import { useDispatch } from '@wordpress/data'
import { pasteHandler } from '@wordpress/blocks'
import { store as blockEditorStore } from '@wordpress/block-editor'
import { useCallback } from '@wordpress/element'

export const useOnPaste = clientId => {
const { replaceBlocks } = useDispatch( blockEditorStore )

return useCallback( event => {
event.preventDefault()
// Get the raw HTML from the clipboard
const html = event.clipboardData.getData( 'text/html' )
const plain = event.clipboardData.getData( 'text/plain' )

const blocks = pasteHandler( {
HTML: html,
plainText: plain,
mode: 'BLOCKS',
} )

if ( blocks ) {
event.preventDefault()
// Replace the current custom block with the parsed block(s)
replaceBlocks( clientId, blocks )
}
}, [ clientId ] )
}