From ba85e8ee1f7b86ed8c098cfd7608b21137087e18 Mon Sep 17 00:00:00 2001 From: Mitchell Austin Date: Fri, 22 Nov 2024 09:50:41 -0800 Subject: [PATCH] =?UTF-8?q?Initialize=20meta=20boxes=20whether=20or=20not?= =?UTF-8?q?=20they=E2=80=99re=20visible?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../edit-post/src/components/layout/index.js | 35 ++++++++++++++++-- .../src/components/meta-boxes/index.js | 37 ++----------------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/packages/edit-post/src/components/layout/index.js b/packages/edit-post/src/components/layout/index.js index aec14eab989f0..d3a21711af2a9 100644 --- a/packages/edit-post/src/components/layout/index.js +++ b/packages/edit-post/src/components/layout/index.js @@ -17,7 +17,7 @@ import { store as editorStore, privateApis as editorPrivateApis, } from '@wordpress/editor'; -import { useSelect, useDispatch } from '@wordpress/data'; +import { useSelect, useDispatch, useRegistry } from '@wordpress/data'; import { privateApis as blockEditorPrivateApis, store as blockEditorStore, @@ -26,6 +26,7 @@ import { PluginArea } from '@wordpress/plugins'; import { __, sprintf } from '@wordpress/i18n'; import { useCallback, + useEffect, useMemo, useId, useRef, @@ -153,17 +154,37 @@ function useEditorStyles( ...additionalStyles ) { * @param {boolean} props.isLegacy True when the editor canvas is not in an iframe. */ function MetaBoxesMain( { isLegacy } ) { - const [ isOpen, openHeight, hasAnyVisible ] = useSelect( ( select ) => { + const [ + hasInitialized, + isEditorReady, + isOpen, + openHeight, + hasAny, + hasAnyVisible, + ] = useSelect( ( select ) => { + const { __unstableIsEditorReady } = select( editorStore ); const { get } = select( preferencesStore ); - const { isMetaBoxLocationVisible } = select( editPostStore ); + const { + areMetaBoxesInitialized, + getMetaBoxesPerLocation, + isMetaBoxLocationVisible, + } = select( editPostStore ); return [ + areMetaBoxesInitialized(), + __unstableIsEditorReady(), get( 'core/edit-post', 'metaBoxesMainIsOpen' ), get( 'core/edit-post', 'metaBoxesMainOpenHeight' ), + [ + ...getMetaBoxesPerLocation( 'normal' ), + ...getMetaBoxesPerLocation( 'advanced' ), + ...getMetaBoxesPerLocation( 'side' ), + ].length > 0, isMetaBoxLocationVisible( 'normal' ) || isMetaBoxLocationVisible( 'advanced' ) || isMetaBoxLocationVisible( 'side' ), ]; }, [] ); + const registry = useRegistry(); const { set: setPreference } = useDispatch( preferencesStore ); const metaBoxesMainRef = useRef(); const isShort = useMediaQuery( '(max-height: 549px)' ); @@ -226,6 +247,14 @@ function MetaBoxesMain( { isLegacy } ) { } }; + // When editor is ready, initialize postboxes (wp core script) and meta box + // saving. This initializes all meta box locations. + useEffect( () => { + if ( isEditorReady && hasAny && ! hasInitialized ) { + registry.dispatch( editPostStore ).initializeMetaBoxes(); + } + }, [ isEditorReady, hasAny, hasInitialized, registry ] ); + if ( ! hasAnyVisible ) { return; } diff --git a/packages/edit-post/src/components/meta-boxes/index.js b/packages/edit-post/src/components/meta-boxes/index.js index 14728c97cf6b6..fdc74a5df4ce9 100644 --- a/packages/edit-post/src/components/meta-boxes/index.js +++ b/packages/edit-post/src/components/meta-boxes/index.js @@ -1,9 +1,7 @@ /** * WordPress dependencies */ -import { useSelect, useRegistry } from '@wordpress/data'; -import { useEffect } from '@wordpress/element'; -import { store as editorStore } from '@wordpress/editor'; +import { useSelect } from '@wordpress/data'; /** * Internal dependencies @@ -13,38 +11,11 @@ import MetaBoxVisibility from './meta-box-visibility'; import { store as editPostStore } from '../../store'; export default function MetaBoxes( { location } ) { - const registry = useRegistry(); - const { metaBoxes, areMetaBoxesInitialized, isEditorReady } = useSelect( - ( select ) => { - const { __unstableIsEditorReady } = select( editorStore ); - const { - getMetaBoxesPerLocation, - areMetaBoxesInitialized: _areMetaBoxesInitialized, - } = select( editPostStore ); - return { - metaBoxes: getMetaBoxesPerLocation( location ), - areMetaBoxesInitialized: _areMetaBoxesInitialized(), - isEditorReady: __unstableIsEditorReady(), - }; - }, - [ location ] + const metaBoxes = useSelect( + ( select ) => + select( editPostStore ).getMetaBoxesPerLocation[ location ] ); - const hasMetaBoxes = !! metaBoxes?.length; - - // When editor is ready, initialize postboxes (wp core script) and metabox - // saving. This initializes all meta box locations, not just this specific - // one. - useEffect( () => { - if ( isEditorReady && hasMetaBoxes && ! areMetaBoxesInitialized ) { - registry.dispatch( editPostStore ).initializeMetaBoxes(); - } - }, [ isEditorReady, hasMetaBoxes, areMetaBoxesInitialized ] ); - - if ( ! areMetaBoxesInitialized ) { - return null; - } - return ( <> { ( metaBoxes ?? [] ).map( ( { id } ) => (