blocks.js (998B)
1 2 /** 3 * Given an array of InnerBlocks templates or Block Objects, 4 * returns an array of created Blocks from them. 5 * It handles the case of having InnerBlocks as Blocks by 6 * converting them to the proper format to continue recursively. 7 * 8 * @param {Array} innerBlocksOrTemplate Nested blocks or InnerBlocks templates. 9 * 10 * @return {Object[]} Array of Block objects. 11 */ 12 export function createBlocksFromInnerBlocksTemplate(innerBlocksOrTemplate = []) { 13 const { createBlock } = window.wp.blocks 14 15 // TODO: This should return the native implementation if available here 16 17 return innerBlocksOrTemplate.map((innerBlock) => { 18 const innerBlockTemplate = Array.isArray(innerBlock) 19 ? innerBlock 20 : [innerBlock.name, innerBlock.attributes, innerBlock.innerBlocks] 21 const [name, attributes, innerBlocks = []] = innerBlockTemplate 22 return createBlock( 23 name, attributes, createBlocksFromInnerBlocksTemplate(innerBlocks), 24 ) 25 }) 26 }