Skip to content

Commit ae4e4b0

Browse files
committed
Add isExternal flag to RESET_BLOCKS and ignore panel state changes when a change is RTC-initiated
1 parent f71f971 commit ae4e4b0

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

docs/reference-guides/data/data-core-block-editor.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,6 +1587,8 @@ Action that resets blocks state to the specified array of blocks, taking precede
15871587
_Parameters_
15881588

15891589
- _blocks_ `Array`: Array of blocks.
1590+
- _options_ `Object`: Optional options.
1591+
- _options.isExternal_ `boolean`: Whether this reset is from an external action (e.g. real-time collaboration).
15901592

15911593
### resetSelection
15921594

packages/block-editor/src/components/provider/use-block-sync.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,12 @@ export default function useBlockSync( {
270270
if ( subscribedRef.current ) {
271271
pendingChangesRef.current.incoming = controlledBlocks;
272272
}
273+
// This reset is always from an external data source (real-time
274+
// collaboration, undo/redo) pushing blocks into the editor,
275+
// so mark it isExternal. The current user's edits are filtered out
276+
// by the isOutgoing check above.
273277
__unstableMarkNextChangeAsNotPersistent();
274-
resetBlocks( controlledBlocks );
278+
resetBlocks( controlledBlocks, { isExternal: true } );
275279
}
276280
};
277281

packages/block-editor/src/store/actions.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,15 @@ const castArray = ( maybeArray ) =>
4646
* Action that resets blocks state to the specified array of blocks, taking precedence
4747
* over any other content reflected as an edit in state.
4848
*
49-
* @param {Array} blocks Array of blocks.
49+
* @param {Array} blocks Array of blocks.
50+
* @param {Object} options Optional options.
51+
* @param {boolean} options.isExternal Whether this reset is from an external
52+
* action (e.g. real-time collaboration).
5053
*/
5154
export const resetBlocks =
52-
( blocks ) =>
55+
( blocks, { isExternal = false } = {} ) =>
5356
( { dispatch } ) => {
54-
dispatch( { type: 'RESET_BLOCKS', blocks } );
57+
dispatch( { type: 'RESET_BLOCKS', blocks, isExternal } );
5558
dispatch( validateBlocksToTemplate( blocks ) );
5659
};
5760

packages/block-editor/src/store/reducer.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2254,7 +2254,7 @@ export function insertionPoint( state = null, action ) {
22542254
*
22552255
* @return {Object} Updated state.
22562256
*/
2257-
function openedListViewPanels(
2257+
export function openedListViewPanels(
22582258
state = { allOpen: false, panels: {} },
22592259
action
22602260
) {
@@ -2293,9 +2293,15 @@ function openedListViewPanels(
22932293
} );
22942294
return hasChanges ? { ...state, panels: newPanels } : state;
22952295
}
2296-
case 'RESET_BLOCKS':
2297-
// Clear all panel state when blocks are reset
2296+
case 'RESET_BLOCKS': {
2297+
// Ignore external resets (like from real-time collaboration) and
2298+
// keep the user's sidebar from collapsing due to remote changes.
2299+
if ( action.isExternal ) {
2300+
return state;
2301+
}
2302+
22982303
return { allOpen: false, panels: {} };
2304+
}
22992305
}
23002306
return state;
23012307
}

0 commit comments

Comments
 (0)