Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transform multiple selected blocks to Columns block #25829

Merged
merged 5 commits into from
Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions packages/block-editor/src/autocompleters/block.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
/**
* External dependencies
*/
import { noop, map, orderBy } from 'lodash';
import { noop, orderBy } from 'lodash';

/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { createBlock } from '@wordpress/blocks';
import {
createBlock,
createBlocksFromInnerBlocksTemplate,
} from '@wordpress/blocks';
import { useMemo } from '@wordpress/element';

/**
Expand All @@ -19,18 +22,6 @@ import BlockIcon from '../components/block-icon';

const SHOWN_BLOCK_TYPES = 9;

const createBlocksFromInnerBlocksTemplate = ( innerBlocksTemplate ) => {
return map(
innerBlocksTemplate,
( [ name, attributes, innerBlocks = [] ] ) =>
createBlock(
name,
attributes,
createBlocksFromInnerBlocksTemplate( innerBlocks )
)
);
};

/** @typedef {import('@wordpress/block-editor').WPEditorInserterItem} WPEditorInserterItem */

/** @typedef {import('@wordpress/components').WPCompleter} WPCompleter */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import {
TouchableWithoutFeedback,
Platform,
} from 'react-native';
import { map } from 'lodash';

/**
* WordPress dependencies
*/
import { withSelect, useDispatch } from '@wordpress/data';
import { compose, usePreferredColorSchemeStyle } from '@wordpress/compose';
import { createBlock } from '@wordpress/blocks';
import { createBlocksFromInnerBlocksTemplate } from '@wordpress/blocks';
import { __ } from '@wordpress/i18n';
import {
PanelBody,
Expand All @@ -33,18 +32,6 @@ import styles from './style.scss';

const hitSlop = { top: 22, bottom: 22, left: 22, right: 22 };

function createBlocksFromInnerBlocksTemplate( innerBlocksTemplate ) {
return map(
innerBlocksTemplate,
( [ name, attributes, innerBlocks = [] ] ) =>
createBlock(
name,
attributes,
createBlocksFromInnerBlocksTemplate( innerBlocks )
)
);
}

function BlockVariationPicker( { isVisible, onClose, clientId, variations } ) {
const { replaceInnerBlocks } = useDispatch( 'core/block-editor' );
const isIOS = Platform.OS === 'ios';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
/**
* External dependencies
*/
import { map } from 'lodash';

/**
* WordPress dependencies
*/
import { useEffect } from '@wordpress/element';
import { createBlock } from '@wordpress/blocks';
import {
createBlock,
createBlocksFromInnerBlocksTemplate,
} from '@wordpress/blocks';
import { useSelect } from '@wordpress/data';

// Copied over from the Columns block. It seems like it should become part of public API.
const createBlocksFromInnerBlocksTemplate = ( innerBlocksTemplate ) => {
return map(
innerBlocksTemplate,
( [ name, attributes, innerBlocks = [] ] ) =>
createBlock(
name,
attributes,
createBlocksFromInnerBlocksTemplate( innerBlocks )
)
);
};

/**
* Retrieves the block types inserter state.
*
Expand Down
19 changes: 5 additions & 14 deletions packages/block-library/src/columns/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import classnames from 'classnames';
import { dropRight, get, map, times } from 'lodash';
import { dropRight, get, times } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -19,7 +19,10 @@ import {
useBlockProps,
} from '@wordpress/block-editor';
import { withDispatch, useDispatch, useSelect } from '@wordpress/data';
import { createBlock } from '@wordpress/blocks';
import {
createBlock,
createBlocksFromInnerBlocksTemplate,
} from '@wordpress/blocks';

/**
* Internal dependencies
Expand Down Expand Up @@ -198,18 +201,6 @@ const ColumnsEditContainerWrapper = withDispatch(
} )
)( ColumnsEditContainer );

const createBlocksFromInnerBlocksTemplate = ( innerBlocksTemplate ) => {
return map(
innerBlocksTemplate,
( [ name, attributes, innerBlocks = [] ] ) =>
createBlock(
name,
attributes,
createBlocksFromInnerBlocksTemplate( innerBlocks )
)
);
};

function Placeholder( { clientId, name, setAttributes } ) {
const { blockType, defaultVariation, variations } = useSelect(
( select ) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/columns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import edit from './edit';
import metadata from './block.json';
import save from './save';
import variations from './variations';
import transforms from './transforms';

const { name } = metadata;

Expand Down Expand Up @@ -84,4 +85,5 @@ export const settings = {
deprecated,
edit,
save,
transforms,
};
39 changes: 39 additions & 0 deletions packages/block-library/src/columns/transforms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* WordPress dependencies
*/
import {
createBlock,
createBlocksFromInnerBlocksTemplate,
} from '@wordpress/blocks';

const MAXIMUM_SELECTED_BLOCKS = 6;

const transforms = {
from: [
{
type: 'block',
isMultiBlock: true,
blocks: [ '*' ],
__experimentalConvert: ( blocks ) => {
const columnWidth = +( 100 / blocks.length ).toFixed( 2 );
const innerBlocksTemplate = blocks.map(
( { name, attributes, innerBlocks } ) => [
'core/column',
{ width: `${ columnWidth }%` },
[ [ name, { ...attributes }, innerBlocks ] ],
]
);
return createBlock(
'core/columns',
{},
createBlocksFromInnerBlocksTemplate( innerBlocksTemplate )
);
},
isMatch: ( { length: selectedBlocksLength } ) =>
selectedBlocksLength > 1 &&
selectedBlocksLength <= MAXIMUM_SELECTED_BLOCKS,
},
],
};

export default transforms;
15 changes: 15 additions & 0 deletions packages/blocks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,21 @@ _Returns_

- `Object`: Block object.

<a name="createBlocksFromInnerBlocksTemplate" href="#createBlocksFromInnerBlocksTemplate">#</a> **createBlocksFromInnerBlocksTemplate**

Given an array of InnerBlocks templates or Block Objects,
returns an array of created Blocks from them.
It handles the case of having InnerBlocks as Blocks by
converting them to the proper format to continue recursively.

_Parameters_

- _innerBlocksOrTemplate_ `Array`: Nested blocks or InnerBlocks templates.

_Returns_

- `Array<Object>`: Array of Block objects.

<a name="doBlocksMatchTemplate" href="#doBlocksMatchTemplate">#</a> **doBlocksMatchTemplate**

Checks whether a list of blocks matches a template by comparing the block names.
Expand Down
30 changes: 30 additions & 0 deletions packages/blocks/src/api/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,36 @@ export function createBlock( name, attributes = {}, innerBlocks = [] ) {
};
}

/**
* Given an array of InnerBlocks templates or Block Objects,
* returns an array of created Blocks from them.
* It handles the case of having InnerBlocks as Blocks by
* converting them to the proper format to continue recursively.
*
* @param {Array} innerBlocksOrTemplate Nested blocks or InnerBlocks templates.
*
* @return {Object[]} Array of Block objects.
*/
export function createBlocksFromInnerBlocksTemplate(
innerBlocksOrTemplate = []
) {
return innerBlocksOrTemplate.map( ( innerBlock ) => {
const innerBlockTemplate = Array.isArray( innerBlock )
? innerBlock
: [
innerBlock.name,
innerBlock.attributes,
innerBlock.innerBlocks,
];
const [ name, attributes, innerBlocks = [] ] = innerBlockTemplate;
return createBlock(
name,
attributes,
createBlocksFromInnerBlocksTemplate( innerBlocks )
);
} );
}

/**
* Given a block object, returns a copy of the block object, optionally merging
* new attributes and/or replacing its inner blocks.
Expand Down
1 change: 1 addition & 0 deletions packages/blocks/src/api/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export {
createBlock,
createBlocksFromInnerBlocksTemplate,
cloneBlock,
getPossibleBlockTransformations,
switchToBlockType,
Expand Down
103 changes: 103 additions & 0 deletions packages/blocks/src/api/test/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { noop, times } from 'lodash';
*/
import {
createBlock,
createBlocksFromInnerBlocksTemplate,
cloneBlock,
getPossibleBlockTransformations,
switchToBlockType,
Expand Down Expand Up @@ -160,6 +161,108 @@ describe( 'block factory', () => {
} );
} );

