Skip to content

Commit

Permalink
EditorProvider: avoid re-rendering EntityProviders on type
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Dec 6, 2023
1 parent 86cf00a commit 6a24efa
Showing 1 changed file with 54 additions and 38 deletions.
92 changes: 54 additions & 38 deletions packages/editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,45 @@ function useBlockEditorProps( post, template, mode ) {
];
}

function BlockEditorProviderFromPost( {
post,
rootLevelPost,
template,
mode,
Component,
children,
} ) {
const { editorSettings, selection } = useSelect( ( select ) => {
const { getEditorSettings, getEditorSelection } = select( editorStore );
return {
editorSettings: getEditorSettings(),
selection: getEditorSelection(),
};
}, [] );
const [ blocks, onInput, onChange ] = useBlockEditorProps(
post,
template,
mode
);
const blockEditorSettings = useBlockEditorSettings(
editorSettings,
rootLevelPost.type,
rootLevelPost.id
);
return (
<Component
value={ blocks }
onChange={ onChange }
onInput={ onInput }
selection={ selection }
settings={ blockEditorSettings }
useSubRegistry={ false }
>
{ children }
</Component>
);
}

export const ExperimentalEditorProvider = withRegistryProvider(
( {
post,
Expand All @@ -138,10 +177,14 @@ export const ExperimentalEditorProvider = withRegistryProvider(
BlockEditorProviderComponent = ExperimentalBlockEditorProvider,
__unstableTemplate: template,
} ) => {
const mode = useSelect(
( select ) => select( editorStore ).getRenderingMode(),
[]
);
const { mode, isReady } = useSelect( ( select ) => {
const { __unstableIsEditorReady, getRenderingMode } =
select( editorStore );
return {
mode: getRenderingMode(),
isReady: __unstableIsEditorReady(),
};
}, [] );
const shouldRenderTemplate = !! template && mode !== 'post-only';
const rootLevelPost = shouldRenderTemplate ? template : post;
const defaultBlockContext = useMemo( () => {
Expand All @@ -166,32 +209,6 @@ export const ExperimentalEditorProvider = withRegistryProvider(
rootLevelPost?.slug,
shouldRenderTemplate,
] );
const { editorSettings, selection, isReady } = useSelect(
( select ) => {
const {
getEditorSettings,
getEditorSelection,
__unstableIsEditorReady,
} = select( editorStore );
return {
editorSettings: getEditorSettings(),
isReady: __unstableIsEditorReady(),
selection: getEditorSelection(),
};
},
[]
);
const { id, type } = rootLevelPost;
const blockEditorSettings = useBlockEditorSettings(
editorSettings,
type,
id
);
const [ blocks, onInput, onChange ] = useBlockEditorProps(
post,
template,
mode
);

const {
updatePostLock,
Expand Down Expand Up @@ -261,20 +278,19 @@ export const ExperimentalEditorProvider = withRegistryProvider(
id={ post.id }
>
<BlockContextProvider value={ defaultBlockContext }>
<BlockEditorProviderComponent
value={ blocks }
onChange={ onChange }
onInput={ onInput }
selection={ selection }
settings={ blockEditorSettings }
useSubRegistry={ false }
<BlockEditorProviderFromPost
post={ post }
rootLevelPost={ rootLevelPost }
template={ template }
mode={ mode }
Component={ BlockEditorProviderComponent }
>
{ children }
<PatternsMenuItems />
{ mode === 'template-locked' && (
<DisableNonPageContentBlocks />
) }
</BlockEditorProviderComponent>
</BlockEditorProviderFromPost>
</BlockContextProvider>
</EntityProvider>
</EntityProvider>
Expand Down

0 comments on commit 6a24efa

Please sign in to comment.