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 <[email protected]>
  • Loading branch information
2 people authored and tellthemachines committed Aug 1, 2023
1 parent 1d8bb10 commit 0bcda10
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

0 comments on commit 0bcda10

Please sign in to comment.