From db484b3bb212751c49d9275f58027c402de509bf Mon Sep 17 00:00:00 2001 From: Marek Hrabe Date: Wed, 31 Jul 2019 09:14:31 -0700 Subject: [PATCH] Update BlockPreview component to accept multiple Blocks to preview (#16033) * export BlockPreview * Update to accept multiple Blocks * Update reusable Blocks preview to use the new single blocks prop * Remove unecessary clone of Blocks Not sure why this was introduced. * Remove export. This is being handled in another PR See https://github.com/WordPress/gutenberg/pull/16801 * Simplify casting to array via lodash * Utilise cloneBlocks to safely merge attributes on blocks prop * Spread reusable block initial attrs correctly * Fix cloneBlock fn to check for innerBlocks before attempting to map The `innerBlocks` of the `block` being cloned can be `undefined`. Therefore, by attempting to map these we trigger an error. Fixed to introduce existence check before attempting to manipulate innerBlocks. * Simplify modifying passed block attrs via cloneBlock * Removes unecessary spread operation initalAttributes is already an object so no need to spread into an object. * block-preview: ensuring to cast BlockEditorProvider value prop as an Array * don't call cloneBlock on a non-block object * bring back the old behavior of cloneBlock --- .../src/components/block-preview/index.js | 15 +++++++++++---- .../src/components/block-styles/index.js | 13 +++---------- .../src/components/block-switcher/index.js | 11 +++++++---- .../block-editor/src/components/inserter/menu.js | 2 +- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/packages/block-editor/src/components/block-preview/index.js b/packages/block-editor/src/components/block-preview/index.js index cc4c846a1cff3..b4d0850f3b810 100644 --- a/packages/block-editor/src/components/block-preview/index.js +++ b/packages/block-editor/src/components/block-preview/index.js @@ -1,8 +1,12 @@ +/** + * External dependencies + */ +import { castArray } from 'lodash'; + /** * WordPress dependencies */ import { __ } from '@wordpress/i18n'; -import { createBlock } from '@wordpress/blocks'; import { Disabled } from '@wordpress/components'; import { withSelect } from '@wordpress/data'; @@ -28,12 +32,15 @@ function BlockPreview( props ) { ); } -export function BlockPreviewContent( { name, attributes, innerBlocks, settings } ) { - const block = createBlock( name, attributes, innerBlocks ); +export function BlockPreviewContent( { blocks, settings } ) { + if ( ! blocks ) { + return null; + } + return ( diff --git a/packages/block-editor/src/components/block-styles/index.js b/packages/block-editor/src/components/block-styles/index.js index 93c522eb227f5..2485c73e9a0a4 100644 --- a/packages/block-editor/src/components/block-styles/index.js +++ b/packages/block-editor/src/components/block-styles/index.js @@ -12,7 +12,7 @@ import { withSelect, withDispatch } from '@wordpress/data'; import TokenList from '@wordpress/token-list'; import { ENTER, SPACE } from '@wordpress/keycodes'; import { _x } from '@wordpress/i18n'; -import { getBlockType } from '@wordpress/blocks'; +import { getBlockType, cloneBlock } from '@wordpress/blocks'; /** * Internal dependencies @@ -68,8 +68,6 @@ function BlockStyles( { styles, className, onChangeClassName, - name, - attributes, type, block, onSwitch = noop, @@ -125,12 +123,9 @@ function BlockStyles( { >
@@ -152,8 +147,6 @@ export default compose( [ return { block, - name: block.name, - attributes: block.attributes, className: block.attributes.className || '', styles: getBlockStyles( block.name ), type: blockType, diff --git a/packages/block-editor/src/components/block-switcher/index.js b/packages/block-editor/src/components/block-switcher/index.js index eae64277471b2..4101092db6d4d 100644 --- a/packages/block-editor/src/components/block-switcher/index.js +++ b/packages/block-editor/src/components/block-switcher/index.js @@ -8,7 +8,7 @@ import { castArray, filter, first, mapKeys, orderBy, uniq, map } from 'lodash'; */ import { __, _n, sprintf } from '@wordpress/i18n'; import { Dropdown, IconButton, Toolbar, PanelBody, Path, SVG } from '@wordpress/components'; -import { getBlockType, getPossibleBlockTransformations, switchToBlockType, hasChildBlocksWithInserterSupport } from '@wordpress/blocks'; +import { getBlockType, getPossibleBlockTransformations, switchToBlockType, hasChildBlocksWithInserterSupport, cloneBlock } from '@wordpress/blocks'; import { Component } from '@wordpress/element'; import { DOWN } from '@wordpress/keycodes'; import { withSelect, withDispatch } from '@wordpress/data'; @@ -161,9 +161,12 @@ export class BlockSwitcher extends Component { { ( hoveredClassName !== null ) && } diff --git a/packages/block-editor/src/components/inserter/menu.js b/packages/block-editor/src/components/inserter/menu.js index bf659f789d188..12d119ce4ab51 100644 --- a/packages/block-editor/src/components/inserter/menu.js +++ b/packages/block-editor/src/components/inserter/menu.js @@ -351,7 +351,7 @@ export class InserterMenu extends Component {
{ hoveredItem && isReusableBlock( hoveredItem ) && - + } );