# extendify/0.5.0/src/util/templateInjection.js

Extendify, version 0.5.0. 33 lines.

- Page: https://pluginprobe.com/plugins/extendify/0.5.0/code/src/util/templateInjection.js
- Raw: https://pluginprobe.com/plugins/extendify/0.5.0/raw/src/util/templateInjection.js
- Modified: 2022-01-06T18:08:32+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/extendify/0.5.0/code/src/util/templateInjection.js#L10-L20`.

```javascript
import { dispatch, select } from '@wordpress/data'

export function injectTemplateBlocks(blocks, templateRaw) {
    const { insertBlocks, replaceBlock } = dispatch('core/block-editor')
    const {
        getSelectedBlock,
        getBlockHierarchyRootClientId,
        getBlockIndex,
        getGlobalBlockCount,
    } = select('core/block-editor')

    const { clientId, name, attributes } = getSelectedBlock() || {}
    const rootClientId = clientId ? getBlockHierarchyRootClientId(clientId) : ''
    const insertPointIndex =
        (rootClientId ? getBlockIndex(rootClientId) : getGlobalBlockCount()) + 1

    const injectblock = () =>
        name === 'core/paragraph' && attributes?.content === ''
            ? replaceBlock(clientId, blocks)
            : insertBlocks(blocks, insertPointIndex)

    return injectblock().then(() =>
        window.dispatchEvent(
            new CustomEvent('extendify::template-inserted', {
                detail: {
                    template: templateRaw,
                },
                bubbles: true,
            }),
        ),
    )
}

```
