| 1 |
import { dispatch, select } from '@wordpress/data'; |
| 2 |
|
| 3 |
export const insertBlocks = async (blocks) => { |
| 4 |
const { insertBlocks, replaceBlock } = dispatch('core/block-editor'); |
| 5 |
const { |
| 6 |
getSelectedBlock, |
| 7 |
getBlockHierarchyRootClientId, |
| 8 |
getBlockIndex, |
| 9 |
getGlobalBlockCount, |
| 10 |
} = select('core/block-editor'); |
| 11 |
|
| 12 |
const { clientId, name, attributes } = getSelectedBlock() || {}; |
| 13 |
const rootClientId = clientId ? getBlockHierarchyRootClientId(clientId) : ''; |
| 14 |
const insertPointIndex = |
| 15 |
(rootClientId ? getBlockIndex(rootClientId) : getGlobalBlockCount()) + 1; |
| 16 |
|
| 17 |
if (name === 'core/paragraph' && attributes?.content === '') { |
| 18 |
return await replaceBlock(clientId, blocks); |
| 19 |
} |
| 20 |
return await insertBlocks(blocks, insertPointIndex); |
| 21 |
}; |
| 22 |
|