Skip to content

Commit

Permalink
Nested blocks: Show the side inserter on empty paragraphs
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Feb 26, 2018
1 parent bb56c0e commit 6e81725
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
10 changes: 8 additions & 2 deletions blocks/api/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
*/
import { every, keys, isEqual } from 'lodash';

/**
* WordPress dependencies
*/
import { applyFilters } from '@wordpress/hooks';

/**
* Internal dependencies
*/
Expand All @@ -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 ] )
Expand Down
15 changes: 14 additions & 1 deletion blocks/hooks/layout.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { assign, compact, get } from 'lodash';
import { assign, compact, get, without } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -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 );
10 changes: 10 additions & 0 deletions blocks/hooks/test/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' ] );
} );
} );
} );

0 comments on commit 6e81725

Please sign in to comment.