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 with block editor setting #44784

Merged
merged 1 commit into from
Oct 12, 2022
Merged
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
15 changes: 7 additions & 8 deletions packages/block-editor/src/components/block-preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ export function BlockPreview( {
( select ) => select( blockEditorStore ).getSettings(),
[]
);
const settings = useMemo( () => {
const _settings = { ...originalSettings };
_settings.__experimentalBlockPatterns = [];
return _settings;
}, [ originalSettings ] );
const settings = useMemo(
() => ( { ...originalSettings, __unstableIsPreviewMode: true } ),
[ originalSettings ]
);
const renderedBlocks = useMemo( () => castArray( blocks ), [ blocks ] );
if ( ! blocks || blocks.length === 0 ) {
return null;
Expand Down Expand Up @@ -94,12 +93,12 @@ export function useBlockPreview( {
( select ) => select( blockEditorStore ).getSettings(),
[]
);
const disabledRef = useDisabled();
const ref = useMergeRefs( [ props.ref, disabledRef ] );
const settings = useMemo(
() => ( { ...originalSettings, __experimentalBlockPatterns: [] } ),
() => ( { ...originalSettings, __unstableIsPreviewMode: true } ),
[ originalSettings ]
);
const disabledRef = useDisabled();
const ref = useMergeRefs( [ props.ref, disabledRef ] );
const renderedBlocks = useMemo( () => castArray( blocks ), [ blocks ] );

const children = (
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/store/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export const SETTINGS_DEFAULTS = {
__experimentalBlockPatterns: [],
__experimentalBlockPatternCategories: [],
__unstableGalleryWithImageBlocks: false,
__unstableIsPreviewMode: false,

generateAnchors: false,
// gradients setting is not used anymore now defaults are passed from theme.json on the server and core has its own defaults.
Expand Down
19 changes: 12 additions & 7 deletions packages/block-editor/src/utils/pre-parse-patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@ const cancelIdleCallback = ( () => {
} )();

export function usePreParsePatterns() {
const patterns = useSelect(
( _select ) =>
_select( blockEditorStore ).getSettings()
.__experimentalBlockPatterns,
[]
);
const { patterns, isPreviewMode } = useSelect( ( _select ) => {
const { __experimentalBlockPatterns, __unstableIsPreviewMode } =
_select( blockEditorStore ).getSettings();
return {
patterns: __experimentalBlockPatterns,
isPreviewMode: __unstableIsPreviewMode,
};
}, [] );

useEffect( () => {
if ( isPreviewMode ) {
return;
}
if ( ! patterns?.length ) {
return;
}
Expand All @@ -58,7 +63,7 @@ export function usePreParsePatterns() {

handle = requestIdleCallback( callback );
return () => cancelIdleCallback( handle );
}, [ patterns ] );
}, [ patterns, isPreviewMode ] );

return null;
}
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