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

StartPageOptions: load and parse patterns only after establishing the need for them #53673

Merged
merged 2 commits into from
Aug 23, 2023
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
2 changes: 0 additions & 2 deletions packages/block-editor/src/components/block-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import BlockListBlock from './block';
import BlockListAppender from '../block-list-appender';
import { useInBetweenInserter } from './use-in-between-inserter';
import { store as blockEditorStore } from '../../store';
import { usePreParsePatterns } from '../../utils/pre-parse-patterns';
import { LayoutProvider, defaultLayout } from './layout';
import { useBlockSelectionClearer } from '../block-selection-clearer';
import { useInnerBlocksProps } from '../inner-blocks';
Expand Down Expand Up @@ -124,7 +123,6 @@ function Root( { className, ...settings } ) {
}

export default function BlockList( settings ) {
usePreParsePatterns();
return (
<BlockEditContextProvider value={ DEFAULT_BLOCK_EDIT_CONTEXT }>
<Root { ...settings } />
Expand Down
69 changes: 0 additions & 69 deletions packages/block-editor/src/utils/pre-parse-patterns.js

This file was deleted.

92 changes: 38 additions & 54 deletions packages/edit-post/src/components/start-page-options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { store as editPostStore } from '../../store';

function useStartPatterns() {
// A pattern is a start pattern if it includes 'core/post-content' in its blockTypes,
// and it has no postTypes declares and the current post type is page or if
// and it has no postTypes declared and the current post type is page or if
// the current post type is part of the postTypes declared.
const { blockPatternsWithPostContentBlockType, postType } = useSelect(
( select ) => {
Expand Down Expand Up @@ -47,8 +47,7 @@ function useStartPatterns() {
}, [ postType, blockPatternsWithPostContentBlockType ] );
}

function PatternSelection( { onChoosePattern } ) {
const blockPatterns = useStartPatterns();
function PatternSelection( { blockPatterns, onChoosePattern } ) {
const shownBlockPatterns = useAsyncList( blockPatterns );
const { resetEditorBlocks } = useDispatch( editorStore );
return (
Expand All @@ -63,70 +62,55 @@ function PatternSelection( { onChoosePattern } ) {
);
}

const START_PAGE_MODAL_STATES = {
INITIAL: 'INITIAL',
PATTERN: 'PATTERN',
CLOSED: 'CLOSED',
};

export default function StartPageOptions() {
const [ modalState, setModalState ] = useState(
START_PAGE_MODAL_STATES.INITIAL
);
const blockPatterns = useStartPatterns();
const hasStartPattern = blockPatterns.length > 0;
const shouldOpenModel = useSelect(
( select ) => {
if (
! hasStartPattern ||
modalState !== START_PAGE_MODAL_STATES.INITIAL
) {
return false;
}
const { getEditedPostContent, isEditedPostSaveable } =
select( editorStore );
const { isEditingTemplate, isFeatureActive } =
select( editPostStore );
return (
! isEditedPostSaveable() &&
'' === getEditedPostContent() &&
! isEditingTemplate() &&
! isFeatureActive( 'welcomeGuide' )
);
},
[ modalState, hasStartPattern ]
);
function StartPageOptionsModal() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe there's also StartTemplateOptions in the site editor, I wonder if it suffers the same issue.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's a good point, I reviewed StartTemplateOptions, and found that it also parses patterns, but it already uses the "two nested components" technique that my PR introduces for StartPageOptions. The outer component checks the pre-conditions, and only the inner component starts working with patterns.

const [ modalState, setModalState ] = useState( 'initial' );
const startPatterns = useStartPatterns();
const hasStartPattern = startPatterns.length > 0;
const shouldOpenModal = hasStartPattern && modalState === 'initial';

useEffect( () => {
if ( shouldOpenModel ) {
setModalState( START_PAGE_MODAL_STATES.PATTERN );
if ( shouldOpenModal ) {
setModalState( 'open' );
}
}, [ shouldOpenModel ] );
}, [ shouldOpenModal ] );

if (
modalState === START_PAGE_MODAL_STATES.INITIAL ||
modalState === START_PAGE_MODAL_STATES.CLOSED
) {
if ( modalState !== 'open' ) {
return null;
}

return (
<Modal
className="edit-post-start-page-options__modal"
title={ __( 'Choose a pattern' ) }
isFullScreen={ true }
onRequestClose={ () => {
setModalState( START_PAGE_MODAL_STATES.CLOSED );
} }
isFullScreen
onRequestClose={ () => setModalState( 'closed' ) }
>
<div className="edit-post-start-page-options__modal-content">
{ modalState === START_PAGE_MODAL_STATES.PATTERN && (
<PatternSelection
onChoosePattern={ () => {
setModalState( START_PAGE_MODAL_STATES.CLOSED );
} }
/>
) }
<PatternSelection
blockPatterns={ startPatterns }
onChoosePattern={ () => setModalState( 'closed' ) }
/>
</div>
</Modal>
);
}

export default function StartPageOptions() {
const shouldEnableModal = useSelect( ( select ) => {
const { getEditedPostContent, isEditedPostSaveable } =
select( editorStore );
const { isEditingTemplate, isFeatureActive } = select( editPostStore );
return (
! isEditedPostSaveable() &&
'' === getEditedPostContent() &&
! isEditingTemplate() &&
! isFeatureActive( 'welcomeGuide' )
);
}, [] );

if ( ! shouldEnableModal ) {
return null;
}

return <StartPageOptionsModal />;
}