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

Try nested patterns previews #44773

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
41 changes: 27 additions & 14 deletions packages/block-editor/src/components/block-preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,21 @@ export function BlockPreview( {
__experimentalOnClick,
__experimentalMinHeight,
} ) {
const originalSettings = useSelect(
( select ) => select( blockEditorStore ).getSettings(),
[]
const { originalSettings, patterns } = useSelect( ( select ) => {
const { getSettings, __experimentalGetAllowedPatterns } =
select( blockEditorStore );
return {
originalSettings: getSettings(),
patterns: __experimentalGetAllowedPatterns(),
};
}, [] );
const settings = useMemo(
() => ( {
...originalSettings,
__experimentalBlockPatterns: patterns,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could even pass the parsed patterns only if we are previewing patterns..

} ),
[ originalSettings, patterns ]
);
const settings = useMemo( () => {
const _settings = { ...originalSettings };
_settings.__experimentalBlockPatterns = [];
return _settings;
}, [ originalSettings ] );
const renderedBlocks = useMemo( () => castArray( blocks ), [ blocks ] );
if ( ! blocks || blocks.length === 0 ) {
return null;
Expand Down Expand Up @@ -90,15 +96,22 @@ export function useBlockPreview( {
props = {},
__experimentalLayout,
} ) {
const originalSettings = useSelect(
( select ) => select( blockEditorStore ).getSettings(),
[]
);
const disabledRef = useDisabled();
const ref = useMergeRefs( [ props.ref, disabledRef ] );
const { originalSettings, patterns } = useSelect( ( select ) => {
const { getSettings, __experimentalGetAllowedPatterns } =
select( blockEditorStore );
return {
originalSettings: getSettings(),
patterns: __experimentalGetAllowedPatterns(),
};
}, [] );
const settings = useMemo(
() => ( { ...originalSettings, __experimentalBlockPatterns: [] } ),
[ originalSettings ]
() => ( {
...originalSettings,
__experimentalBlockPatterns: patterns,
} ),
[ originalSettings, patterns ]
);
const renderedBlocks = useMemo( () => castArray( blocks ), [ blocks ] );

Expand Down
5 changes: 5 additions & 0 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2249,6 +2249,11 @@ export const __experimentalGetParsedPattern = createSelector(
if ( ! pattern ) {
return null;
}
// In previews we pass the already passed patterns
// to avoid parsing again.
if ( pattern.blocks ) {
return { ...pattern };
}
return {
...pattern,
blocks: parse( pattern.content, {
Expand Down
13 changes: 10 additions & 3 deletions packages/block-library/src/pattern/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,17 @@ const PatternEdit = ( { attributes, clientId } ) => {
// It will continue to pull from the pattern file unless changes are made to its respective template part.
useEffect( () => {
if ( selectedPattern?.blocks ) {
__unstableMarkNextChangeAsNotPersistent();
replaceBlocks( clientId, selectedPattern.blocks );
// We batch updates to block list settings to avoid triggering cascading renders
// for each container block included in a tree and optimize initial render.
// Since the above uses microtasks, we need to use a microtask here as well,
// because nested pattern blocks cannot be inserted if the parent block supports
// inner blocks but doesn't have blockSettings in the state.
window.queueMicrotask( () => {
__unstableMarkNextChangeAsNotPersistent();
replaceBlocks( clientId, selectedPattern.blocks );
} );
}
}, [ selectedPattern?.blocks ] );
}, [ clientId, selectedPattern?.blocks ] );

const props = useBlockProps();

Expand Down