UNPKG

@wordpress/block-library

Version:
85 lines (73 loc) 2.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createListBlockFromDOMElement = createListBlockFromDOMElement; exports.migrateToListV2 = migrateToListV2; var _blocks = require("@wordpress/blocks"); /** * WordPress dependencies */ function createListBlockFromDOMElement(listElement) { var _listElement$getAttri; const listAttributes = { ordered: 'OL' === listElement.tagName, anchor: listElement.id === '' ? undefined : listElement.id, start: listElement.getAttribute('start') ? parseInt(listElement.getAttribute('start'), 10) : undefined, reversed: listElement.hasAttribute('reversed') ? true : undefined, type: (_listElement$getAttri = listElement.getAttribute('type')) !== null && _listElement$getAttri !== void 0 ? _listElement$getAttri : undefined }; 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 === null || nestedList === void 0 ? void 0 : nestedList.tagName) === 'UL' || (nestedList === null || nestedList === void 0 ? void 0 : nestedList.tagName) === 'OL'; if (!hasNestedList) { return (0, _blocks.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 (0, _blocks.createBlock)('core/list-item', childAttributes, childInnerBlocks); }); return (0, _blocks.createBlock)('core/list', listAttributes, innerBlocks); } function migrateToListV2(attributes) { const { values, start, reversed, ordered, type } = 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 = createListBlockFromDOMElement(list); const { values: omittedValues, ...restAttributes } = attributes; return [{ ...restAttributes, ...listBlock.attributes }, listBlock.innerBlocks]; } //# sourceMappingURL=utils.js.map