UNPKG

@wordpress/blocks

Version:
195 lines (193 loc) 7.81 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/blocks/src/store/process-block-type.js var process_block_type_exports = {}; __export(process_block_type_exports, { processBlockType: () => processBlockType }); module.exports = __toCommonJS(process_block_type_exports); var import_is_plain_object = require("is-plain-object"); var import_react_is = require("react-is"); var import_deprecated = __toESM(require("@wordpress/deprecated")); var import_hooks = require("@wordpress/hooks"); var import_warning = __toESM(require("@wordpress/warning")); var import_utils = require("../api/utils"); var import_constants = require("../api/constants"); var LEGACY_CATEGORY_MAPPING = { common: "text", formatting: "text", layout: "design" }; function mergeBlockVariations(bootstrappedVariations = [], clientVariations = []) { const result = [...bootstrappedVariations]; clientVariations.forEach((clientVariation) => { const index = result.findIndex( (bootstrappedVariation) => bootstrappedVariation.name === clientVariation.name ); if (index !== -1) { result[index] = { ...result[index], ...clientVariation }; } else { result.push(clientVariation); } }); return result; } var processBlockType = (name, blockSettings) => ({ select }) => { const bootstrappedBlockType = select.getBootstrappedBlockType(name); const blockType = { apiVersion: 1, name, icon: import_constants.BLOCK_ICON_DEFAULT, keywords: [], attributes: {}, providesContext: {}, usesContext: [], selectors: {}, supports: {}, styles: [], blockHooks: {}, save: () => null, ...bootstrappedBlockType, ...blockSettings, // blockType.variations can be defined as a filePath. variations: mergeBlockVariations( Array.isArray(bootstrappedBlockType?.variations) ? bootstrappedBlockType.variations : [], Array.isArray(blockSettings?.variations) ? blockSettings.variations : [] ) }; if (!blockType.attributes || typeof blockType.attributes !== "object") { (0, import_warning.default)( 'The block "' + name + '" is registering attributes as `null` or `undefined`. Use an empty object (`attributes: {}`) or exclude the `attributes` key.' ); blockType.attributes = {}; } const settings = (0, import_hooks.applyFilters)( "blocks.registerBlockType", blockType, name, null ); if (settings.apiVersion <= 2) { (0, import_warning.default)( `The block "${name}" is registered with API version 2 or lower. This means that the post editor may work as a non-iframe editor. Since all editors are planned to work as iframes in the future, set the \`apiVersion\` field to 3 and test the block inside the iframe editor. See: https://developer.wordpress.org/block-editor/reference-guides/block-api/block-api-versions/#version-3-wordpress-6-3` ); } if (settings.description && typeof settings.description !== "string") { (0, import_deprecated.default)("Declaring non-string block descriptions", { since: "6.2" }); } if (settings.deprecated) { settings.deprecated = settings.deprecated.map( (deprecation) => Object.fromEntries( Object.entries( // Only keep valid deprecation keys. (0, import_hooks.applyFilters)( "blocks.registerBlockType", // Merge deprecation keys with pre-filter settings // so that filters that depend on specific keys being // present don't fail. { // Omit deprecation keys here so that deprecations // can opt out of specific keys like "supports". ...(0, import_utils.omit)(blockType, import_constants.DEPRECATED_ENTRY_KEYS), ...deprecation }, blockType.name, deprecation ) ).filter( ([key]) => import_constants.DEPRECATED_ENTRY_KEYS.includes(key) ) ) ); } if (!(0, import_is_plain_object.isPlainObject)(settings)) { (0, import_warning.default)("Block settings must be a valid object."); return; } if (typeof settings.save !== "function") { (0, import_warning.default)('The "save" property must be a valid function.'); return; } if ("edit" in settings && !(0, import_react_is.isValidElementType)(settings.edit)) { (0, import_warning.default)('The "edit" property must be a valid component.'); return; } if (LEGACY_CATEGORY_MAPPING.hasOwnProperty(settings.category)) { settings.category = LEGACY_CATEGORY_MAPPING[settings.category]; } if ("category" in settings && !select.getCategories().some(({ slug }) => slug === settings.category)) { (0, import_warning.default)( 'The block "' + name + '" is registered with an invalid category "' + settings.category + '".' ); delete settings.category; } if (!("title" in settings) || settings.title === "") { (0, import_warning.default)('The block "' + name + '" must have a title.'); return; } if (typeof settings.title !== "string") { (0, import_warning.default)("Block titles must be strings."); return; } settings.icon = (0, import_utils.normalizeIconObject)(settings.icon); if (!(0, import_utils.isValidIcon)(settings.icon.src)) { (0, import_warning.default)( "The icon passed is invalid. The icon should be a string, an element, a function, or an object following the specifications documented in https://developer.wordpress.org/block-editor/developers/block-api/block-registration/#icon-optional" ); return; } if (typeof settings?.parent === "string" || settings?.parent instanceof String) { settings.parent = [settings.parent]; (0, import_warning.default)( "Parent must be undefined or an array of strings (block types), but it is a string." ); } if (!Array.isArray(settings?.parent) && settings?.parent !== void 0) { (0, import_warning.default)( "Parent must be undefined or an array of block types, but it is ", settings.parent ); return; } if (1 === settings?.parent?.length && name === settings.parent[0]) { (0, import_warning.default)( 'Block "' + name + '" cannot be a parent of itself. Please remove the block name from the parent list.' ); return; } return settings; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { processBlockType }); //# sourceMappingURL=process-block-type.js.map