Skip to content

Commit

Permalink
Blocks: Add deprecated migration for Columns
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Jun 29, 2018
1 parent 0dda057 commit 41a7288
Showing 1 changed file with 61 additions and 1 deletion.
62 changes: 61 additions & 1 deletion core-blocks/columns/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { times } from 'lodash';
import { times, property, omit } from 'lodash';
import classnames from 'classnames';
import memoize from 'memize';

Expand All @@ -11,6 +11,7 @@ import memoize from 'memize';
import { __, sprintf } from '@wordpress/i18n';
import { PanelBody, RangeControl } from '@wordpress/components';
import { Fragment } from '@wordpress/element';
import { createBlock } from '@wordpress/blocks';
import {
InspectorControls,
InnerBlocks,
Expand Down Expand Up @@ -60,6 +61,65 @@ export const settings = {
align: [ 'wide', 'full' ],
},

deprecated: [
{
attributes: {
columns: {
type: 'number',
default: 2,
},
},
isEligible( attributes, innerBlocks ) {
return innerBlocks.some( property( [ 'attributes', 'layout' ] ) );
},
migrate( attributes, innerBlocks ) {
function withoutLayout( block ) {
return {
...block,
attributes: omit( block.attributes, [ 'layout' ] ),
};
}

const columns = innerBlocks.reduce( ( result, innerBlock ) => {
const { layout } = innerBlock.attributes;

let columnIndex, columnMatch;
if ( layout && ( columnMatch = layout.match( /^column-(\d+)$/ ) ) ) {
columnIndex = Number( columnMatch[ 1 ] ) - 1;
} else {
columnIndex = 0;
}

if ( ! result[ columnIndex ] ) {
result[ columnIndex ] = [];
}

result[ columnIndex ].push( withoutLayout( innerBlock ) );

return result;
}, [] );

const migratedInnerBlocks = columns.map( ( columnBlocks ) => (
createBlock( 'core/column', {}, columnBlocks )
) );

return [
attributes,
migratedInnerBlocks,
];
},
save( { attributes } ) {
const { columns } = attributes;

return (
<div className={ `has-${ columns }-columns` }>
<InnerBlocks.Content />
</div>
);
},
},
],

edit( { attributes, setAttributes, className } ) {
const { columns } = attributes;
const classes = classnames( className, `has-${ columns }-columns` );
Expand Down

0 comments on commit 41a7288

Please sign in to comment.