@wordpress/block-library
Version:
Block library for the WordPress editor.
1 lines • 4.43 kB
Source Map (JSON)
{"version":3,"file":"utils.cjs","names":["createBlock","rawHandler"],"sources":["../../src/list/utils.js"],"sourcesContent":["import { createBlock, rawHandler } from '@wordpress/blocks';\n\nconst LIST_STYLES = {\n\tA: 'upper-alpha',\n\ta: 'lower-alpha',\n\tI: 'upper-roman',\n\ti: 'lower-roman',\n};\n\nexport function createListBlockFromDOMElement( listElement ) {\n\tconst type = listElement.getAttribute( 'type' );\n\tconst listAttributes = {\n\t\tordered: 'OL' === listElement.tagName,\n\t\tanchor: listElement.id ? listElement.id : undefined,\n\t\tstart: listElement.getAttribute( 'start' )\n\t\t\t? parseInt( listElement.getAttribute( 'start' ), 10 )\n\t\t\t: undefined,\n\t\treversed: listElement.hasAttribute( 'reversed' ) ? true : undefined,\n\t\ttype: type && LIST_STYLES[ type ] ? LIST_STYLES[ type ] : undefined,\n\t};\n\n\tconst innerBlocks = Array.from( listElement.children ).map(\n\t\t( listItem ) => {\n\t\t\tconst children = Array.from( listItem.childNodes ).filter(\n\t\t\t\t( node ) =>\n\t\t\t\t\tnode.nodeType !== node.TEXT_NODE ||\n\t\t\t\t\tnode.textContent.trim().length !== 0\n\t\t\t);\n\t\t\tchildren.reverse();\n\t\t\tconst [ nestedList, ...nodes ] = children;\n\n\t\t\tconst hasNestedList =\n\t\t\t\tnestedList?.tagName === 'UL' || nestedList?.tagName === 'OL';\n\t\t\tif ( ! hasNestedList ) {\n\t\t\t\treturn createBlock( 'core/list-item', {\n\t\t\t\t\tcontent: listItem.innerHTML,\n\t\t\t\t} );\n\t\t\t}\n\t\t\tconst htmlNodes = nodes.map( ( node ) => {\n\t\t\t\tif ( node.nodeType === node.TEXT_NODE ) {\n\t\t\t\t\treturn node.textContent;\n\t\t\t\t}\n\t\t\t\treturn node.outerHTML;\n\t\t\t} );\n\t\t\thtmlNodes.reverse();\n\t\t\tconst childAttributes = {\n\t\t\t\tcontent: htmlNodes.join( '' ).trim(),\n\t\t\t};\n\t\t\tconst childInnerBlocks = [\n\t\t\t\tcreateListBlockFromDOMElement( nestedList ),\n\t\t\t];\n\t\t\treturn createBlock(\n\t\t\t\t'core/list-item',\n\t\t\t\tchildAttributes,\n\t\t\t\tchildInnerBlocks\n\t\t\t);\n\t\t}\n\t);\n\n\treturn createBlock( 'core/list', listAttributes, innerBlocks );\n}\n\nexport function migrateToListV2( attributes ) {\n\tconst { values, start, reversed, ordered, type, ...otherAttributes } =\n\t\tattributes;\n\n\tconst list = document.createElement( ordered ? 'ol' : 'ul' );\n\tlist.innerHTML = values;\n\tif ( start ) {\n\t\tlist.setAttribute( 'start', start );\n\t}\n\tif ( reversed ) {\n\t\tlist.setAttribute( 'reversed', true );\n\t}\n\tif ( type ) {\n\t\tlist.setAttribute( 'type', type );\n\t}\n\n\tconst [ listBlock ] = rawHandler( { HTML: list.outerHTML } );\n\n\treturn [\n\t\t{ ...otherAttributes, ...listBlock.attributes },\n\t\tlistBlock.innerBlocks,\n\t];\n}\n\nexport function migrateTypeToInlineStyle( attributes ) {\n\tconst { type } = attributes;\n\n\tif ( type && LIST_STYLES[ type ] ) {\n\t\treturn {\n\t\t\t...attributes,\n\t\t\ttype: LIST_STYLES[ type ],\n\t\t};\n\t}\n\n\treturn attributes;\n}\n"],"mappings":";;;;;;;AAEA,MAAM,cAAc;CACnB,GAAG;CACH,GAAG;CACH,GAAG;CACH,GAAG;AACJ;AAEA,SAAgB,8BAA+B,aAAc;CAC5D,MAAM,OAAO,YAAY,aAAc,MAAO;CAC9C,MAAM,iBAAiB;EACtB,SAAS,SAAS,YAAY;EAC9B,QAAQ,YAAY,KAAK,YAAY,KAAK,KAAA;EAC1C,OAAO,YAAY,aAAc,OAAQ,IACtC,SAAU,YAAY,aAAc,OAAQ,GAAG,EAAG,IAClD,KAAA;EACH,UAAU,YAAY,aAAc,UAAW,IAAI,OAAO,KAAA;EAC1D,MAAM,QAAQ,YAAa,QAAS,YAAa,QAAS,KAAA;CAC3D;C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