UNPKG

@wordpress/block-library

Version:
8 lines (7 loc) 9.21 kB
{ "version": 3, "sources": ["../../src/columns/deprecated.js"], "sourcesContent": ["/**\n * External dependencies\n */\nimport clsx from 'clsx';\n\n/**\n * WordPress dependencies\n */\nimport { createBlock } from '@wordpress/blocks';\nimport { InnerBlocks, getColorClassName } from '@wordpress/block-editor';\n\n/**\n * Given an HTML string for a deprecated columns inner block, returns the\n * column index to which the migrated inner block should be assigned. Returns\n * undefined if the inner block was not assigned to a column.\n *\n * @param {string} originalContent Deprecated Columns inner block HTML.\n *\n * @return {number | undefined} Column to which inner block is to be assigned.\n */\nfunction getDeprecatedLayoutColumn( originalContent ) {\n\tlet { doc } = getDeprecatedLayoutColumn;\n\tif ( ! doc ) {\n\t\tdoc = document.implementation.createHTMLDocument( '' );\n\t\tgetDeprecatedLayoutColumn.doc = doc;\n\t}\n\n\tlet columnMatch;\n\n\tdoc.body.innerHTML = originalContent;\n\tfor ( const classListItem of doc.body.firstChild.classList ) {\n\t\tif (\n\t\t\t( columnMatch = classListItem.match( /^layout-column-(\\d+)$/ ) )\n\t\t) {\n\t\t\treturn Number( columnMatch[ 1 ] ) - 1;\n\t\t}\n\t}\n}\n\nconst migrateCustomColors = ( attributes ) => {\n\tif ( ! attributes.customTextColor && ! attributes.customBackgroundColor ) {\n\t\treturn attributes;\n\t}\n\tconst style = { color: {} };\n\tif ( attributes.customTextColor ) {\n\t\tstyle.color.text = attributes.customTextColor;\n\t}\n\tif ( attributes.customBackgroundColor ) {\n\t\tstyle.color.background = attributes.customBackgroundColor;\n\t}\n\n\tconst { customTextColor, customBackgroundColor, ...restAttributes } =\n\t\tattributes;\n\n\treturn {\n\t\t...restAttributes,\n\t\tstyle,\n\t\tisStackedOnMobile: true,\n\t};\n};\n\nexport default [\n\t{\n\t\tattributes: {\n\t\t\tverticalAlignment: {\n\t\t\t\ttype: 'string',\n\t\t\t},\n\t\t\tbackgroundColor: {\n\t\t\t\ttype: 'string',\n\t\t\t},\n\t\t\tcustomBackgroundColor: {\n\t\t\t\ttype: 'string',\n\t\t\t},\n\t\t\tcustomTextColor: {\n\t\t\t\ttype: 'string',\n\t\t\t},\n\t\t\ttextColor: {\n\t\t\t\ttype: 'string',\n\t\t\t},\n\t\t},\n\t\tmigrate: migrateCustomColors,\n\t\tsave( { attributes } ) {\n\t\t\tconst {\n\t\t\t\tverticalAlignment,\n\t\t\t\tbackgroundColor,\n\t\t\t\tcustomBackgroundColor,\n\t\t\t\ttextColor,\n\t\t\t\tcustomTextColor,\n\t\t\t} = attributes;\n\n\t\t\tconst backgroundClass = getColorClassName(\n\t\t\t\t'background-color',\n\t\t\t\tbackgroundColor\n\t\t\t);\n\n\t\t\tconst textClass = getColorClassName( 'color', textColor );\n\n\t\t\tconst className = clsx( {\n\t\t\t\t'has-background': backgroundColor || customBackgroundColor,\n\t\t\t\t'has-text-color': textColor || customTextColor,\n\t\t\t\t[ backgroundClass ]: backgroundClass,\n\t\t\t\t[ textClass ]: textClass,\n\t\t\t\t[ `are-vertically-aligned-${ verticalAlignment }` ]:\n\t\t\t\t\tverticalAlignment,\n\t\t\t} );\n\n\t\t\tconst style = {\n\t\t\t\tbackgroundColor: backgroundClass\n\t\t\t\t\t? undefined\n\t\t\t\t\t: customBackgroundColor,\n\t\t\t\tcolor: textClass ? undefined : customTextColor,\n\t\t\t};\n\n\t\t\treturn (\n\t\t\t\t<div\n\t\t\t\t\tclassName={ className ? className : undefined }\n\t\t\t\t\tstyle={ style }\n\t\t\t\t>\n\t\t\t\t\t<InnerBlocks.Content />\n\t\t\t\t</div>\n\t\t\t);\n\t\t},\n\t},\n\t{\n\t\tattributes: {\n\t\t\tcolumns: {\n\t\t\t\ttype: 'number',\n\t\t\t\tdefault: 2,\n\t\t\t},\n\t\t},\n\t\tisEligible( attributes, innerBlocks ) {\n\t\t\t// Since isEligible is called on every valid instance of the\n\t\t\t// Columns block and a deprecation is the unlikely case due to\n\t\t\t// its subsequent migration, optimize for the `false` condition\n\t\t\t// by performing a naive, inaccurate pass at inner blocks.\n\t\t\tconst isFastPassEligible = innerBlocks.some( ( innerBlock ) =>\n\t\t\t\t/layout-column-\\d+/.test( innerBlock.originalContent )\n\t\t\t);\n\n\t\t\tif ( ! isFastPassEligible ) {\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\t// Only if the fast pass is considered eligible is the more\n\t\t\t// accurate, durable, slower condition performed.\n\t\t\treturn innerBlocks.some(\n\t\t\t\t( innerBlock ) =>\n\t\t\t\t\tgetDeprecatedLayoutColumn( innerBlock.originalContent ) !==\n\t\t\t\t\tundefined\n\t\t\t);\n\t\t},\n\t\tmigrate( attributes, innerBlocks ) {\n\t\t\tconst columns = innerBlocks.reduce( ( accumulator, innerBlock ) => {\n\t\t\t\tconst { originalContent } = innerBlock;\n\n\t\t\t\tlet columnIndex = getDeprecatedLayoutColumn( originalContent );\n\t\t\t\tif ( columnIndex === undefined ) {\n\t\t\t\t\tcolumnIndex = 0;\n\t\t\t\t}\n\n\t\t\t\tif ( ! accumulator[ columnIndex ] ) {\n\t\t\t\t\taccumulator[ columnIndex ] = [];\n\t\t\t\t}\n\n\t\t\t\taccumulator[ columnIndex ].push( innerBlock );\n\n\t\t\t\treturn accumulator;\n\t\t\t}, [] );\n\n\t\t\tconst migratedInnerBlocks = columns.map( ( columnBlocks ) =>\n\t\t\t\tcreateBlock( 'core/column', {}, columnBlocks )\n\t\t\t);\n\n\t\t\tconst { columns: ignoredColumns, ...restAttributes } = attributes;\n\n\t\t\treturn [\n\t\t\t\t{\n\t\t\t\t\t...restAttributes,\n\t\t\t\t\tisStackedOnMobile: true,\n\t\t\t\t},\n\t\t\t\tmigratedInnerBlocks,\n\t\t\t];\n\t\t},\n\t\tsave( { attributes } ) {\n\t\t\tconst { columns } = attributes;\n\n\t\t\treturn (\n\t\t\t\t<div className={ `has-${ columns }-columns` }>\n\t\t\t\t\t<InnerBlocks.Content />\n\t\t\t\t</div>\n\t\t\t);\n\t\t},\n\t},\n\t{\n\t\tattributes: {\n\t\t\tcolumns: {\n\t\t\t\ttype: 'number',\n\t\t\t\tdefault: 2,\n\t\t\t},\n\t\t},\n\t\tmigrate( attributes, innerBlocks ) {\n\t\t\tconst { columns, ...restAttributes } = attributes;\n\t\t\tattributes = {\n\t\t\t\t...restAttributes,\n\t\t\t\tisStackedOnMobile: true,\n\t\t\t};\n\n\t\t\treturn [ attributes, innerBlocks ];\n\t\t},\n\t\tsave( { attributes } ) {\n\t\t\tconst { verticalAlignment, columns } = attributes;\n\n\t\t\tconst wrapperClasses = clsx( `has-${ columns }-columns`, {\n\t\t\t\t[ `are-vertically-aligned-${ verticalAlignment }` ]:\n\t\t\t\t\tverticalAlignment,\n\t\t\t} );\n\n\t\t\treturn (\n\t\t\t\t<div className={ wrapperClasses }>\n\t\t\t\t\t<InnerBlocks.Content />\n\t\t\t\t</div>\n\t\t\t);\n\t\t},\n\t},\n];\n"], "mappings": ";AAGA,OAAO,UAAU;AAKjB,SAAS,mBAAmB;AAC5B,SAAS,aAAa,yBAAyB;AA6G1C;AAlGL,SAAS,0BAA2B,iBAAkB;AACrD,MAAI,EAAE,IAAI,IAAI;AACd,MAAK,CAAE,KAAM;AACZ,UAAM,SAAS,eAAe,mBAAoB,EAAG;AACrD,8BAA0B,MAAM;AAAA,EACjC;AAEA,MAAI;AAEJ,MAAI,KAAK,YAAY;AACrB,aAAY,iBAAiB,IAAI,KAAK,WAAW,WAAY;AAC5D,QACG,cAAc,cAAc,MAAO,uBAAwB,GAC5D;AACD,aAAO,OAAQ,YAAa,CAAE,CAAE,IAAI;AAAA,IACrC;AAAA,EACD;AACD;AAEA,IAAM,sBAAsB,CAAE,eAAgB;AAC7C,MAAK,CAAE,WAAW,mBAAmB,CAAE,WAAW,uBAAwB;AACzE,WAAO;AAAA,EACR;AACA,QAAM,QAAQ,EAAE,OAAO,CAAC,EAAE;AAC1B,MAAK,WAAW,iBAAkB;AACjC,UAAM,MAAM,OAAO,WAAW;AAAA,EAC/B;AACA,MAAK,WAAW,uBA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