Skip to content

Commit

Permalink
Fix not expanding pattern in page editor (#53169)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
  • Loading branch information
kevin940726 and aaronrobertshaw authored Aug 1, 2023
1 parent da0f6ab commit ee96729
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
20 changes: 19 additions & 1 deletion packages/block-library/src/pattern/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import {
useBlockProps,
} from '@wordpress/block-editor';

const PatternEdit = ( { attributes, clientId } ) => {
/**
* Internal dependencies
*/
import { unlock } from '../lock-unlock';

const PatternEdit = ( { attributes, clientId, rootClientId } ) => {
const selectedPattern = useSelect(
( select ) =>
select( blockEditorStore ).__experimentalGetParsedPattern(
Expand All @@ -20,6 +25,8 @@ const PatternEdit = ( { attributes, clientId } ) => {

const { replaceBlocks, __unstableMarkNextChangeAsNotPersistent } =
useDispatch( blockEditorStore );
const { setBlockEditingMode } = unlock( useDispatch( blockEditorStore ) );
const { getBlockEditingMode } = unlock( useSelect( blockEditorStore ) );

// Run this effect when the component loads.
// This adds the Pattern's contents to the post.
Expand All @@ -38,15 +45,26 @@ const PatternEdit = ( { attributes, clientId } ) => {
const clonedBlocks = selectedPattern.blocks.map( ( block ) =>
cloneBlock( block )
);
const rootEditingMode = getBlockEditingMode( rootClientId );
// Temporarily set the root block to default mode to allow replacing the pattern.
// This could happen when the page is disabling edits of non-content blocks.
__unstableMarkNextChangeAsNotPersistent();
setBlockEditingMode( rootClientId, 'default' );
__unstableMarkNextChangeAsNotPersistent();
replaceBlocks( clientId, clonedBlocks );
// Restore the root block's original mode.
__unstableMarkNextChangeAsNotPersistent();
setBlockEditingMode( rootClientId, rootEditingMode );
} );
}
}, [
rootClientId,
clientId,
selectedPattern?.blocks,
__unstableMarkNextChangeAsNotPersistent,
replaceBlocks,
getBlockEditingMode,
setBlockEditingMode,
] );

const props = useBlockProps();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function useDisableNonPageContentBlocks() {

const withDisableNonPageContentBlocks = createHigherOrderComponent(
( BlockEdit ) => ( props ) => {
const isDescendentOfQueryLoop = !! props.context.queryId;
const isDescendentOfQueryLoop = props.context.queryId !== undefined;
const isPageContent =
PAGE_CONTENT_BLOCK_TYPES.includes( props.name ) &&
! isDescendentOfQueryLoop;
Expand Down

1 comment on commit ee96729

@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 ee96729.
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/5721749448
📝 Reported issues:

Please sign in to comment.