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

Parsing patterns when idling (performance follow-up for inserting patterns into containers) #29444

Merged
merged 19 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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: 2 additions & 0 deletions packages/block-editor/src/components/block-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import useInsertionPoint from './insertion-point';
import BlockPopover from './block-popover';
import { store as blockEditorStore } from '../../store';
import { useScrollSelectionIntoView } from '../selection-scroll-into-view';
import { usePreParsePatterns } from '../../utils/pre-parse-patterns';

export const BlockNodes = createContext();
export const SetBlockNodes = createContext();
Expand All @@ -28,6 +29,7 @@ export default function BlockList( { className } ) {
const [ blockNodes, setBlockNodes ] = useState( {} );
const insertionPoint = useInsertionPoint( ref );
useScrollSelectionIntoView( ref );
usePreParsePatterns();

return (
<BlockNodes.Provider value={ blockNodes }>
Expand Down
1 change: 0 additions & 1 deletion packages/block-editor/src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { default as transformStyles } from './transform-styles';
export * from './theme';
export * from './block-variation-transforms';
export { usePreParsePatterns as __experimentalPreParsePatterns } from './pre-parse-patterns';
15 changes: 8 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,12 +28,13 @@ const cancelIdleCallback = ( () => {
} )();

export function usePreParsePatterns() {
const patterns = useSelect(
( _select ) =>
_select( blockEditorStore ).getSettings()
.__experimentalBlockPatterns,
[]
);
const patterns =
useSelect(
( _select ) =>
_select( blockEditorStore ).getSettings()
.__experimentalBlockPatterns,
[]
) || [];

useEffect( () => {
if ( ! patterns?.length ) {
Expand Down Expand Up @@ -65,7 +66,7 @@ export function usePreParsePatterns() {

handle = requestIdleCallback( callback );
return () => cancelIdleCallback( handle );
}, [ patterns ] );
}, patterns );
youknowriad marked this conversation as resolved.
Show resolved Hide resolved

return null;
}
3 changes: 0 additions & 3 deletions packages/edit-post/src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
SlotFillProvider,
__unstableDropZoneContextProvider as DropZoneContextProvider,
} from '@wordpress/components';
import { __experimentalPreParsePatterns as usePreParsePatterns } from '@wordpress/block-editor';

/**
* Internal dependencies
Expand Down Expand Up @@ -167,8 +166,6 @@ function Editor( {
return hasThemeStyles ? settings.styles : settings.defaultEditorStyles;
}, [ settings, hasThemeStyles ] );

usePreParsePatterns();

if ( ! post ) {
return null;
}
Expand Down
8 changes: 1 addition & 7 deletions packages/edit-site/src/components/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ import {
Button,
} from '@wordpress/components';
import { EntityProvider } from '@wordpress/core-data';
import {
BlockContextProvider,
BlockBreadcrumb,
__experimentalPreParsePatterns as usePreParsePatterns,
} from '@wordpress/block-editor';
import { BlockContextProvider, BlockBreadcrumb } from '@wordpress/block-editor';
import {
FullscreenMode,
InterfaceSkeleton,
Expand Down Expand Up @@ -151,8 +147,6 @@ function Editor( { initialSettings } ) {
}
}, [ isNavigationOpen ] );

usePreParsePatterns();

// Don't render the Editor until the settings are set and loaded
if ( ! settings?.siteUrl ) {
return null;
Expand Down