@wordpress/block-library
Version:
Block library for the WordPress editor.
1 lines • 8.4 kB
Source Map (JSON)
{"version":3,"file":"deprecated.mjs","names":[],"sources":["../../src/columns/deprecated.jsx"],"sourcesContent":["import clsx from 'clsx';\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":";;;;;;;;;;;;;;AAaA,SAAS,0BAA2B,iBAAkB;CACrD,IAAI,EAAE,QAAQ;CACd,IAAK,CAAE,KAAM;EACZ,MAAM,SAAS,eAAe,mBAAoB,EAAG;EACrD,0BAA0B,MAAM;CACjC;CAEA,IAAI;CAEJ,IAAI,KAAK,YAAY;CACrB,KAAM,MAAM,iBAAiB,IAAI,KAAK,WAAW,WAChD,IACG,cAAc,cAAc,MAAO,uBAAwB,GAE7D,OAAO,OAAQ,YAAa,EAAI,IAAI;AAGvC;AAEA,MAAM,uBAAwB,eAAgB;CAC7C,IAAK,CAAE,WAAW,mBAAmB,CAAE,WAAW,uBACjD,OAAO;CAER,MAAM,QAAQ,EAAE,OAAO,CAAC,EAAE;CAC1B,IAAK,WAAW,iBACf,MAAM,MAAM,OAAO,WAAW;CAE/B,IAAK,WAAW,uBACf,MAAM,MAAM,aAAa,WAAW;CAGrC,MAAM,EAAE,iBAAiB,uBAAuB,GAAG,mBAClD;CAED,OAAO;EACN,GAAG;EACH;EACA,mBAAmB;CACpB;AACD;AAEA,IAAA,qBAAe;CACd;EACC,YAAY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