Skip to content

Commit

Permalink
StartPageOptions: load and parse patterns only after establishing the…
Browse files Browse the repository at this point in the history
… need for them (#53673)

* Remove pattern pre-parsing

* StartPageOptions: load and parse patterns only after establishing the need for them
  • Loading branch information
jsnajdr authored Aug 23, 2023
1 parent f5a26db commit 52532a5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 125 deletions.
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() {
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 />;
}

0 comments on commit 52532a5

Please sign in to comment.