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

Patterns: Prevent exception if a pattern with overrides is nested in another pattern #58359

Closed
wants to merge 3 commits into from
Closed
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
71 changes: 45 additions & 26 deletions packages/block-library/src/block/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ function getOverridesFromBlocks( blocks, defaultValues ) {
/** @type {Record<string, Record<string, unknown>>} */
const overrides = {};
for ( const block of blocks ) {
if (
block.name === 'core/block' &&
getHasOverridableBlocks( block.innerBlocks )
) {
continue;
}
Object.assign(
overrides,
getOverridesFromBlocks( block.innerBlocks, defaultValues )
Expand Down Expand Up @@ -185,28 +191,37 @@ export default function ReusableBlockEdit( {
} = useDispatch( blockEditorStore );
const { syncDerivedUpdates } = unlock( useDispatch( blockEditorStore ) );

const { innerBlocks, userCanEdit, getBlockEditingMode, getPostLinkProps } =
useSelect(
( select ) => {
const { canUser } = select( coreStore );
const {
getBlocks,
getBlockEditingMode: editingMode,
getSettings,
} = select( blockEditorStore );
const blocks = getBlocks( patternClientId );
const canEdit = canUser( 'update', 'blocks', ref );

// For editing link to the site editor if the theme and user permissions support it.
return {
innerBlocks: blocks,
userCanEdit: canEdit,
getBlockEditingMode: editingMode,
getPostLinkProps: getSettings().getPostLinkProps,
};
},
[ patternClientId, ref ]
);
const {
innerBlocks,
userCanEdit,
getBlockEditingMode,
getPostLinkProps,
hasParentPattern,
} = useSelect(
( select ) => {
const { canUser } = select( coreStore );
const {
getBlocks,
getBlockEditingMode: editingMode,
getSettings,
getBlockParentsByBlockName,
} = select( blockEditorStore );
const blocks = getBlocks( patternClientId );
const canEdit = canUser( 'update', 'blocks', ref );

// For editing link to the site editor if the theme and user permissions support it.
return {
innerBlocks: blocks,
userCanEdit: canEdit,
getBlockEditingMode: editingMode,
getPostLinkProps: getSettings().getPostLinkProps,
hasParentPattern:
getBlockParentsByBlockName( patternClientId, 'core/block' )
.length > 0,
};
},
[ patternClientId, ref ]
);

const editOriginalProps = getPostLinkProps
? getPostLinkProps( {
Expand All @@ -215,10 +230,14 @@ export default function ReusableBlockEdit( {
} )
: {};

useEffect(
() => setBlockEditMode( setBlockEditingMode, innerBlocks ),
[ innerBlocks, setBlockEditingMode ]
);
useEffect( () => {
if ( hasParentPattern ) {
setBlockEditMode( setBlockEditingMode, innerBlocks, 'disabled' );
return;
}

setBlockEditMode( setBlockEditingMode, innerBlocks );
}, [ hasParentPattern, innerBlocks, setBlockEditingMode ] );

const hasOverridableBlocks = useMemo(
() => getHasOverridableBlocks( innerBlocks ),
Expand Down
Loading