-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix (text): force the replacement of parsed blocks for pasting serial…
…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
Showing
2 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -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 ] ) | ||
} |