Repository navigation
Stop keeping stale controlled blocks after reset #76591
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -601,26 +601,23 @@ const withBlockReset = ( reducer ) => ( state, action ) => { | |
| if ( action.type === 'RESET_BLOCKS' ) { | ||
| /** | ||
| * Preserve controlled inner block flags across RESET_BLOCKS. | ||
| * Previously this was cleared to `{}`, which caused nested | ||
| * controllers (e.g. post-content, patterns) to lose their | ||
| * controlled status and unnecessarily re-clone blocks. Stale | ||
| * flags are cleaned up naturally by unsetControlledBlocks() | ||
| * when useBlockSync unmounts. | ||
| * If there are old and new blocks that: | ||
| * - have the same `clientId` | ||
| * - have the `controlledInnerBlocks` flag | ||
| * - don't have any own, uncontrolled children | ||
| * then we preserve the `controlledInnerBlocks` flag and the controlled sub-trees. | ||
| * Nested controllers (e.g., `post-content`, patterns) don't lose their | ||
| * controlled status and don't unnecessarily re-clone blocks. | ||
| */ | ||
| const newState = reducer( undefined, { | ||
| type: 'INSERT_BLOCKS', | ||
| rootClientId: '', | ||
| blocks: action.blocks, | ||
| } ); | ||
|
|
||
| const preservedControlledInnerBlocks = | ||
| state?.controlledInnerBlocks ?? {}; | ||
|
|
||
| const newState = { | ||
| ...state, | ||
| byClientId: new Map( | ||
| getFlattenedBlocksWithoutAttributes( action.blocks ) | ||
| ), | ||
| attributes: new Map( getFlattenedBlockAttributes( action.blocks ) ), | ||
| order: mapBlockOrder( action.blocks ), | ||
| parents: new Map( mapBlockParents( action.blocks ) ), | ||
| controlledInnerBlocks: preservedControlledInnerBlocks, | ||
| }; | ||
|
|
||
| // Preserve controlled inner blocks data from the old state. | ||
| // The maps above are rebuilt solely from action.blocks, but | ||
| // controlled inner blocks live under cloned IDs that aren't | ||
|
|
@@ -637,23 +634,24 @@ const withBlockReset = ( reducer ) => ( state, action ) => { | |
| if ( ! newState.byClientId.has( clientId ) ) { | ||
| continue; | ||
| } | ||
| newState.controlledInnerBlocks[ clientId ] = true; | ||
| const oldOrder = state.order.get( clientId ); | ||
| if ( ! oldOrder?.length ) { | ||
| continue; | ||
| } | ||
| newState.order.set( clientId, oldOrder ); | ||
| const preserveBlock = ( blockId, parentId ) => { | ||
| const blockData = state.byClientId?.get( blockId ); | ||
| const blockData = state.byClientId.get( blockId ); | ||
| if ( ! blockData ) { | ||
| return; | ||
| } | ||
| newState.byClientId.set( blockId, blockData ); | ||
| newState.attributes.set( | ||
| blockId, | ||
| state.attributes?.get( blockId ) | ||
| state.attributes.get( blockId ) | ||
| ); | ||
| newState.parents.set( blockId, parentId ); | ||
| const childOrder = state.order?.get( blockId ) || []; | ||
| const childOrder = state.order.get( blockId ) || []; | ||
| newState.order.set( blockId, childOrder ); | ||
| childOrder.forEach( ( childId ) => | ||
| preserveBlock( childId, blockId ) | ||
|
|
@@ -663,43 +661,38 @@ const withBlockReset = ( reducer ) => ( state, action ) => { | |
| } | ||
| } | ||
|
|
||
| newState.tree = new Map( state?.tree ); | ||
| updateBlockTreeForBlocks( newState, action.blocks ); | ||
|
|
||
| // Fix tree entries for controlled blocks. updateBlockTreeForBlocks | ||
| // built tree entries using action.blocks' inner block structure | ||
| // (entity-level IDs), but we need them to reference the preserved | ||
| // cloned inner blocks instead. Mutating the existing object | ||
| // preserves references held by ancestor tree entries. | ||
| for ( const clientId of Object.keys( | ||
| preservedControlledInnerBlocks | ||
| newState.controlledInnerBlocks | ||
| ) ) { | ||
| if ( ! preservedControlledInnerBlocks[ clientId ] ) { | ||
| continue; | ||
| } | ||
| if ( ! newState.byClientId.has( clientId ) ) { | ||
| continue; | ||
| } | ||
| const controlledOrder = newState.order.get( clientId ); | ||
| if ( ! controlledOrder?.length ) { | ||
| continue; | ||
| } | ||
| const innerBlocks = controlledOrder.map( ( id ) => | ||
| newState.tree.get( id ) | ||
| state.tree.get( id ) | ||
| ); | ||
| const existingEntry = newState.tree.get( clientId ); | ||
| if ( existingEntry ) { | ||
| existingEntry.innerBlocks = innerBlocks; | ||
| } | ||
| newState.tree.set( 'controlled||' + clientId, { innerBlocks } ); | ||
| const preserveTreeEntry = ( blockId ) => { | ||
| const treeEntry = state.tree.get( blockId ); | ||
| if ( ! treeEntry ) { | ||
| return; | ||
| } | ||
| newState.tree.set( blockId, treeEntry ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are references to these
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we're copying entries in the controlled subtrees that are guaranteed to be not touched. We can copy them. All the Other reducers also do this. Typically they copy all old entries to a new map with |
||
| const childOrder = newState.order.get( blockId ) || []; | ||
| childOrder.forEach( preserveTreeEntry ); | ||
| }; | ||
| controlledOrder.forEach( preserveTreeEntry ); | ||
| } | ||
|
|
||
| newState.tree.set( '', { | ||
| innerBlocks: action.blocks.map( ( subBlock ) => | ||
| newState.tree.get( subBlock.clientId ) | ||
| ), | ||
| } ); | ||
|
|
||
| return newState; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we still keep this just in case? It's totally fine to rebuild the state from scratch, but I just want to be sure we're not missing something
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To ensure we're not forgetting some piece of state, not even in the future, I switched to another approach. Let the inner reducer create the new state, by initializing an empty state with
INSERT_BLOCKS:That's exactly what the previous code was doing. Everything is much simpler now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really like how this simplified the logic. Great refactoring, @jsnajdr!