@wordpress/block-library
Version:
Block library for the WordPress editor.
76 lines (75 loc) • 2.22 kB
JavaScript
// packages/block-library/src/buttons/transforms.js
import { createBlock } from "@wordpress/blocks";
import { __unstableCreateElement as createElement } from "@wordpress/rich-text";
import { getTransformedAttributes } from "../utils/get-transformed-attributes";
var transforms = {
from: [
{
type: "block",
isMultiBlock: true,
blocks: ["core/button"],
transform: (buttons) => (
// Creates the buttons block.
createBlock(
"core/buttons",
{},
// Loop the selected buttons.
buttons.map(
(attributes) => (
// Create singular button in the buttons block.
createBlock("core/button", attributes)
)
)
)
)
},
{
type: "block",
isMultiBlock: true,
blocks: ["core/paragraph"],
transform: (buttons) => (
// Creates the buttons block.
createBlock(
"core/buttons",
{},
// Loop the selected buttons.
buttons.map((attributes) => {
const { content } = attributes;
const element = createElement(document, content);
const text = element.innerText || "";
const link = element.querySelector("a");
const url = link?.getAttribute("href");
return createBlock("core/button", {
...attributes,
...getTransformedAttributes(
attributes,
"core/button",
({ content: contentBinding }) => ({
text: contentBinding
})
),
text,
url
});
})
)
),
isMatch: (paragraphs) => {
return paragraphs.every((attributes) => {
const element = createElement(
document,
attributes.content
);
const text = element.innerText || "";
const links = element.querySelectorAll("a");
return text.length <= 30 && links.length <= 1;
});
}
}
]
};
var transforms_default = transforms;
export {
transforms_default as default
};
//# sourceMappingURL=transforms.js.map