Skip to content

Commit

Permalink
Fix: InnerBlocks allowed blocks change with different sizes. (#53943)
Browse files Browse the repository at this point in the history
Co-authored-by: George Mamadashvili <[email protected]>
  • Loading branch information
jorgefilipecosta and Mamaduka authored Sep 5, 2023
1 parent b5a6f0b commit b7f4ef5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/**
* WordPress dependencies
*/
import { useLayoutEffect, useMemo } from '@wordpress/element';
import { useLayoutEffect, useMemo, useState } from '@wordpress/element';
import { useSelect, useDispatch, useRegistry } from '@wordpress/data';
import deprecated from '@wordpress/deprecated';
import isShallowEqual from '@wordpress/is-shallow-equal';

/**
* Internal dependencies
Expand All @@ -15,6 +16,14 @@ import { getLayoutType } from '../../layouts';

const pendingSettingsUpdates = new WeakMap();

function useShallowMemo( value ) {
const [ prevValue, setPrevValue ] = useState( value );
if ( ! isShallowEqual( prevValue, value ) ) {
setPrevValue( value );
}
return prevValue;
}

/**
* This hook is a side effect which updates the block-editor store when changes
* happen to inner block settings. The given props are transformed into a
Expand Down Expand Up @@ -70,16 +79,12 @@ export default function useNestedSettingsUpdate(
[ clientId ]
);

// Memoize allowedBlocks and prioritisedInnerBlocks based on the contents
// of the arrays. Implementors often pass a new array on every render,
// Implementors often pass a new array on every render,
// and the contents of the arrays are just strings, so the entire array
// can be passed as dependencies.

const _allowedBlocks = useMemo(
() => allowedBlocks,
// eslint-disable-next-line react-hooks/exhaustive-deps
allowedBlocks
);
// can be passed as dependencies but We need to include the length of the array,
// otherwise if the arrays change length but the first elements are equal the comparison,
// does not works as expected.
const _allowedBlocks = useShallowMemo( allowedBlocks );

const _prioritizedInserterBlocks = useMemo(
() => prioritizedInserterBlocks,
Expand Down
26 changes: 16 additions & 10 deletions packages/e2e-tests/plugins/inner-blocks-allowed-blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
const { useSelect } = wp.data;
const { registerBlockType } = wp.blocks;
const { createElement: el } = wp.element;
const { InnerBlocks } = wp.blockEditor;
const { InnerBlocks, useBlockProps } = wp.blockEditor;
const divProps = {
className: 'product',
style: { outline: '1px solid gray', padding: 5 },
};

const allowedBlocksWhenSingleEmptyChild = [ 'core/image', 'core/list' ];
const allowedBlocksWhenMultipleChildren = [ 'core/gallery', 'core/video' ];
const allowedBlocksWhenTwoChildren = [ 'core/gallery', 'core/video' ];
const allowedBlocksWhenTreeOrMoreChildren = [ 'core/gallery', 'core/video', 'core/list' ];

registerBlockType( 'test/allowed-blocks-dynamic', {
apiVersion: 3,
Expand All @@ -25,18 +26,23 @@
},
[ props.clientId ]
);
const blockProps = useBlockProps({
...divProps,
'data-number-of-children': numberOfChildren,
});

let allowedBlocks = allowedBlocksWhenSingleEmptyChild;
if ( numberOfChildren === 2 ) {
allowedBlocks = allowedBlocksWhenTwoChildren;
} else if( numberOfChildren > 2 ){
allowedBlocks = allowedBlocksWhenTreeOrMoreChildren;
}

return el(
'div',
{
...divProps,
'data-number-of-children': numberOfChildren,
},
blockProps,
el( InnerBlocks, {
allowedBlocks:
numberOfChildren < 2
? allowedBlocksWhenSingleEmptyChild
: allowedBlocksWhenMultipleChildren,
allowedBlocks
} )
);
},
Expand Down
12 changes: 12 additions & 0 deletions test/e2e/specs/editor/plugins/inner-blocks-allowed-blocks.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,17 @@ test.describe( 'Allowed Blocks Setting on InnerBlocks', () => {
'Gallery',
'Video',
] );

await blockListBox.getByRole( 'option', { name: 'Gallery' } ).click();

await editor.clickBlockToolbarButton( 'Select Allowed Blocks Dynamic' );
await blockAppender.click();

// It should display a different allowed block list.
await expect( blockListBox.getByRole( 'option' ) ).toHaveText( [
'Gallery',
'List',
'Video',
] );
} );
} );

1 comment on commit b7f4ef5

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in b7f4ef5.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6086624807
📝 Reported issues:

Please sign in to comment.