@@ -5,7 +5,7 @@ import { useEffect, useCallback, useRef } from '@wordpress/element';
55import { useDispatch , useSelect } from '@wordpress/data' ;
66import { store as coreStore } from '@wordpress/core-data' ;
77import { store as editorStore } from '@wordpress/editor' ;
8- import { parse , serialize , getBlockType } from '@wordpress/blocks' ;
8+ import { parse , serialize , getBlockType , createBlock } from '@wordpress/blocks' ;
99import { store as blockEditorStore } from '@wordpress/block-editor' ;
1010import { insert , create , toHTMLString } from '@wordpress/rich-text' ;
1111
@@ -21,12 +21,15 @@ export function useHostBridge( post, editorRef ) {
2121 const { undo, redo, switchEditorMode } = useDispatch ( editorStore ) ;
2222 const { getEditedPostAttribute, getEditedPostContent } =
2323 useSelect ( editorStore ) ;
24- const { updateBlock, selectionChange } = useDispatch ( blockEditorStore ) ;
24+ const { updateBlock, selectionChange, insertBlock, replaceBlock } =
25+ useDispatch ( blockEditorStore ) ;
2526 const {
2627 getSelectedBlockClientId,
2728 getBlock,
2829 getSelectionStart,
2930 getSelectionEnd,
31+ getBlockRootClientId,
32+ getBlockIndex,
3033 } = useSelect ( blockEditorStore ) ;
3134
3235 const editContent = useCallback (
@@ -118,6 +121,44 @@ export function useHostBridge( post, editorRef ) {
118121 }
119122 } ;
120123
124+ window . editor . getSelectedBlockClientId = ( ) => {
125+ return getSelectedBlockClientId ( ) ;
126+ } ;
127+
128+ window . editor . insertBlock = (
129+ blockName ,
130+ selectedBlockClientId = null
131+ ) => {
132+ const targetBlockClientId =
133+ selectedBlockClientId || getSelectedBlockClientId ( ) ;
134+ const block = createBlock ( blockName ) ;
135+
136+ if ( targetBlockClientId ) {
137+ const selectedBlock = getBlock ( targetBlockClientId ) ;
138+
139+ // If the selected block is an empty paragraph, replace it
140+ const isParagraph = selectedBlock ?. name === 'core/paragraph' ;
141+ const isEmpty =
142+ ! selectedBlock ?. attributes ?. content ||
143+ selectedBlock . attributes . content . trim ( ) === '' ;
144+
145+ if ( isParagraph && isEmpty ) {
146+ replaceBlock ( targetBlockClientId , block ) ;
147+ } else {
148+ // Otherwise, insert after the currently selected block
149+ const rootClientId =
150+ getBlockRootClientId ( targetBlockClientId ) ;
151+ const blockIndex = getBlockIndex ( targetBlockClientId ) ;
152+ insertBlock ( block , blockIndex + 1 , rootClientId , true ) ;
153+ }
154+ } else {
155+ // No selection, insert at the current position
156+ insertBlock ( block , undefined , undefined , true ) ;
157+ }
158+
159+ return true ;
160+ } ;
161+
121162 window . editor . appendTextAtCursor = ( text ) => {
122163 const selectedBlockClientId = getSelectedBlockClientId ( ) ;
123164
@@ -185,6 +226,8 @@ export function useHostBridge( post, editorRef ) {
185226 delete window . editor . redo ;
186227 delete window . editor . switchEditorMode ;
187228 delete window . editor . dismissTopModal ;
229+ delete window . editor . getSelectedBlockClientId ;
230+ delete window . editor . insertBlock ;
188231 delete window . editor . appendTextAtCursor ;
189232 } ;
190233 } , [
@@ -199,8 +242,12 @@ export function useHostBridge( post, editorRef ) {
199242 getBlock ,
200243 getSelectionStart ,
201244 getSelectionEnd ,
245+ getBlockRootClientId ,
246+ getBlockIndex ,
202247 updateBlock ,
203248 selectionChange ,
249+ insertBlock ,
250+ replaceBlock ,
204251 ] ) ;
205252}
206253
0 commit comments