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

Revert "useBlockSync: remove isControlled effect" #61480

Merged
merged 2 commits into from
May 8, 2024
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
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 );
} );
} );
Loading