@wordpress/blocks
Version:
Block API for WordPress.
8 lines (7 loc) • 11.4 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/store/process-block-type.js"],
"sourcesContent": ["/**\n * External dependencies\n */\nimport { isPlainObject } from 'is-plain-object';\nimport { isValidElementType } from 'react-is';\n\n/**\n * WordPress dependencies\n */\nimport deprecated from '@wordpress/deprecated';\nimport { applyFilters } from '@wordpress/hooks';\nimport warning from '@wordpress/warning';\n\n/**\n * Internal dependencies\n */\nimport { isValidIcon, normalizeIconObject, omit } from '../api/utils';\nimport { BLOCK_ICON_DEFAULT, DEPRECATED_ENTRY_KEYS } from '../api/constants';\n\n/** @typedef {import('../api/registration').WPBlockType} WPBlockType */\n\n/**\n * Mapping of legacy category slugs to their latest normal values, used to\n * accommodate updates of the default set of block categories.\n *\n * @type {Record<string,string>}\n */\nconst LEGACY_CATEGORY_MAPPING = {\n\tcommon: 'text',\n\tformatting: 'text',\n\tlayout: 'design',\n};\n\n/**\n * Merge block variations bootstrapped from the server and client.\n *\n * When a variation is registered in both places, its properties are merged.\n *\n * @param {Array} bootstrappedVariations - A block type variations from the server.\n * @param {Array} clientVariations - A block type variations from the client.\n * @return {Array} The merged array of block variations.\n */\nfunction mergeBlockVariations(\n\tbootstrappedVariations = [],\n\tclientVariations = []\n) {\n\tconst result = [ ...bootstrappedVariations ];\n\n\tclientVariations.forEach( ( clientVariation ) => {\n\t\tconst index = result.findIndex(\n\t\t\t( bootstrappedVariation ) =>\n\t\t\t\tbootstrappedVariation.name === clientVariation.name\n\t\t);\n\n\t\tif ( index !== -1 ) {\n\t\t\tresult[ index ] = { ...result[ index ], ...clientVariation };\n\t\t} else {\n\t\t\tresult.push( clientVariation );\n\t\t}\n\t} );\n\n\treturn result;\n}\n\n/**\n * Takes the unprocessed block type settings, merges them with block type metadata\n * and applies all the existing filters for the registered block type.\n * Next, it validates all the settings and performs additional processing to the block type definition.\n *\n * @param {string} name Block name.\n * @param {WPBlockType} blockSettings Unprocessed block type settings.\n *\n * @return {WPBlockType | undefined} The block, if it has been processed and can be registered; otherwise `undefined`.\n */\nexport const processBlockType =\n\t( name, blockSettings ) =>\n\t( { select } ) => {\n\t\tconst bootstrappedBlockType = select.getBootstrappedBlockType( name );\n\n\t\tconst blockType = {\n\t\t\tapiVersion: 1,\n\t\t\tname,\n\t\t\ticon: BLOCK_ICON_DEFAULT,\n\t\t\tkeywords: [],\n\t\t\tattributes: {},\n\t\t\tprovidesContext: {},\n\t\t\tusesContext: [],\n\t\t\tselectors: {},\n\t\t\tsupports: {},\n\t\t\tstyles: [],\n\t\t\tblockHooks: {},\n\t\t\tsave: () => null,\n\t\t\t...bootstrappedBlockType,\n\t\t\t...blockSettings,\n\t\t\t// blockType.variations can be defined as a filePath.\n\t\t\tvariations: mergeBlockVariations(\n\t\t\t\tArray.isArray( bootstrappedBlockType?.variations )\n\t\t\t\t\t? bootstrappedBlockType.variations\n\t\t\t\t\t: [],\n\t\t\t\tArray.isArray( blockSettings?.variations )\n\t\t\t\t\t? blockSettings.variations\n\t\t\t\t\t: []\n\t\t\t),\n\t\t};\n\n\t\t// If the block is registering attributes as null or undefined, warn and default to empty object.\n\t\tif (\n\t\t\t! blockType.attributes ||\n\t\t\ttypeof blockType.attributes !== 'object'\n\t\t) {\n\t\t\twarning(\n\t\t\t\t'The block \"' +\n\t\t\t\t\tname +\n\t\t\t\t\t'\" is registering attributes as `null` or `undefined`. Use an empty object (`attributes: {}`) or exclude the `attributes` key.'\n\t\t\t);\n\t\t\tblockType.attributes = {};\n\t\t}\n\n\t\tconst settings = applyFilters(\n\t\t\t'blocks.registerBlockType',\n\t\t\tblockType,\n\t\t\tname,\n\t\t\tnull\n\t\t);\n\n\t\tif ( settings.apiVersion <= 2 ) {\n\t\t\tdeprecated( 'Block with API version 2 or lower', {\n\t\t\t\tsince: '6.9',\n\t\t\t\thint: `The block \"${ name }\" is registered with API version ${ settings.apiVersion }. 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.`,\n\t\t\t\tlink: 'https://developer.wordpress.org/block-editor/reference-guides/block-api/block-api-versions/block-migration-for-iframe-editor-compatibility/',\n\t\t\t} );\n\t\t}\n\n\t\tif (\n\t\t\tsettings.description &&\n\t\t\ttypeof settings.description !== 'string'\n\t\t) {\n\t\t\tdeprecated( 'Declaring non-string block descriptions', {\n\t\t\t\tsince: '6.2',\n\t\t\t} );\n\t\t}\n\n\t\tif ( settings.deprecated ) {\n\t\t\tsettings.deprecated = settings.deprecated.map( ( deprecation ) =>\n\t\t\t\tObject.fromEntries(\n\t\t\t\t\tObject.entries(\n\t\t\t\t\t\t// Only keep valid deprecation keys.\n\t\t\t\t\t\tapplyFilters(\n\t\t\t\t\t\t\t'blocks.registerBlockType',\n\t\t\t\t\t\t\t// Merge deprecation keys with pre-filter settings\n\t\t\t\t\t\t\t// so that filters that depend on specific keys being\n\t\t\t\t\t\t\t// present don't fail.\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t// Omit deprecation keys here so that deprecations\n\t\t\t\t\t\t\t\t// can opt out of specific keys like \"supports\".\n\t\t\t\t\t\t\t\t...omit( blockType, DEPRECATED_ENTRY_KEYS ),\n\t\t\t\t\t\t\t\t...deprecation,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tblockType.name,\n\t\t\t\t\t\t\tdeprecation\n\t\t\t\t\t\t)\n\t\t\t\t\t).filter( ( [ key ] ) =>\n\t\t\t\t\t\tDEPRECATED_ENTRY_KEYS.includes( key )\n\t\t\t\t\t)\n\t\t\t\t)\n\t\t\t);\n\t\t}\n\n\t\tif ( ! isPlainObject( settings ) ) {\n\t\t\twarning( 'Block settings must be a valid object.' );\n\t\t\treturn;\n\t\t}\n\n\t\tif ( typeof settings.save !== 'function' ) {\n\t\t\twarning( 'The \"save\" property must be a valid function.' );\n\t\t\treturn;\n\t\t}\n\t\tif ( 'edit' in settings && ! isValidElementType( settings.edit ) ) {\n\t\t\twarning( 'The \"edit\" property must be a valid component.' );\n\t\t\treturn;\n\t\t}\n\n\t\t// Canonicalize legacy categories to equivalent fallback.\n\t\tif ( LEGACY_CATEGORY_MAPPING.hasOwnProperty( settings.category ) ) {\n\t\t\tsettings.category = LEGACY_CATEGORY_MAPPING[ settings.category ];\n\t\t}\n\n\t\tif (\n\t\t\t'category' in settings &&\n\t\t\t! select\n\t\t\t\t.getCategories()\n\t\t\t\t.some( ( { slug } ) => slug === settings.category )\n\t\t) {\n\t\t\twarning(\n\t\t\t\t'The block \"' +\n\t\t\t\t\tname +\n\t\t\t\t\t'\" is registered with an invalid category \"' +\n\t\t\t\t\tsettings.category +\n\t\t\t\t\t'\".'\n\t\t\t);\n\t\t\tdelete settings.category;\n\t\t}\n\n\t\tif ( ! ( 'title' in settings ) || settings.title === '' ) {\n\t\t\twarning( 'The block \"' + name + '\" must have a title.' );\n\t\t\treturn;\n\t\t}\n\t\tif ( typeof settings.title !== 'string' ) {\n\t\t\twarning( 'Block titles must be strings.' );\n\t\t\treturn;\n\t\t}\n\n\t\tsettings.icon = normalizeIconObject( settings.icon );\n\t\tif ( ! isValidIcon( settings.icon.src ) ) {\n\t\t\twarning(\n\t\t\t\t'The icon passed is invalid. ' +\n\t\t\t\t\t'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'\n\t\t\t);\n\t\t\treturn;\n\t\t}\n\n\t\tif (\n\t\t\ttypeof settings?.parent === 'string' ||\n\t\t\tsettings?.parent instanceof String\n\t\t) {\n\t\t\tsettings.parent = [ settings.parent ];\n\t\t\twarning(\n\t\t\t\t'Parent must be undefined or an array of strings (block types), but it is a string.'\n\t\t\t);\n\t\t\t// Intentionally continue:\n\t\t\t//\n\t\t\t// While string values were never supported, they appeared to work with some unintended side-effects\n\t\t\t// that have been fixed by [#66250](https://github.com/WordPress/gutenberg/pull/66250).\n\t\t\t//\n\t\t\t// To be backwards-compatible, this code that automatically migrates strings to arrays.\n\t\t}\n\n\t\tif (\n\t\t\t! Array.isArray( settings?.parent ) &&\n\t\t\tsettings?.parent !== undefined\n\t\t) {\n\t\t\twarning(\n\t\t\t\t'Parent must be undefined or an array of block types, but it is ',\n\t\t\t\tsettings.parent\n\t\t\t);\n\t\t\treturn;\n\t\t}\n\n\t\tif ( 1 === settings?.parent?.length && name === settings.parent[ 0 ] ) {\n\t\t\twarning(\n\t\t\t\t'Block \"' +\n\t\t\t\t\tname +\n\t\t\t\t\t'\" cannot be a parent of itself. Please remove the block name from the parent list.'\n\t\t\t);\n\t\t\treturn;\n\t\t}\n\n\t\treturn settings;\n\t};\n"],
"mappings": ";AAGA,SAAS,qBAAqB;AAC9B,SAAS,0BAA0B;AAKnC,OAAO,gBAAgB;AACvB,SAAS,oBAAoB;AAC7B,OAAO,aAAa;AAKpB,SAAS,aAAa,qBAAqB,YAAY;AACvD,SAAS,oBAAoB,6BAA6B;AAU1D,IAAM,0BAA0B;AAAA,EAC/B,QAAQ;AAAA,EACR,YAAY;AAAA,EACZ,QAAQ;AACT;AAWA,SAAS,qBACR,yBAAyB,CAAC,GAC1B,mBAAmB,CAAC,GACnB;AACD,QAAM,SAAS,CAAE,GAAG,sBAAuB;AAE3C,mBAAiB,QAAS,CAAE,oBAAqB;AAChD,UAAM,QAAQ,OAAO;AAAA,MACpB,CAAE,0BACD,sBAAsB,SAAS,gBAAgB;AAAA,IACjD;AAEA,QAAK,UAAU,IAAK;AACnB,aAAQ,KAAM,IAAI,EAAE,GAAG,OAAQ,KAAM,GAAG,GAAG,gBAAgB;AAAA,IAC5D,OAAO;AACN,aAAO,KAAM,eAAgB;AAAA,IAC9B;AAAA,EACD,CAAE;AAEF,SAAO;AACR;AAYO,IAAM,mBACZ,CAAE,MAAM,kBACR,CAAE,EAAE,OAAO,MAAO;AACjB,QAAM,wBAAwB,OAAO,yBAA0B,IAAK;AAEpE,QAAM,YAAY;AAAA,IACjB,YAAY;AAAA,IACZ;AAAA,IACA,MAAM;AAAA,IACN,U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