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

Fix not expanding pattern in page editor #53169

Merged
merged 2 commits into from
Aug 1, 2023
Merged
Changes from 1 commit
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
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 } ) => {
Copy link
Member

Choose a reason for hiding this comment

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

The rootClientId will be undefined since the block edit component doesn't receive it as a prop.

let blockEdit = (
<BlockEdit
name={ name }
isSelected={ isSelected }
attributes={ attributes }
setAttributes={ setAttributes }
insertBlocksAfter={ isLocked ? undefined : onInsertBlocksAfter }
onReplace={ canRemove ? onReplace : undefined }
onRemove={ canRemove ? onRemove : undefined }
mergeBlocks={ canRemove ? onMerge : undefined }
clientId={ clientId }
isSelectionEnabled={ isSelectionEnabled }
toggleSelection={ toggleSelection }
__unstableLayoutClassNames={ layoutClassNames }
__unstableParentLayout={
Object.keys( parentLayout ).length ? parentLayout : undefined
}
/>
);

Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch @Mamaduka! I've opened a tiny follow-up in #53206

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 );
Comment on lines +49 to +57
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should batch these updates. What do you think?

} );
}
}, [
rootClientId,
clientId,
selectedPattern?.blocks,
__unstableMarkNextChangeAsNotPersistent,
replaceBlocks,
getBlockEditingMode,
setBlockEditingMode,
] );

const props = useBlockProps();
Expand Down
Loading