UNPKG

@wordpress/blocks

Version:
397 lines (395 loc) 15.1 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/api/registration.js var registration_exports = {}; __export(registration_exports, { getBlockBindingsSource: () => getBlockBindingsSource, getBlockBindingsSources: () => getBlockBindingsSources, getBlockSupport: () => getBlockSupport, getBlockType: () => getBlockType, getBlockTypes: () => getBlockTypes, getBlockVariations: () => getBlockVariations, getChildBlockNames: () => getChildBlockNames, getDefaultBlockName: () => getDefaultBlockName, getFreeformContentHandlerName: () => getFreeformContentHandlerName, getGroupingBlockName: () => getGroupingBlockName, getUnregisteredTypeHandlerName: () => getUnregisteredTypeHandlerName, hasBlockSupport: () => hasBlockSupport, hasChildBlocks: () => hasChildBlocks, hasChildBlocksWithInserterSupport: () => hasChildBlocksWithInserterSupport, isReusableBlock: () => isReusableBlock, isTemplatePart: () => isTemplatePart, registerBlockBindingsSource: () => registerBlockBindingsSource, registerBlockCollection: () => registerBlockCollection, registerBlockStyle: () => registerBlockStyle, registerBlockType: () => registerBlockType, registerBlockVariation: () => registerBlockVariation, setDefaultBlockName: () => setDefaultBlockName, setFreeformContentHandlerName: () => setFreeformContentHandlerName, setGroupingBlockName: () => setGroupingBlockName, setUnregisteredTypeHandlerName: () => setUnregisteredTypeHandlerName, unregisterBlockBindingsSource: () => unregisterBlockBindingsSource, unregisterBlockCollection: () => unregisterBlockCollection, unregisterBlockStyle: () => unregisterBlockStyle, unregisterBlockType: () => unregisterBlockType, unregisterBlockVariation: () => unregisterBlockVariation, unstable__bootstrapServerSideBlockDefinitions: () => unstable__bootstrapServerSideBlockDefinitions }); module.exports = __toCommonJS(registration_exports); var import_data = require("@wordpress/data"); var import_i18n = require("@wordpress/i18n"); var import_warning = __toESM(require("@wordpress/warning")); var import_i18n_block = __toESM(require("./i18n-block.json")); var import_store = require("../store"); var import_lock_unlock = require("../lock-unlock"); function isObject(object) { return object !== null && typeof object === "object"; } function unstable__bootstrapServerSideBlockDefinitions(definitions) { const { addBootstrappedBlockType } = (0, import_lock_unlock.unlock)((0, import_data.dispatch)(import_store.store)); for (const [name, blockType] of Object.entries(definitions)) { addBootstrappedBlockType(name, blockType); } } function getBlockSettingsFromMetadata({ textdomain, ...metadata }) { const allowedFields = [ "apiVersion", "title", "category", "parent", "ancestor", "icon", "description", "keywords", "attributes", "providesContext", "usesContext", "selectors", "supports", "styles", "example", "variations", "blockHooks", "allowedBlocks" ]; const settings = Object.fromEntries( Object.entries(metadata).filter( ([key]) => allowedFields.includes(key) ) ); if (textdomain) { Object.keys(import_i18n_block.default).forEach((key) => { if (!settings[key]) { return; } settings[key] = translateBlockSettingUsingI18nSchema( import_i18n_block.default[key], settings[key], textdomain ); }); } return settings; } function registerBlockType(blockNameOrMetadata, settings) { const name = isObject(blockNameOrMetadata) ? blockNameOrMetadata.name : blockNameOrMetadata; if (typeof name !== "string") { (0, import_warning.default)("Block names must be strings."); return; } if (!/^[a-z][a-z0-9-]*\/[a-z][a-z0-9-]*$/.test(name)) { (0, import_warning.default)( "Block names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-block" ); return; } if ((0, import_data.select)(import_store.store).getBlockType(name)) { (0, import_warning.default)('Block "' + name + '" is already registered.'); return; } const { addBootstrappedBlockType, addUnprocessedBlockType } = (0, import_lock_unlock.unlock)( (0, import_data.dispatch)(import_store.store) ); if (isObject(blockNameOrMetadata)) { const metadata = getBlockSettingsFromMetadata(blockNameOrMetadata); addBootstrappedBlockType(name, metadata); } addUnprocessedBlockType(name, settings); return (0, import_data.select)(import_store.store).getBlockType(name); } function translateBlockSettingUsingI18nSchema(i18nSchema, settingValue, textdomain) { if (typeof i18nSchema === "string" && typeof settingValue === "string") { return (0, import_i18n._x)(settingValue, i18nSchema, textdomain); } if (Array.isArray(i18nSchema) && i18nSchema.length && Array.isArray(settingValue)) { return settingValue.map( (value) => translateBlockSettingUsingI18nSchema( i18nSchema[0], value, textdomain ) ); } if (isObject(i18nSchema) && Object.entries(i18nSchema).length && isObject(settingValue)) { return Object.keys(settingValue).reduce((accumulator, key) => { if (!i18nSchema[key]) { accumulator[key] = settingValue[key]; return accumulator; } accumulator[key] = translateBlockSettingUsingI18nSchema( i18nSchema[key], settingValue[key], textdomain ); return accumulator; }, {}); } return settingValue; } function registerBlockCollection(namespace, { title, icon }) { (0, import_data.dispatch)(import_store.store).addBlockCollection(namespace, title, icon); } function unregisterBlockCollection(namespace) { (0, import_data.dispatch)(import_store.store).removeBlockCollection(namespace); } function unregisterBlockType(name) { const oldBlock = (0, import_data.select)(import_store.store).getBlockType(name); if (!oldBlock) { (0, import_warning.default)('Block "' + name + '" is not registered.'); return; } (0, import_data.dispatch)(import_store.store).removeBlockTypes(name); return oldBlock; } function setFreeformContentHandlerName(blockName) { (0, import_data.dispatch)(import_store.store).setFreeformFallbackBlockName(blockName); } function getFreeformContentHandlerName() { return (0, import_data.select)(import_store.store).getFreeformFallbackBlockName(); } function getGroupingBlockName() { return (0, import_data.select)(import_store.store).getGroupingBlockName(); } function setUnregisteredTypeHandlerName(blockName) { (0, import_data.dispatch)(import_store.store).setUnregisteredFallbackBlockName(blockName); } function getUnregisteredTypeHandlerName() { return (0, import_data.select)(import_store.store).getUnregisteredFallbackBlockName(); } function setDefaultBlockName(name) { (0, import_data.dispatch)(import_store.store).setDefaultBlockName(name); } function setGroupingBlockName(name) { (0, import_data.dispatch)(import_store.store).setGroupingBlockName(name); } function getDefaultBlockName() { return (0, import_data.select)(import_store.store).getDefaultBlockName(); } function getBlockType(name) { return (0, import_data.select)(import_store.store)?.getBlockType(name); } function getBlockTypes() { return (0, import_data.select)(import_store.store).getBlockTypes(); } function getBlockSupport(nameOrType, feature, defaultSupports) { return (0, import_data.select)(import_store.store).getBlockSupport( nameOrType, feature, defaultSupports ); } function hasBlockSupport(nameOrType, feature, defaultSupports) { return (0, import_data.select)(import_store.store).hasBlockSupport( nameOrType, feature, defaultSupports ); } function isReusableBlock(blockOrType) { return blockOrType?.name === "core/block"; } function isTemplatePart(blockOrType) { return blockOrType?.name === "core/template-part"; } var getChildBlockNames = (blockName) => { return (0, import_data.select)(import_store.store).getChildBlockNames(blockName); }; var hasChildBlocks = (blockName) => { return (0, import_data.select)(import_store.store).hasChildBlocks(blockName); }; var hasChildBlocksWithInserterSupport = (blockName) => { return (0, import_data.select)(import_store.store).hasChildBlocksWithInserterSupport(blockName); }; var registerBlockStyle = (blockNames, styleVariation) => { (0, import_data.dispatch)(import_store.store).addBlockStyles(blockNames, styleVariation); }; var unregisterBlockStyle = (blockName, styleVariationName) => { (0, import_data.dispatch)(import_store.store).removeBlockStyles(blockName, styleVariationName); }; var getBlockVariations = (blockName, scope) => { return (0, import_data.select)(import_store.store).getBlockVariations(blockName, scope); }; var registerBlockVariation = (blockName, variation) => { if (typeof variation.name !== "string") { (0, import_warning.default)("Variation names must be unique strings."); } (0, import_data.dispatch)(import_store.store).addBlockVariations(blockName, variation); }; var unregisterBlockVariation = (blockName, variationName) => { (0, import_data.dispatch)(import_store.store).removeBlockVariations(blockName, variationName); }; var registerBlockBindingsSource = (source) => { const { name, label, usesContext, getValues, setValues, canUserEditValue, getFieldsList } = source; const existingSource = (0, import_lock_unlock.unlock)( (0, import_data.select)(import_store.store) ).getBlockBindingsSource(name); const serverProps = ["label", "usesContext"]; for (const prop in existingSource) { if (!serverProps.includes(prop) && existingSource[prop]) { (0, import_warning.default)( 'Block bindings source "' + name + '" is already registered.' ); return; } } if (!name) { (0, import_warning.default)("Block bindings source must contain a name."); return; } if (typeof name !== "string") { (0, import_warning.default)("Block bindings source name must be a string."); return; } if (/[A-Z]+/.test(name)) { (0, import_warning.default)( "Block bindings source name must not contain uppercase characters." ); return; } if (!/^[a-z0-9/-]+$/.test(name)) { (0, import_warning.default)( "Block bindings source name must contain only valid characters: lowercase characters, hyphens, or digits. Example: my-plugin/my-custom-source." ); return; } if (!/^[a-z0-9-]+\/[a-z0-9-]+$/.test(name)) { (0, import_warning.default)( "Block bindings source name must contain a namespace and valid characters. Example: my-plugin/my-custom-source." ); return; } if (!label && !existingSource?.label) { (0, import_warning.default)("Block bindings source must contain a label."); return; } if (label && typeof label !== "string") { (0, import_warning.default)("Block bindings source label must be a string."); return; } if (label && existingSource?.label && label !== existingSource?.label) { (0, import_warning.default)('Block bindings "' + name + '" source label was overridden.'); } if (usesContext && !Array.isArray(usesContext)) { (0, import_warning.default)("Block bindings source usesContext must be an array."); return; } if (getValues && typeof getValues !== "function") { (0, import_warning.default)("Block bindings source getValues must be a function."); return; } if (setValues && typeof setValues !== "function") { (0, import_warning.default)("Block bindings source setValues must be a function."); return; } if (canUserEditValue && typeof canUserEditValue !== "function") { (0, import_warning.default)("Block bindings source canUserEditValue must be a function."); return; } if (getFieldsList && typeof getFieldsList !== "function") { (0, import_warning.default)("Block bindings source getFieldsList must be a function."); return; } return (0, import_lock_unlock.unlock)((0, import_data.dispatch)(import_store.store)).addBlockBindingsSource(source); }; function unregisterBlockBindingsSource(name) { const oldSource = getBlockBindingsSource(name); if (!oldSource) { (0, import_warning.default)('Block bindings source "' + name + '" is not registered.'); return; } (0, import_lock_unlock.unlock)((0, import_data.dispatch)(import_store.store)).removeBlockBindingsSource(name); } function getBlockBindingsSource(name) { return (0, import_lock_unlock.unlock)((0, import_data.select)(import_store.store)).getBlockBindingsSource(name); } function getBlockBindingsSources() { return (0, import_lock_unlock.unlock)((0, import_data.select)(import_store.store)).getAllBlockBindingsSources(); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { getBlockBindingsSource, getBlockBindingsSources, getBlockSupport, getBlockType, getBlockTypes, getBlockVariations, getChildBlockNames, getDefaultBlockName, getFreeformContentHandlerName, getGroupingBlockName, getUnregisteredTypeHandlerName, hasBlockSupport, hasChildBlocks, hasChildBlocksWithInserterSupport, isReusableBlock, isTemplatePart, registerBlockBindingsSource, registerBlockCollection, registerBlockStyle, registerBlockType, registerBlockVariation, setDefaultBlockName, setFreeformContentHandlerName, setGroupingBlockName, setUnregisteredTypeHandlerName, unregisterBlockBindingsSource, unregisterBlockCollection, unregisterBlockStyle, unregisterBlockType, unregisterBlockVariation, unstable__bootstrapServerSideBlockDefinitions }); //# sourceMappingURL=registration.js.map