@wordpress/block-library
Version:
Block library for the WordPress editor.
40 lines (39 loc) • 1.97 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
let _wordpress_blocks = require("@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 = (0, _wordpress_blocks.getBlockType)(newBlockName);
if (!newBlockType) return;
const transformedAttributes = {};
if ((0, _wordpress_blocks.hasBlockSupport)(newBlockType, "anchor") && attributes.anchor) transformedAttributes.anchor = attributes.anchor;
if ((0, _wordpress_blocks.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;
}
exports.getTransformedAttributes = getTransformedAttributes;
//# sourceMappingURL=get-transformed-attributes.cjs.map