@wordpress/block-library
Version:
Block library for the WordPress editor.
85 lines (84 loc) • 2.81 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
let _wordpress_blocks = require("@wordpress/blocks");
let _wordpress_rich_text = require("@wordpress/rich-text");
let __utils_js = require("./utils.cjs");
// packages/block-library/src/list/transforms.js
function getListContentSchema({ phrasingContentSchema }) {
const listContentSchema = {
...phrasingContentSchema,
ul: {},
ol: { attributes: [
"type",
"start",
"reversed"
] }
};
["ul", "ol"].forEach((tag) => {
listContentSchema[tag].children = { li: { children: listContentSchema } };
});
return listContentSchema;
}
function getListContentFlat(blocks) {
return blocks.flatMap(({ name, attributes, innerBlocks = [] }) => {
if (name === "core/list-item") return [attributes.content, ...getListContentFlat(innerBlocks)];
return getListContentFlat(innerBlocks);
});
}
const transforms = {
from: [
{
type: "block",
isMultiBlock: true,
blocks: ["core/paragraph", "core/heading"],
transform: (blockAttributes) => {
let childBlocks = [];
if (blockAttributes.length > 1) childBlocks = blockAttributes.map(({ content }) => {
return (0, _wordpress_blocks.createBlock)("core/list-item", { content });
});
else if (blockAttributes.length === 1) {
const value = (0, _wordpress_rich_text.create)({ html: blockAttributes[0].content });
childBlocks = (0, _wordpress_rich_text.split)(value, "\n").map((result) => {
return (0, _wordpress_blocks.createBlock)("core/list-item", { content: (0, _wordpress_rich_text.toHTMLString)({ value: result }) });
});
}
return (0, _wordpress_blocks.createBlock)("core/list", { anchor: blockAttributes.anchor }, childBlocks);
}
},
{
type: "raw",
selector: "ol,ul",
schema: (args) => ({
ol: getListContentSchema(args).ol,
ul: getListContentSchema(args).ul
}),
transform: __utils_js.createListBlockFromDOMElement
},
...["*", "-"].map((prefix) => ({
type: "prefix",
prefix,
transform(content) {
return (0, _wordpress_blocks.createBlock)("core/list", {}, [(0, _wordpress_blocks.createBlock)("core/list-item", { content })]);
}
})),
...["1.", "1)"].map((prefix) => ({
type: "prefix",
prefix,
transform(content) {
return (0, _wordpress_blocks.createBlock)("core/list", { ordered: true }, [(0, _wordpress_blocks.createBlock)("core/list-item", { content })]);
}
}))
],
to: [...["core/paragraph", "core/heading"].map((block) => ({
type: "block",
blocks: [block],
transform: (_attributes, childBlocks) => {
return getListContentFlat(childBlocks).map((content) => (0, _wordpress_blocks.createBlock)(block, { content }));
}
}))]
};
exports.default = transforms;
//# sourceMappingURL=transforms.cjs.map