@wordpress/block-library
Version:
Block library for the WordPress editor.
59 lines (58 loc) • 1.97 kB
JavaScript
import { getBlockType, hasBlockSupport } from "@wordpress/blocks";
function getTransformedAttributes(attributes, newBlockName, bindingsCallback = null) {
if (!attributes) {
return void 0;
}
const newBlockType = getBlockType(newBlockName);
if (!newBlockType) {
return void 0;
}
const transformedAttributes = {};
if (hasBlockSupport(newBlockType, "allowedBlocks") && attributes.allowedBlocks) {
transformedAttributes.allowedBlocks = attributes.allowedBlocks;
}
if (hasBlockSupport(newBlockType, "anchor") && attributes.anchor) {
transformedAttributes.anchor = attributes.anchor;
}
if (hasBlockSupport(newBlockType, "ariaLabel") && attributes.ariaLabel) {
transformedAttributes.ariaLabel = attributes.ariaLabel;
}
if (hasBlockSupport(newBlockType, "className") && attributes.className) {
transformedAttributes.className = attributes.className;
}
if (attributes.metadata) {
const transformedMetadata = ["noteId"];
if (bindingsCallback) {
transformedMetadata.push("id", "bindings");
}
if (hasBlockSupport(newBlockType, "renaming", true)) {
transformedMetadata.push("name");
}
if (hasBlockSupport(newBlockType, "blockVisibility", true)) {
transformedMetadata.push("blockVisibility");
}
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 void 0;
}
return transformedAttributes;
}
export {
getTransformedAttributes
};
//# sourceMappingURL=get-transformed-attributes.js.map