Skip to content

Commit 462c689

Browse files
Arukuenbfintal
andauthored
fix (text): force the replacement of parsed blocks for pasting serialized 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]>
1 parent 15d0788 commit 462c689

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/block/text/edit.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Internal dependencies
33
*/
44
import blockStyles from './style'
5+
import { useOnPaste } from './util'
56

67
/**
78
* External dependencies
@@ -118,6 +119,8 @@ const Edit = props => {
118119
version: VERSION,
119120
} )
120121

122+
const onPaste = useOnPaste( clientId )
123+
121124
return (
122125
<>
123126
<InspectorControls
@@ -141,6 +144,7 @@ const Edit = props => {
141144
onMerge={ mergeBlocks }
142145
onRemove={ onRemove }
143146
onReplace={ onReplace }
147+
onPaste={ onPaste }
144148
/>
145149
</BlockDiv>
146150
{ props.isHovered && <MarginBottom /> }

src/block/text/util.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* WordPress dependencies
3+
*/
4+
import { useDispatch } from '@wordpress/data'
5+
import { pasteHandler } from '@wordpress/blocks'
6+
import { store as blockEditorStore } from '@wordpress/block-editor'
7+
import { useCallback } from '@wordpress/element'
8+
9+
export const useOnPaste = clientId => {
10+
const { replaceBlocks } = useDispatch( blockEditorStore )
11+
12+
return useCallback( event => {
13+
event.preventDefault()
14+
// Get the raw HTML from the clipboard
15+
const html = event.clipboardData?.getData( 'text/html' ) || ''
16+
const plain = event.clipboardData?.getData( 'text/plain' ) || ''
17+
18+
const blocks = pasteHandler( {
19+
HTML: html,
20+
plainText: plain,
21+
mode: 'BLOCKS',
22+
} )
23+
24+
if ( blocks ) {
25+
event.preventDefault()
26+
// Replace the current custom block with the parsed block(s)
27+
replaceBlocks( clientId, blocks )
28+
}
29+
}, [ clientId ] )
30+
}

0 commit comments

Comments
 (0)