UNPKG

@wordpress/blocks

Version:
97 lines (95 loc) 3.5 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/blocks/src/api/templates.js var templates_exports = {}; __export(templates_exports, { doBlocksMatchTemplate: () => doBlocksMatchTemplate, synchronizeBlocksWithTemplate: () => synchronizeBlocksWithTemplate }); module.exports = __toCommonJS(templates_exports); var import_element = require("@wordpress/element"); var import_convert_legacy_block = require("./parser/convert-legacy-block"); var import_factory = require("./factory"); var import_registration = require("./registration"); function doBlocksMatchTemplate(blocks = [], template = []) { return blocks.length === template.length && template.every(([name, , innerBlocksTemplate], index) => { const block = blocks[index]; return name === block.name && doBlocksMatchTemplate(block.innerBlocks, innerBlocksTemplate); }); } var isHTMLAttribute = (attributeDefinition) => attributeDefinition?.source === "html"; var isQueryAttribute = (attributeDefinition) => attributeDefinition?.source === "query"; function normalizeAttributes(schema, values) { if (!values) { return {}; } return Object.fromEntries( Object.entries(values).map(([key, value]) => [ key, normalizeAttribute(schema[key], value) ]) ); } function normalizeAttribute(definition, value) { if (isHTMLAttribute(definition) && Array.isArray(value)) { return (0, import_element.renderToString)(value); } if (isQueryAttribute(definition) && value) { return value.map((subValues) => { return normalizeAttributes(definition.query, subValues); }); } return value; } function synchronizeBlocksWithTemplate(blocks = [], template) { if (!template) { return blocks; } return template.map( ([name, attributes, innerBlocksTemplate], index) => { const block = blocks[index]; if (block && block.name === name) { const innerBlocks = synchronizeBlocksWithTemplate( block.innerBlocks, innerBlocksTemplate ); return { ...block, innerBlocks }; } const blockType = (0, import_registration.getBlockType)(name); const normalizedAttributes = normalizeAttributes( blockType?.attributes ?? {}, attributes ); const [blockName, blockAttributes] = (0, import_convert_legacy_block.convertLegacyBlockNameAndAttributes)( name, normalizedAttributes ); return (0, import_factory.createBlock)( blockName, blockAttributes, synchronizeBlocksWithTemplate([], innerBlocksTemplate) ); } ); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { doBlocksMatchTemplate, synchronizeBlocksWithTemplate }); //# sourceMappingURL=templates.js.map