From 995520c837654aec61683016ee32ff9b714a6b7f Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Mon, 19 Feb 2018 10:46:08 +0100 Subject: [PATCH] Nested blocks: Show the side inserter on empty paragraphs --- blocks/api/utils.js | 10 ++++++++-- blocks/hooks/layout.js | 15 ++++++++++++++- blocks/hooks/test/layout.js | 10 ++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/blocks/api/utils.js b/blocks/api/utils.js index b3e4a28819b52f..d1900a15ca836b 100644 --- a/blocks/api/utils.js +++ b/blocks/api/utils.js @@ -3,6 +3,11 @@ */ import { every, keys, isEqual } from 'lodash'; +/** + * WordPress dependencies + */ +import { applyFilters } from '@wordpress/hooks'; + /** * Internal dependencies */ @@ -25,10 +30,11 @@ export function isUnmodifiedDefaultBlock( block ) { } const newDefaultBlock = createBlock( defaultBlockName ); - const attributeKeys = [ + + const attributeKeys = applyFilters( 'blocks.isUnmodifiedDefaultBlock.attributes', [ ...keys( newDefaultBlock.attributes ), ...keys( block.attributes ), - ]; + ] ); return every( attributeKeys, key => isEqual( newDefaultBlock.attributes[ key ], block.attributes[ key ] ) diff --git a/blocks/hooks/layout.js b/blocks/hooks/layout.js index a313c6ee35f18f..5a92d73b1269e2 100644 --- a/blocks/hooks/layout.js +++ b/blocks/hooks/layout.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { assign, compact, get } from 'lodash'; +import { assign, compact, get, without } from 'lodash'; /** * WordPress dependencies @@ -70,6 +70,19 @@ function preserveLayoutAttribute( transformedBlock, blocks ) { return transformedBlock; } +/** + * Excludes the layout from the list of attributes to check + * when determining if a block is unmodified or not. + * + * @param {Object} attributeKeys Attribute keys to check + * + * @return {Object} Modified list of attribute keys + */ +function excludeLayoutFromUnmodifiedBlockCheck( attributeKeys ) { + return without( attributeKeys, 'layout' ); +} + addFilter( 'blocks.registerBlockType', 'core/layout/attribute', addAttribute ); addFilter( 'blocks.getSaveContent.extraProps', 'core/layout/save-props', addSaveProps ); addFilter( 'blocks.switchToBlockType.transformedBlock', 'core/layout/preserve-layout', preserveLayoutAttribute ); +addFilter( 'blocks.isUnmodifiedDefaultBlock.attributes', 'core/layout/exclude-layout-attribute-check', excludeLayoutFromUnmodifiedBlockCheck ); diff --git a/blocks/hooks/test/layout.js b/blocks/hooks/test/layout.js index 24ac163077807b..cc5e6a5b0ba49a 100644 --- a/blocks/hooks/test/layout.js +++ b/blocks/hooks/test/layout.js @@ -53,4 +53,14 @@ describe( 'layout', () => { expect( transformedBlock.attributes.layout ).toBe( 'wide' ); } ); } ); + + describe( 'excludeLayoutFromUnmodifiedBlockCheck', () => { + const excludeLayoutAttribute = applyFilters.bind( null, 'blocks.isUnmodifiedDefaultBlock.attributes' ); + + it( 'should exclude the layout attribute', () => { + const attributeKeys = excludeLayoutAttribute( [ 'align', 'content', 'layout' ] ); + + expect( attributeKeys ).toEqual( [ 'align', 'content' ] ); + } ); + } ); } );