Skip to content

Commit

Permalink
Revert "useBlockSync: remove isControlled effect" (#61480)
Browse files Browse the repository at this point in the history
Co-authored-by: ellatrix <ellatrix@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
  • Loading branch information
3 people authored May 8, 2024
1 parent 966f0a1 commit 2c2f899
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
28 changes: 27 additions & 1 deletion packages/block-editor/src/components/provider/use-block-sync.js
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 } from '@wordpress/data';
import { useRegistry, useSelect } from '@wordpress/data';
import { cloneBlock } from '@wordpress/blocks';

/**
Expand Down Expand Up @@ -82,6 +82,15 @@ 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 @@ -177,6 +186,23 @@ 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,
Expand Down
42 changes: 42 additions & 0 deletions test/e2e/specs/site-editor/undo.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* WordPress dependencies
*/
const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' );

test.describe( 'undo', () => {
test.beforeAll( async ( { requestUtils } ) => {
await requestUtils.activateTheme( 'emptytheme' );
} );

test.afterAll( async ( { requestUtils } ) => {
await requestUtils.activateTheme( 'twentytwentyone' );
} );

test( 'does not empty header', async ( { admin, page, editor } ) => {
await admin.visitSiteEditor( { canvas: 'edit' } );

// Check if there's a valid child block with a type (not appender).
await expect(
editor.canvas.locator(
'[data-type="core/template-part"] [data-type]'
)
).not.toHaveCount( 0 );

// insert a block
await editor.insertBlock( { name: 'core/paragraph' } );

// undo
await page
.getByRole( 'button', {
name: 'Undo',
} )
.click();

// Check if there's a valid child block with a type (not appender).
await expect(
editor.canvas.locator(
'[data-type="core/template-part"] [data-type]'
)
).not.toHaveCount( 0 );
} );
} );

0 comments on commit 2c2f899

Please sign in to comment.