Skip to content

Commit

Permalink
Move setAttributes handler to the withDispatch HoC
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Apr 16, 2019
1 parent 651ae89 commit 79f7a6e
Showing 1 changed file with 23 additions and 28 deletions.
51 changes: 23 additions & 28 deletions packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,8 @@ function BlockListBlock( {
attributes,
initialPosition,
wrapperProps,
onMetaChange,
setAttributes,
onReplace,
onChange,
onInsertBlocksAfter,
onMerge,
onSelect,
Expand Down Expand Up @@ -254,25 +253,6 @@ function BlockListBlock( {
}, [ isFirstMultiSelected ] );

// Other event handlers -----------------------------------------------------
const setAttributes = ( newAttributes ) => {
const type = getBlockType( name );
onChange( clientId, newAttributes );
const metaAttributes = reduce(
newAttributes,
( result, value, key ) => {
if ( get( type, [ 'attributes', key, 'source' ] ) === 'meta' ) {
result[ type.attributes[ key ].meta ] = value;
}

return result;
},
{}
);

if ( size( metaAttributes ) ) {
onMetaChange( metaAttributes );
}
};

/**
* Marks the block as selected when focused and not already selected. This
Expand Down Expand Up @@ -675,11 +655,31 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps, { select } ) => {
mergeBlocks,
replaceBlocks,
toggleSelection,

} = dispatch( 'core/block-editor' );

return {
onChange( clientId, attributes ) {
updateBlockAttributes( clientId, attributes );
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;
},
{}
);

if ( size( metaAttributes ) ) {
const { getSettings } = select( 'core/block-editor' );
const onChangeMeta = getSettings().__experimentalMetaSource.onChange;
onChangeMeta( metaAttributes );
}
},
onSelect( clientId = ownProps.clientId, initialPosition ) {
selectBlock( clientId, initialPosition );
Expand Down Expand Up @@ -729,11 +729,6 @@ const applyWithDispatch = withDispatch( ( dispatch, ownProps, { select } ) => {
onReplace( blocks ) {
replaceBlocks( [ ownProps.clientId ], blocks );
},
onMetaChange( updatedMeta ) {
const { getSettings } = select( 'core/block-editor' );
const onChangeMeta = getSettings().__experimentalMetaSource.onChange;
onChangeMeta( updatedMeta );
},
onShiftSelection() {
if ( ! ownProps.isSelectionEnabled ) {
return;
Expand Down

0 comments on commit 79f7a6e

Please sign in to comment.