UNPKG

@wordpress/block-library

Version:
88 lines (87 loc) 2.68 kB
// packages/block-library/src/list/utils.js import { createBlock, rawHandler } from "@wordpress/blocks"; var LIST_STYLES = { A: "upper-alpha", a: "lower-alpha", I: "upper-roman", i: "lower-roman" }; function createListBlockFromDOMElement(listElement) { const type = listElement.getAttribute("type"); const listAttributes = { ordered: "OL" === listElement.tagName, anchor: listElement.id === "" ? void 0 : listElement.id, start: listElement.getAttribute("start") ? parseInt(listElement.getAttribute("start"), 10) : void 0, reversed: listElement.hasAttribute("reversed") ? true : void 0, type: type && LIST_STYLES[type] ? LIST_STYLES[type] : void 0 }; const innerBlocks = Array.from(listElement.children).map( (listItem) => { const children = Array.from(listItem.childNodes).filter( (node) => node.nodeType !== node.TEXT_NODE || node.textContent.trim().length !== 0 ); children.reverse(); const [nestedList, ...nodes] = children; const hasNestedList = nestedList?.tagName === "UL" || nestedList?.tagName === "OL"; if (!hasNestedList) { return createBlock("core/list-item", { content: listItem.innerHTML }); } const htmlNodes = nodes.map((node) => { if (node.nodeType === node.TEXT_NODE) { return node.textContent; } return node.outerHTML; }); htmlNodes.reverse(); const childAttributes = { content: htmlNodes.join("").trim() }; const childInnerBlocks = [ createListBlockFromDOMElement(nestedList) ]; return createBlock( "core/list-item", childAttributes, childInnerBlocks ); } ); return createBlock("core/list", listAttributes, innerBlocks); } function migrateToListV2(attributes) { const { values, start, reversed, ordered, type, ...otherAttributes } = attributes; const list = document.createElement(ordered ? "ol" : "ul"); list.innerHTML = values; if (start) { list.setAttribute("start", start); } if (reversed) { list.setAttribute("reversed", true); } if (type) { list.setAttribute("type", type); } const [listBlock] = rawHandler({ HTML: list.outerHTML }); return [ { ...otherAttributes, ...listBlock.attributes }, listBlock.innerBlocks ]; } function migrateTypeToInlineStyle(attributes) { const { type } = attributes; if (type && LIST_STYLES[type]) { return { ...attributes, type: LIST_STYLES[type] }; } return attributes; } export { createListBlockFromDOMElement, migrateToListV2, migrateTypeToInlineStyle }; //# sourceMappingURL=utils.js.map