Skip to content

Commit

Permalink
fix (text): force the replacement of parsed blocks for pasting serial…
Browse files Browse the repository at this point in the history
…ized bl… (#3367)

* fix: force the replacement of parsed blocks for pasting serialized blocks

* Update src/block/text/util.js

---------

Co-authored-by: Benjamin Intal <[email protected]>
  • Loading branch information
Arukuen and bfintal authored Nov 14, 2024
1 parent 15d0788 commit 462c689
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
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 ] )
}

0 comments on commit 462c689

Please sign in to comment.