describe( 'createBlocksFromInnerBlocksTemplate', () => {
it( 'should create a block without InnerBlocks', () => {
const blockName = 'core/test-block';
registerBlockType( blockName, { ...defaultBlockSettings } );
const res = createBlock(
blockName,
{ ...defaultBlockSettings },
createBlocksFromInnerBlocksTemplate()
);
expect( res ).toEqual(
expect.objectContaining( {
name: blockName,
innerBlocks: [],
} )
);
} );
describe( 'create block with InnerBlocks', () => {
beforeEach( () => {
registerBlockType( 'core/test-block', {
...defaultBlockSettings,
} );
registerBlockType( 'core/test-other', {
...defaultBlockSettings,
} );
registerBlockType( 'core/test-paragraph', {
...defaultBlockSettings,
attributes: {
content: {
type: 'string',
default: 'hello',
},
},
} );
} );
it( 'should create block with InnerBlocks from template', () => {
const res = createBlock(
'core/test-block',
defaultBlockSettings,
createBlocksFromInnerBlocksTemplate( [
[ 'core/test-other' ],
[ 'core/test-paragraph', { content: 'fromTemplate' } ],
[ 'core/test-paragraph' ],
] )
);
expect( res.innerBlocks ).toHaveLength( 3 );
expect( res.innerBlocks ).toEqual(
expect.arrayContaining( [
expect.objectContaining( {
name: 'core/test-other',
} ),
expect.objectContaining( {
name: 'core/test-paragraph',
attributes: { content: 'fromTemplate' },
} ),
expect.objectContaining( {
name: 'core/test-paragraph',
attributes: { content: 'hello' },
} ),
] )
);
} );
it( 'should create blocks with InnerBlocks template and InnerBlock objects', () => {
const nestedInnerBlocks = [
createBlock( 'core/test-other' ),
createBlock( 'core/test-paragraph' ),
];
const res = createBlock(
'core/test-block',
defaultBlockSettings,
createBlocksFromInnerBlocksTemplate( [
[ 'core/test-other' ],
[
'core/test-paragraph',
{ content: 'fromTemplate' },
nestedInnerBlocks,
],
] )
);
expect( res.innerBlocks ).toHaveLength( 2 );
expect( res.innerBlocks ).toEqual(
expect.arrayContaining( [
expect.objectContaining( {
name: 'core/test-other',
} ),
expect.objectContaining( {
name: 'core/test-paragraph',
attributes: { content: 'fromTemplate' },
innerBlocks: expect.arrayContaining( [
expect.objectContaining( {
name: 'core/test-other',
} ),
expect.objectContaining( {
name: 'core/test-other',
} ),
] ),
} ),
] )
);
} );
} );
} );

describe( 'cloneBlock()', () => {
it( 'should merge attributes into the existing block', () => {
registerBlockType( 'core/test-block', {
Expand Down
Loading