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]: Fix content loss from replaceInnerBlocks with controlled blocks #41948

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
20 changes: 17 additions & 3 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,9 +689,9 @@ const withReplaceInnerBlocks = ( reducer ) => ( state, action ) => {
index: 0,
} );

// We need to re-attach the block order of the controlled inner blocks.
// Otherwise, an inner block controller's blocks will be deleted entirely
// from its entity..
// We need to re-attach the controlled inner blocks to the blocks tree and
// preserve their block order. Otherwise, an inner block controller's blocks
// will be deleted entirely from its entity.
stateAfterInsert.order = {
...stateAfterInsert.order,
...reduce(
Expand All @@ -705,6 +705,20 @@ const withReplaceInnerBlocks = ( reducer ) => ( state, action ) => {
{}
),
};
stateAfterInsert.tree = {
...stateAfterInsert.tree,
...reduce(
nestedControllers,
( result, value, _key ) => {
const key = `controlled||${ _key }`;
if ( state.tree[ key ] ) {
result[ key ] = state.tree[ key ];
}
return result;
},
{}
),
};
}
return stateAfterInsert;
};
Expand Down
124 changes: 124 additions & 0 deletions packages/block-editor/src/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2094,6 +2094,130 @@ describe( 'state', () => {
expect( state.tree.child ).toBeUndefined();
expect( state.tree.chicken.innerBlocks ).toEqual( [] );
} );
it( 'should preserve the controlled blocks in state and re-attach them in other pieces of state(order, tree, etc..), when we replace inner blocks', () => {
const initialState = {
byClientId: {
'group-id': {
clientId: 'group-id',
name: 'core/group',
isValid: true,
},
'reusable-id': {
clientId: 'reusable-id',
name: 'core/block',
isValid: true,
},
'paragraph-id': {
clientId: 'paragraph-id',
name: 'core/paragraph',
isValid: true,
},
},
order: {
'': [ 'group-id' ],
'group-id': [ 'reusable-id' ],
'reusable-id': [ 'paragraph-id' ],
'paragraph-id': [],
},
controlledInnerBlocks: {
'reusable-id': true,
},
parents: {
'group-id': '',
'reusable-id': 'group-id',
'paragraph-id': 'reusable-id',
},
tree: {
'group-id': {
clientId: 'group-id',
name: 'core/group',
isValid: true,
innerBlocks: [
{
clientId: 'reusable-id',
name: 'core/block',
isValid: true,
attributes: {
ref: 687,
},
innerBlocks: [],
},
],
},
'reusable-id': {
clientId: 'reusable-id',
name: 'core/block',
isValid: true,
attributes: {
ref: 687,
},
innerBlocks: [],
},
'': {
innerBlocks: [
{
clientId: 'group-id',
name: 'core/group',
isValid: true,
innerBlocks: [
{
clientId: 'reusable-id',
name: 'core/block',
isValid: true,
attributes: {
ref: 687,
},
innerBlocks: [],
},
],
},
],
},
'paragraph-id': {
clientId: 'paragraph-id',
name: 'core/paragraph',
isValid: true,
innerBlocks: [],
},
'controlled||reusable-id': {
innerBlocks: [
{
clientId: 'paragraph-id',
name: 'core/paragraph',
isValid: true,
innerBlocks: [],
},
],
},
},
};
// We will dispatch an action that replaces the inner
// blocks with the same inner blocks, which contain
// a controlled block (`reusable-id`).
const action = {
type: 'REPLACE_INNER_BLOCKS',
rootClientId: 'group-id',
blocks: [
{
clientId: 'reusable-id',
name: 'core/block',
isValid: true,
attributes: {
ref: 687,
},
innerBlocks: [],
},
],
updateSelection: false,
};
const state = blocks( initialState, action );
expect( state.order ).toEqual(
expect.objectContaining( initialState.order )
);
expect( state.tree ).toEqual(
expect.objectContaining( initialState.tree )
);
} );
} );
} );
} );
Expand Down