balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

templateInjection.js (820B)


      1 import { dispatch } from '@wordpress/data'
      2 import { get } from 'lodash'
      3 
      4 import { createBlocksFromInnerBlocksTemplate } from './blocks'
      5 
      6 export function injectTemplate(template) {
      7     if (!template) {
      8         throw Error('Template not found')
      9     }
     10 
     11     const { parse } = window.wp.blocks
     12     const createdBlocks = createBlocksFromInnerBlocksTemplate(parse(get(template, 'fields.code')))
     13     return injectTemplateBlocks(createdBlocks, template)
     14 }
     15 
     16 export function injectTemplateBlocks(blocks, templateRaw) {
     17     const { insertBlocks } = dispatch('core/block-editor')
     18     return insertBlocks(blocks).then(() => {
     19         window.dispatchEvent(new CustomEvent('extendify-sdk::template-inserted', {
     20             detail: {
     21                 template: templateRaw,
     22             },
     23             bubbles: true,
     24         }))
     25     })
     26 
     27 }