File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed
Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 22 * Internal dependencies
33 */
44import 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 /> }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments