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

Block Editor: Partition attributes updates to avoid conflating meta and blocks attributes #15781

Merged
merged 1 commit into from
May 27, 2019
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
40 changes: 28 additions & 12 deletions packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -644,18 +644,34 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps, { select } ) => {
setAttributes( newAttributes ) {
const { name, clientId } = ownProps;
const type = getBlockType( name );
updateBlockAttributes( clientId, newAttributes );
const metaAttributes = reduce(
newAttributes,
( result, value, key ) => {
if ( get( type, [ 'attributes', key, 'source' ] ) === 'meta' ) {
result[ type.attributes[ key ].meta ] = value;
}

return result;
},
{}
);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The behavior of all this setAttributes function seems like something that should be absorbed in the updateBlockAttributes action instead.

As you said, we should ship the bug fix and consider these consolidations separately.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The behavior of all this setAttributes function seems like something that should be absorbed in the updateBlockAttributes action instead.

As you said, we should ship the bug fix and consider these consolidations separately.

Yes, I think so too. One point of clarification / affirmation is that: I don't think the changes here dig us any further into a hole, and in-fact serve more as an implementation which ought to have existed in the first place.

function isMetaAttribute( key ) {
return get( type, [ 'attributes', key, 'source' ] ) === 'meta';
}

// 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 ) ) {
updateBlockAttributes( clientId, blockAttributes );
}

if ( size( metaAttributes ) ) {
const { getSettings } = select( 'core/block-editor' );
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