UNPKG

@wordpress/block-editor

Version:
67 lines (53 loc) 2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.addMetaAttribute = addMetaAttribute; exports.addSaveProps = addSaveProps; exports.hasBlockMetadataSupport = hasBlockMetadataSupport; var _hooks = require("@wordpress/hooks"); var _blocks = require("@wordpress/blocks"); /** * WordPress dependencies */ const META_ATTRIBUTE_NAME = 'metadata'; function hasBlockMetadataSupport(blockType, feature = '') { // Only core blocks are allowed to use __experimentalMetadata until the fetaure is stablised. if (!blockType.name.startsWith('core/')) { return false; } const support = (0, _blocks.getBlockSupport)(blockType, '__experimentalMetadata'); return !!(true === support || support?.[feature]); } /** * Filters registered block settings, extending attributes to include `metadata`. * * see: https://github.com/WordPress/gutenberg/pull/40393/files#r864632012 * * @param {Object} blockTypeSettings Original block settings. * @return {Object} Filtered block settings. */ function addMetaAttribute(blockTypeSettings) { // Allow blocks to specify their own attribute definition with default values if needed. if (blockTypeSettings?.attributes?.[META_ATTRIBUTE_NAME]?.type) { return blockTypeSettings; } const supportsBlockNaming = hasBlockMetadataSupport(blockTypeSettings, 'name'); if (supportsBlockNaming) { blockTypeSettings.attributes = { ...blockTypeSettings.attributes, [META_ATTRIBUTE_NAME]: { type: 'object' } }; } return blockTypeSettings; } function addSaveProps(extraProps, blockType, attributes) { if (hasBlockMetadataSupport(blockType)) { extraProps[META_ATTRIBUTE_NAME] = attributes[META_ATTRIBUTE_NAME]; } return extraProps; } (0, _hooks.addFilter)('blocks.registerBlockType', 'core/metadata/addMetaAttribute', addMetaAttribute); (0, _hooks.addFilter)('blocks.getSaveContent.extraProps', 'core/metadata/save-props', addSaveProps); //# sourceMappingURL=metadata.js.map