From 462c6894392b4e101ac6342df1a5b7faf8de684d Mon Sep 17 00:00:00 2001 From: Alquen Antonio Sarmiento Date: Thu, 14 Nov 2024 13:31:09 +0800 Subject: [PATCH] =?UTF-8?q?fix=20(text):=20force=20the=20replacement=20of?= =?UTF-8?q?=20parsed=20blocks=20for=20pasting=20serialized=20bl=E2=80=A6?= =?UTF-8?q?=20(#3367)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: force the replacement of parsed blocks for pasting serialized blocks * Update src/block/text/util.js --------- Co-authored-by: Benjamin Intal --- src/block/text/edit.js | 4 ++++ src/block/text/util.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/block/text/util.js diff --git a/src/block/text/edit.js b/src/block/text/edit.js index 43c8426358..84ad8c26c5 100644 --- a/src/block/text/edit.js +++ b/src/block/text/edit.js @@ -2,6 +2,7 @@ * Internal dependencies */ import blockStyles from './style' +import { useOnPaste } from './util' /** * External dependencies @@ -118,6 +119,8 @@ const Edit = props => { version: VERSION, } ) + const onPaste = useOnPaste( clientId ) + return ( <> { onMerge={ mergeBlocks } onRemove={ onRemove } onReplace={ onReplace } + onPaste={ onPaste } /> { props.isHovered && } diff --git a/src/block/text/util.js b/src/block/text/util.js new file mode 100644 index 0000000000..068b023956 --- /dev/null +++ b/src/block/text/util.js @@ -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 ] ) +}