Skip to content

Commit

Permalink
Block Editor: Partition attributes updates to avoid conflating meta a…
Browse files Browse the repository at this point in the history
…nd blocks attributes (#15781)
  • Loading branch information
aduth authored and youknowriad committed May 29, 2019
1 parent 88e464f commit 55515dd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
39 changes: 27 additions & 12 deletions packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,22 +163,37 @@ export class BlockListBlock extends Component {
}
}

setAttributes( attributes ) {
setAttributes( newAttributes ) {
const { clientId, name, onChange } = this.props;
const type = getBlockType( name );
onChange( clientId, attributes );

const metaAttributes = reduce(
attributes,
( result, value, key ) => {
if ( get( type, [ 'attributes', key, 'source' ] ) === 'meta' ) {
result[ type.attributes[ key ].meta ] = value;
}
function isMetaAttribute( key ) {
return get( type, [ 'attributes', key, 'source' ] ) === 'meta';
}

return result;
},
{}
);
// Partition new attributes to delegate update behavior by source.
//
// TODO: A consolidated approach to external attributes sourcing
// should be devised to avoid specific handling for meta, enable
// additional attributes sources.
//
// See: https://github.com/WordPress/gutenberg/issues/2759
const {
blockAttributes,
metaAttributes,
} = reduce( newAttributes, ( result, value, key ) => {
if ( isMetaAttribute( key ) ) {
result.metaAttributes[ type.attributes[ key ].meta ] = value;
} else {
result.blockAttributes[ key ] = value;
}

return result;
}, { blockAttributes: {}, metaAttributes: {} } );

if ( size( blockAttributes ) ) {
onChange( clientId, blockAttributes );
}

if ( size( metaAttributes ) ) {
this.props.onMetaChange( metaAttributes );
Expand Down
12 changes: 11 additions & 1 deletion packages/e2e-tests/specs/plugins/meta-attribute-block.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getEditedPostContent,
insertBlock,
saveDraft,
pressKeyTimes,
} from '@wordpress/e2e-test-utils';

describe( 'Block with a meta attribute', () => {
Expand All @@ -25,7 +26,16 @@ describe( 'Block with a meta attribute', () => {

it( 'Should persist the meta attribute properly', async () => {
await insertBlock( 'Test Meta Attribute Block' );
await page.keyboard.type( 'Meta Value' );
await page.keyboard.type( 'Value' );

// Regression Test: Previously the caret would wrongly reset to the end
// of any input for meta-sourced attributes, due to syncing behavior of
// meta attribute updates.
//
// See: https://github.com/WordPress/gutenberg/issues/15739
await pressKeyTimes( 'ArrowLeft', 5 );
await page.keyboard.type( 'Meta ' );

await saveDraft();
await page.reload();

Expand Down

0 comments on commit 55515dd

Please sign in to comment.