UNPKG

@wordpress/block-library

Version:
35 lines (34 loc) • 1.76 kB
import { getBlockType, hasBlockSupport } from "@wordpress/blocks"; // packages/block-library/src/utils/get-transformed-attributes.js /** * Transform block support attributes and metadata during block transforms. * * @param {Object} attributes Original attributes from the block being transformed. * @param {string} newBlockName Name of the target block after transformation. * @param {Function} bindingsCallback Optional callback to transform the `bindings` property object. * @return {Object} New attributes object with preserved block support attributes and metadata. */ function getTransformedAttributes(attributes, newBlockName, bindingsCallback = null) { if (!attributes) return; const newBlockType = getBlockType(newBlockName); if (!newBlockType) return; const transformedAttributes = {}; if (hasBlockSupport(newBlockType, "anchor") && attributes.anchor) transformedAttributes.anchor = attributes.anchor; if (hasBlockSupport(newBlockType, "ariaLabel") && attributes.ariaLabel) transformedAttributes.ariaLabel = attributes.ariaLabel; if (attributes.metadata) { const transformedMetadata = []; if (bindingsCallback) transformedMetadata.push("id", "bindings"); if (transformedMetadata.length > 0) { const newMetadata = Object.entries(attributes.metadata).reduce((obj, [prop, value]) => { if (!transformedMetadata.includes(prop)) return obj; obj[prop] = prop === "bindings" ? bindingsCallback(value) : value; return obj; }, {}); if (Object.keys(newMetadata).length > 0) transformedAttributes.metadata = newMetadata; } } if (Object.keys(transformedAttributes).length === 0) return; return transformedAttributes; } export { getTransformedAttributes }; //# sourceMappingURL=get-transformed-attributes.mjs.map