Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress dependencies
*/
import { useEffect, useRef } from '@wordpress/element';
import { useRegistry, useSelect } from '@wordpress/data';
import { useRegistry } from '@wordpress/data';
import { cloneBlock } from '@wordpress/blocks';

/**
Expand Down Expand Up @@ -82,15 +82,6 @@ export default function useBlockSync( {
} = registry.dispatch( blockEditorStore );
const { getBlockName, getBlocks, getSelectionStart, getSelectionEnd } =
registry.select( blockEditorStore );
const isControlled = useSelect(
( select ) => {
return (
! clientId ||
select( blockEditorStore ).areInnerBlocksControlled( clientId )
);
},
[ clientId ]
);

const pendingChanges = useRef( { incoming: null, outgoing: [] } );
const subscribed = useRef( false );
Expand Down Expand Up @@ -186,29 +177,11 @@ export default function useBlockSync( {
}
}, [ controlledBlocks, clientId ] );

const isMounted = useRef( false );

useEffect( () => {
// On mount, controlled blocks are already set in the effect above.
if ( ! isMounted.current ) {
isMounted.current = true;
return;
}

// When the block becomes uncontrolled, it means its inner state has been reset
// we need to take the blocks again from the external value property.
if ( ! isControlled ) {
pendingChanges.current.outgoing = [];
setControlledBlocks();
}
}, [ isControlled ] );

useEffect( () => {
const {
getSelectedBlocksInitialCaretPosition,
isLastBlockChangePersistent,
__unstableIsLastBlockChangeIgnored,
areInnerBlocksControlled,
} = registry.select( blockEditorStore );

let blocks = getBlocks( clientId );
Expand All @@ -228,16 +201,6 @@ export default function useBlockSync( {
return;
}

// When RESET_BLOCKS on parent blocks get called, the controlled blocks
// can reset to uncontrolled, in these situations, it means we need to populate
// the blocks again from the external blocks (the value property here)
// and we should stop triggering onChange
const isStillControlled =
! clientId || areInnerBlocksControlled( clientId );
if ( ! isStillControlled ) {
return;
}

const newIsPersistent = isLastBlockChangePersistent();
const newBlocks = getBlocks( clientId );
const areBlocksDifferent = newBlocks !== blocks;
Expand Down
33 changes: 24 additions & 9 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -592,16 +592,31 @@ const withBlockReset = ( reducer ) => ( state, action ) => {
if ( action.type === 'RESET_BLOCKS' ) {
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: {},
byClientId: new Map( state?.byClientId ),
attributes: new Map( state?.attributes ),
order: new Map( state?.order ),
parents: new Map( state?.parents ),
tree: new Map( state?.tree ),
controlledInnerBlocks: state?.controlledInnerBlocks ?? {},
};

newState.tree = new Map( state?.tree );
for ( const [ key, value ] of getFlattenedBlocksWithoutAttributes(
action.blocks
) ) {
newState.byClientId.set( key, value );
}
for ( const [ key, value ] of getFlattenedBlockAttributes(
action.blocks
) ) {
newState.attributes.set( key, value );
}
for ( const [ key, value ] of mapBlockOrder( action.blocks ) ) {
if ( ! newState.controlledInnerBlocks[ key ] ) {
newState.order.set( key, value );
}
}
for ( const [ key, value ] of mapBlockParents( action.blocks ) ) {
newState.parents.set( key, value );
}
updateBlockTreeForBlocks( newState, action.blocks );
newState.tree.set( '', {
innerBlocks: action.blocks.map( ( subBlock ) =>
Expand Down