| 1 |
import { dispatch, select } from '@wordpress/data' |
| 2 |
|
| 3 |
export function injectTemplateBlocks(blocks, templateRaw) { |
| 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 |
const injectblock = () => |
| 18 |
name === 'core/paragraph' && attributes?.content === '' |
| 19 |
? replaceBlock(clientId, blocks) |
| 20 |
: insertBlocks(blocks, insertPointIndex) |
| 21 |
|
| 22 |
return injectblock().then(() => |
| 23 |
window.dispatchEvent( |
| 24 |
new CustomEvent('extendify::template-inserted', { |
| 25 |
detail: { |
| 26 |
template: templateRaw, |
| 27 |
}, |
| 28 |
bubbles: true, |
| 29 |
}), |
| 30 |
), |
| 31 |
) |
| 32 |
} |
| 33 |
|