From c432edcd96bb4cf8679e9517472e1a2bd1eb9d47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ribari=C4=87?= Date: Thu, 8 Apr 2021 12:28:59 +0200 Subject: [PATCH 01/18] Fix metabox reordering --- packages/edit-post/README.md | 2 +- packages/edit-post/src/editor.js | 14 +++++++++++++- packages/edit-post/src/index.js | 29 +++++++++++++++++------------ 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/packages/edit-post/README.md b/packages/edit-post/README.md index 4f3611fc67a200..e489e1251a3a54 100644 --- a/packages/edit-post/README.md +++ b/packages/edit-post/README.md @@ -37,7 +37,7 @@ call initializeEditor(). This is due to metaBox timing. _Parameters_ - _id_ `string`: Unique identifier for editor instance. -- _postType_ `Object`: Post type of the post to edit. +- _postType_ `string`: Post type of the post to edit. - _postId_ `Object`: ID of the post to edit. - _settings_ `?Object`: Editor settings object. - _initialEdits_ `Object`: Programmatic edits to apply initially, to be considered as non-user-initiated (bypass for unsaved changes prompt). diff --git a/packages/edit-post/src/editor.js b/packages/edit-post/src/editor.js index 3f0fa4d1105c7a..090a8d6e32b396 100644 --- a/packages/edit-post/src/editor.js +++ b/packages/edit-post/src/editor.js @@ -35,6 +35,7 @@ function Editor( { settings, initialEdits, onError, + markEditorReady, ...props } ) { const { @@ -50,6 +51,7 @@ function Editor( { keepCaretInsideBlock, isTemplateMode, template, + isEditorReady } = useSelect( ( select ) => { const { isFeatureActive, @@ -63,7 +65,7 @@ function Editor( { getPostType, getEntityRecords, } = select( 'core' ); - const { getEditorSettings, getCurrentPost } = select( 'core/editor' ); + const { getEditorSettings, getCurrentPost, __unstableIsEditorReady } = select( 'core/editor' ); const { getBlockTypes } = select( blocksStore ); const isTemplate = [ 'wp_template', 'wp_template_part' ].includes( postType @@ -107,6 +109,7 @@ function Editor( { ? __experimentalGetTemplateForLink( postObject.link ) : null, post: postObject, + isEditorReady: __unstableIsEditorReady() }; } ); @@ -170,6 +173,15 @@ function Editor( { return null; } + /** + * markEditorReady is a Promise resolve callback. It is useful to mark + * editor as ready because some features like admin metaboxes JavaScript + * rely on it being ready. + */ + if ( markEditorReady && isEditorReady ) { + markEditorReady(); + } + return ( diff --git a/packages/edit-post/src/index.js b/packages/edit-post/src/index.js index 25acadf68292c4..754b516e619770 100644 --- a/packages/edit-post/src/index.js +++ b/packages/edit-post/src/index.js @@ -9,13 +9,13 @@ import { __experimentalRegisterExperimentalCoreBlocks, } from '@wordpress/block-library'; import { render, unmountComponentAtNode } from '@wordpress/element'; +import { select, subscribe, dispatch } from '@wordpress/data'; /** * Internal dependencies */ import './hooks'; import './plugins'; -export { store } from './store'; import Editor from './editor'; /** @@ -68,12 +68,13 @@ export function reinitializeEditor( * call initializeEditor(). This is due to metaBox timing. * * @param {string} id Unique identifier for editor instance. - * @param {Object} postType Post type of the post to edit. + * @param {string} postType Post type of the post to edit. * @param {Object} postId ID of the post to edit. * @param {?Object} settings Editor settings object. * @param {Object} initialEdits Programmatic edits to apply initially, to be * considered as non-user-initiated (bypass for * unsaved changes prompt). + * @return {Promise} Promise that resolves when editor is initialized. */ export function initializeEditor( id, @@ -138,16 +139,19 @@ export function initializeEditor( } ); } - render( - , - target - ); + return new Promise( ( resolve ) => { + render( + , + target + ); + } ); } export { default as PluginBlockSettingsMenuItem } from './components/block-settings-menu/plugin-block-settings-menu-item'; @@ -160,3 +164,4 @@ export { default as PluginSidebar } from './components/sidebar/plugin-sidebar'; export { default as PluginSidebarMoreMenuItem } from './components/header/plugin-sidebar-more-menu-item'; export { default as __experimentalFullscreenModeClose } from './components/header/fullscreen-mode-close'; export { default as __experimentalMainDashboardButton } from './components/header/main-dashboard-button'; +export { store } from './store'; From 91ca744c5f0a99f96a32079eb8aa082ac04e9cd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ribari=C4=87?= Date: Thu, 8 Apr 2021 16:23:07 +0200 Subject: [PATCH 02/18] Cleanup --- packages/edit-post/src/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/edit-post/src/index.js b/packages/edit-post/src/index.js index 754b516e619770..ff0219df4c10e7 100644 --- a/packages/edit-post/src/index.js +++ b/packages/edit-post/src/index.js @@ -9,7 +9,6 @@ import { __experimentalRegisterExperimentalCoreBlocks, } from '@wordpress/block-library'; import { render, unmountComponentAtNode } from '@wordpress/element'; -import { select, subscribe, dispatch } from '@wordpress/data'; /** * Internal dependencies From a30bd3c4ecb45392149cf72671b17cdca79354d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ribari=C4=87?= Date: Thu, 8 Apr 2021 16:42:10 +0200 Subject: [PATCH 03/18] Cleanup --- packages/edit-post/src/editor.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/edit-post/src/editor.js b/packages/edit-post/src/editor.js index 5f9718a2a95c49..c69778317cd44a 100644 --- a/packages/edit-post/src/editor.js +++ b/packages/edit-post/src/editor.js @@ -168,11 +168,10 @@ function Editor( { return null; } - /** - * markEditorReady is a Promise resolve callback. It is useful to mark - * editor as ready because some features like admin metaboxes JavaScript - * rely on it being ready. - */ + /** + * Some features like back compat metaboxes rely on editor notifying that + * it's ready by calling markEditorReady. + */ if ( markEditorReady && isEditorReady ) { markEditorReady(); } From 68e70f40864de424dfd1d148485ad47744911e95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ribari=C4=87?= Date: Thu, 8 Apr 2021 16:46:40 +0200 Subject: [PATCH 04/18] Formatting --- packages/edit-post/src/editor.js | 16 +++++++++------- packages/edit-post/src/index.js | 26 +++++++++++++------------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/packages/edit-post/src/editor.js b/packages/edit-post/src/editor.js index c69778317cd44a..aa80e8824f2438 100644 --- a/packages/edit-post/src/editor.js +++ b/packages/edit-post/src/editor.js @@ -35,7 +35,7 @@ function Editor( { settings, initialEdits, onError, - markEditorReady, + markEditorReady, ...props } ) { const { @@ -51,7 +51,7 @@ function Editor( { keepCaretInsideBlock, isTemplateMode, template, - isEditorReady + isEditorReady, } = useSelect( ( select ) => { const { isFeatureActive, @@ -63,7 +63,9 @@ function Editor( { const { getEntityRecord, getPostType, getEntityRecords } = select( 'core' ); - const { getEditorSettings, __unsableIsEditorReady } = select( 'core/editor' ); + const { getEditorSettings, __unsableIsEditorReady } = select( + 'core/editor' + ); const { getBlockTypes } = select( blocksStore ); const isTemplate = [ 'wp_template', 'wp_template_part' ].includes( postType @@ -104,7 +106,7 @@ function Editor( { ? getEditedPostTemplate() : null, post: postObject, - isEditorReady: __unstableIsEditorReady() + isEditorReady: __unstableIsEditorReady(), }; } ); @@ -172,9 +174,9 @@ function Editor( { * Some features like back compat metaboxes rely on editor notifying that * it's ready by calling markEditorReady. */ - if ( markEditorReady && isEditorReady ) { - markEditorReady(); - } + if ( markEditorReady && isEditorReady ) { + markEditorReady(); + } return ( diff --git a/packages/edit-post/src/index.js b/packages/edit-post/src/index.js index ff0219df4c10e7..15bd2f114ec4d8 100644 --- a/packages/edit-post/src/index.js +++ b/packages/edit-post/src/index.js @@ -138,19 +138,19 @@ export function initializeEditor( } ); } - return new Promise( ( resolve ) => { - render( - , - target - ); - } ); + return new Promise( ( resolve ) => { + render( + , + target + ); + } ); } export { default as PluginBlockSettingsMenuItem } from './components/block-settings-menu/plugin-block-settings-menu-item'; From d36f3c2d2146ea40f3c9475f30eaf8ae63eb6dc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ribari=C4=87?= Date: Thu, 8 Apr 2021 17:01:12 +0200 Subject: [PATCH 05/18] Fix typo --- packages/edit-post/src/editor.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/edit-post/src/editor.js b/packages/edit-post/src/editor.js index aa80e8824f2438..bf38419b7f08a1 100644 --- a/packages/edit-post/src/editor.js +++ b/packages/edit-post/src/editor.js @@ -63,7 +63,7 @@ function Editor( { const { getEntityRecord, getPostType, getEntityRecords } = select( 'core' ); - const { getEditorSettings, __unsableIsEditorReady } = select( + const { getEditorSettings, __unstableIsEditorReady } = select( 'core/editor' ); const { getBlockTypes } = select( blocksStore ); From eb73aff5b3b3a0ff9e0e5d51b7eb3146a3862ada Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ribari=C4=87?= Date: Thu, 8 Apr 2021 17:23:38 +0200 Subject: [PATCH 06/18] Update docs --- packages/edit-post/README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/edit-post/README.md b/packages/edit-post/README.md index e489e1251a3a54..3f8d9330633c64 100644 --- a/packages/edit-post/README.md +++ b/packages/edit-post/README.md @@ -42,6 +42,10 @@ _Parameters_ - _settings_ `?Object`: Editor settings object. - _initialEdits_ `Object`: Programmatic edits to apply initially, to be considered as non-user-initiated (bypass for unsaved changes prompt). +_Returns_ + +- `Promise`: Promise that resolves when editor is initialized. + # **PluginBlockSettingsMenuItem** Renders a new item in the block settings menu. From 3a263c6b4c789d2c8564dad6c4427830e0ebe712 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ribari=C4=87?= Date: Fri, 30 Jul 2021 19:52:50 +0200 Subject: [PATCH 07/18] Formatting --- packages/edit-post/src/editor.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/edit-post/src/editor.js b/packages/edit-post/src/editor.js index 643bcbe2f537f1..d9c14bbb36ff6e 100644 --- a/packages/edit-post/src/editor.js +++ b/packages/edit-post/src/editor.js @@ -61,7 +61,9 @@ function Editor( { const { getEntityRecord, getPostType, getEntityRecords } = select( coreStore ); - const { getEditorSettings, __unstableIsEditorReady } = select( editorStore ); + const { getEditorSettings, __unstableIsEditorReady } = select( + editorStore + ); const { getBlockTypes } = select( blocksStore ); const isTemplate = [ 'wp_template', 'wp_template_part' ].includes( postType From a24dcbc6eefb287651718901ff6515526c17f853 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ribari=C4=87?= Date: Fri, 27 Aug 2021 01:51:10 +0200 Subject: [PATCH 08/18] Improvements --- packages/edit-post/README.md | 7 -- .../src/components/meta-boxes/index.js | 88 ++++++++++++++++--- packages/edit-post/src/editor.js | 15 +--- packages/edit-post/src/index.js | 27 +++--- packages/edit-post/src/store/actions.js | 50 +---------- 5 files changed, 87 insertions(+), 100 deletions(-) diff --git a/packages/edit-post/README.md b/packages/edit-post/README.md index f6df81a420ac76..677e0c8257aa05 100644 --- a/packages/edit-post/README.md +++ b/packages/edit-post/README.md @@ -31,9 +31,6 @@ They can be found in the global variable `wp.editPost` when defining `wp-edit-po Initializes and returns an instance of Editor. -The return value of this function is not necessary if we change where we -call initializeEditor(). This is due to metaBox timing. - _Parameters_ - _id_ `string`: Unique identifier for editor instance. @@ -42,10 +39,6 @@ _Parameters_ - _settings_ `?Object`: Editor settings object. - _initialEdits_ `Object`: Programmatic edits to apply initially, to be considered as non-user-initiated (bypass for unsaved changes prompt). -_Returns_ - -- `Promise`: Promise that resolves when editor is initialized. - ### PluginBlockSettingsMenuItem Renders a new item in the block settings menu. diff --git a/packages/edit-post/src/components/meta-boxes/index.js b/packages/edit-post/src/components/meta-boxes/index.js index e5e3e2ac2066df..3ee24d72d7bbc7 100644 --- a/packages/edit-post/src/components/meta-boxes/index.js +++ b/packages/edit-post/src/components/meta-boxes/index.js @@ -6,7 +6,9 @@ import { map } from 'lodash'; /** * WordPress dependencies */ -import { withSelect } from '@wordpress/data'; +import { useSelect, select, subscribe, dispatch } from '@wordpress/data'; +import { store as editorStore } from '@wordpress/editor'; +import { useEffect, useState } from '@wordpress/element'; /** * Internal dependencies @@ -15,7 +17,78 @@ import MetaBoxesArea from './meta-boxes-area'; import MetaBoxVisibility from './meta-box-visibility'; import { store as editPostStore } from '../../store'; -function MetaBoxes( { location, isVisible, metaBoxes } ) { +export default function MetaBoxes( { location } ) { + const { isReady, metaBoxes, isVisible, postType } = useSelect( + ( _select ) => { + const { __unstableIsEditorReady, getCurrentPostType } = _select( + editorStore + ); + const { + isMetaBoxLocationVisible, + getMetaBoxesPerLocation, + } = _select( editPostStore ); + return { + isReady: __unstableIsEditorReady(), + metaBoxes: getMetaBoxesPerLocation( location ), + isVisible: isMetaBoxLocationVisible( location ), + postType: getCurrentPostType(), + }; + } + ); + const [ initialized, setInitialized ] = useState( false ); + + // When editor is ready, initialize postboxes (wp core script) and metabox + // saving. + useEffect( () => { + let saveMetaboxUnsubscribe; + + if ( postType !== undefined && isReady && ! initialized ) { + setInitialized( true ); + + if ( window.postboxes.page !== postType ) { + window.postboxes.add_postbox_toggles( postType ); + } + + let wasSavingPost = select( editorStore ).isSavingPost(); + let wasAutosavingPost = select( editorStore ).isAutosavingPost(); + const hasActiveMetaBoxes = select( editPostStore ).hasMetaBoxes(); + + // Save metaboxes when performing a full save on the post. + saveMetaboxUnsubscribe = subscribe( () => { + const isSavingPost = select( editorStore ).isSavingPost(); + const isAutosavingPost = select( + editorStore + ).isAutosavingPost(); + + // Save metaboxes on save completion, except for autosaves that are not a post preview. + // + // Meta boxes are initialized once at page load. It is not necessary to + // account for updates on each state change. + // + // See: https://github.com/WordPress/WordPress/blob/5.1.1/wp-admin/includes/post.php#L2307-L2309 + const shouldTriggerMetaboxesSave = + hasActiveMetaBoxes && + wasSavingPost && + ! isSavingPost && + ! wasAutosavingPost; + + // Save current state for next inspection. + wasSavingPost = isSavingPost; + wasAutosavingPost = isAutosavingPost; + + if ( shouldTriggerMetaboxesSave ) { + dispatch( editPostStore ).requestMetaBoxUpdates(); + } + } ); + } + + return () => saveMetaboxUnsubscribe && saveMetaboxUnsubscribe(); + }, [ isReady, postType, initialized ] ); + + if ( ! isReady ) { + return null; + } + return ( <> { map( metaBoxes, ( { id } ) => ( @@ -25,14 +98,3 @@ function MetaBoxes( { location, isVisible, metaBoxes } ) { ); } - -export default withSelect( ( select, { location } ) => { - const { isMetaBoxLocationVisible, getMetaBoxesPerLocation } = select( - editPostStore - ); - - return { - metaBoxes: getMetaBoxesPerLocation( location ), - isVisible: isMetaBoxLocationVisible( location ), - }; -} )( MetaBoxes ); diff --git a/packages/edit-post/src/editor.js b/packages/edit-post/src/editor.js index dca5695d7cdd16..e2781d1a6a4446 100644 --- a/packages/edit-post/src/editor.js +++ b/packages/edit-post/src/editor.js @@ -33,7 +33,6 @@ function Editor( { settings, initialEdits, onError, - markEditorReady, ...props } ) { const { @@ -49,7 +48,6 @@ function Editor( { keepCaretInsideBlock, isTemplateMode, template, - isEditorReady, } = useSelect( ( select ) => { const { isFeatureActive, @@ -61,9 +59,7 @@ function Editor( { const { getEntityRecord, getPostType, getEntityRecords } = select( coreStore ); - const { getEditorSettings, __unstableIsEditorReady } = select( - editorStore - ); + const { getEditorSettings } = select( editorStore ); const { getBlockTypes } = select( blocksStore ); const isTemplate = [ 'wp_template', 'wp_template_part' ].includes( postType @@ -104,7 +100,6 @@ function Editor( { ? getEditedPostTemplate() : null, post: postObject, - isEditorReady: __unstableIsEditorReady(), }; } ); @@ -168,14 +163,6 @@ function Editor( { return null; } - /** - * Some features like back compat metaboxes rely on editor notifying that - * it's ready by calling markEditorReady. - */ - if ( markEditorReady && isEditorReady ) { - markEditorReady(); - } - return ( diff --git a/packages/edit-post/src/index.js b/packages/edit-post/src/index.js index f4569c9b3c98ad..c20cff1441222b 100644 --- a/packages/edit-post/src/index.js +++ b/packages/edit-post/src/index.js @@ -62,9 +62,6 @@ export function reinitializeEditor( /** * Initializes and returns an instance of Editor. * - * The return value of this function is not necessary if we change where we - * call initializeEditor(). This is due to metaBox timing. - * * @param {string} id Unique identifier for editor instance. * @param {string} postType Post type of the post to edit. * @param {Object} postId ID of the post to edit. @@ -72,7 +69,6 @@ export function reinitializeEditor( * @param {Object} initialEdits Programmatic edits to apply initially, to be * considered as non-user-initiated (bypass for * unsaved changes prompt). - * @return {Promise} Promise that resolves when editor is initialized. */ export function initializeEditor( id, @@ -148,19 +144,16 @@ export function initializeEditor( } ); } - return new Promise( ( resolve ) => { - render( - , - target - ); - } ); + render( + , + target + ); } export { default as PluginBlockSettingsMenuItem } from './components/block-settings-menu/plugin-block-settings-menu-item'; diff --git a/packages/edit-post/src/store/actions.js b/packages/edit-post/src/store/actions.js index d253d55e74efd7..a7672629675468 100644 --- a/packages/edit-post/src/store/actions.js +++ b/packages/edit-post/src/store/actions.js @@ -9,7 +9,7 @@ import { castArray, reduce } from 'lodash'; import { __ } from '@wordpress/i18n'; import { apiFetch } from '@wordpress/data-controls'; import { store as interfaceStore } from '@wordpress/interface'; -import { controls, dispatch, select, subscribe } from '@wordpress/data'; +import { controls } from '@wordpress/data'; import { speak } from '@wordpress/a11y'; import { store as noticesStore } from '@wordpress/notices'; import { store as coreStore } from '@wordpress/core-data'; @@ -265,8 +265,6 @@ export function showBlockTypes( blockNames ) { }; } -let saveMetaboxUnsubscribe; - /** * Returns an action object used in signaling * what Meta boxes are available in which location. @@ -280,52 +278,6 @@ export function* setAvailableMetaBoxesPerLocation( metaBoxesPerLocation ) { type: 'SET_META_BOXES_PER_LOCATIONS', metaBoxesPerLocation, }; - - const postType = yield controls.select( editorStore, 'getCurrentPostType' ); - if ( window.postboxes.page !== postType ) { - window.postboxes.add_postbox_toggles( postType ); - } - - let wasSavingPost = yield controls.select( editorStore, 'isSavingPost' ); - let wasAutosavingPost = yield controls.select( - editorStore, - 'isAutosavingPost' - ); - - // Meta boxes are initialized once at page load. It is not necessary to - // account for updates on each state change. - // - // See: https://github.com/WordPress/WordPress/blob/5.1.1/wp-admin/includes/post.php#L2307-L2309 - const hasActiveMetaBoxes = yield controls.select( - editPostStore, - 'hasMetaBoxes' - ); - - // First remove any existing subscription in order to prevent multiple saves - if ( !! saveMetaboxUnsubscribe ) { - saveMetaboxUnsubscribe(); - } - - // Save metaboxes when performing a full save on the post. - saveMetaboxUnsubscribe = subscribe( () => { - const isSavingPost = select( editorStore ).isSavingPost(); - const isAutosavingPost = select( editorStore ).isAutosavingPost(); - - // Save metaboxes on save completion, except for autosaves that are not a post preview. - const shouldTriggerMetaboxesSave = - hasActiveMetaBoxes && - wasSavingPost && - ! isSavingPost && - ! wasAutosavingPost; - - // Save current state for next inspection. - wasSavingPost = isSavingPost; - wasAutosavingPost = isAutosavingPost; - - if ( shouldTriggerMetaboxesSave ) { - dispatch( editPostStore ).requestMetaBoxUpdates(); - } - } ); } /** From 47ffc13ecfb99cf4dc38e5910c970ad2863380b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ribari=C4=87?= Date: Fri, 27 Aug 2021 02:37:07 +0200 Subject: [PATCH 09/18] Don't unsubscribe --- packages/edit-post/src/components/meta-boxes/index.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/edit-post/src/components/meta-boxes/index.js b/packages/edit-post/src/components/meta-boxes/index.js index 3ee24d72d7bbc7..3ddf1538c6e564 100644 --- a/packages/edit-post/src/components/meta-boxes/index.js +++ b/packages/edit-post/src/components/meta-boxes/index.js @@ -40,8 +40,6 @@ export default function MetaBoxes( { location } ) { // When editor is ready, initialize postboxes (wp core script) and metabox // saving. useEffect( () => { - let saveMetaboxUnsubscribe; - if ( postType !== undefined && isReady && ! initialized ) { setInitialized( true ); @@ -54,7 +52,7 @@ export default function MetaBoxes( { location } ) { const hasActiveMetaBoxes = select( editPostStore ).hasMetaBoxes(); // Save metaboxes when performing a full save on the post. - saveMetaboxUnsubscribe = subscribe( () => { + subscribe( () => { const isSavingPost = select( editorStore ).isSavingPost(); const isAutosavingPost = select( editorStore @@ -81,8 +79,6 @@ export default function MetaBoxes( { location } ) { } } ); } - - return () => saveMetaboxUnsubscribe && saveMetaboxUnsubscribe(); }, [ isReady, postType, initialized ] ); if ( ! isReady ) { From f86617ec3e5052179b4fff4b85e24608fb69236d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ribari=C4=87?= Date: Fri, 27 Aug 2021 03:41:38 +0200 Subject: [PATCH 10/18] Refactoring --- .../src/components/meta-boxes/index.js | 28 ++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/packages/edit-post/src/components/meta-boxes/index.js b/packages/edit-post/src/components/meta-boxes/index.js index 3ddf1538c6e564..e191470d097260 100644 --- a/packages/edit-post/src/components/meta-boxes/index.js +++ b/packages/edit-post/src/components/meta-boxes/index.js @@ -6,7 +6,7 @@ import { map } from 'lodash'; /** * WordPress dependencies */ -import { useSelect, select, subscribe, dispatch } from '@wordpress/data'; +import { useSelect, subscribe, dispatch } from '@wordpress/data'; import { store as editorStore } from '@wordpress/editor'; import { useEffect, useState } from '@wordpress/element'; @@ -19,14 +19,14 @@ import { store as editPostStore } from '../../store'; export default function MetaBoxes( { location } ) { const { isReady, metaBoxes, isVisible, postType } = useSelect( - ( _select ) => { - const { __unstableIsEditorReady, getCurrentPostType } = _select( + ( select ) => { + const { __unstableIsEditorReady, getCurrentPostType } = select( editorStore ); const { isMetaBoxLocationVisible, getMetaBoxesPerLocation, - } = _select( editPostStore ); + } = select( editPostStore ); return { isReady: __unstableIsEditorReady(), metaBoxes: getMetaBoxesPerLocation( location ), @@ -35,6 +35,8 @@ export default function MetaBoxes( { location } ) { }; } ); + const { isSavingPost, isAutosavingPost } = useSelect( editorStore ); + const { hasMetaBoxes } = useSelect( editPostStore ); const [ initialized, setInitialized ] = useState( false ); // When editor is ready, initialize postboxes (wp core script) and metabox @@ -47,17 +49,11 @@ export default function MetaBoxes( { location } ) { window.postboxes.add_postbox_toggles( postType ); } - let wasSavingPost = select( editorStore ).isSavingPost(); - let wasAutosavingPost = select( editorStore ).isAutosavingPost(); - const hasActiveMetaBoxes = select( editPostStore ).hasMetaBoxes(); + let wasSavingPost = isSavingPost(); + let wasAutosavingPost = isAutosavingPost(); // Save metaboxes when performing a full save on the post. subscribe( () => { - const isSavingPost = select( editorStore ).isSavingPost(); - const isAutosavingPost = select( - editorStore - ).isAutosavingPost(); - // Save metaboxes on save completion, except for autosaves that are not a post preview. // // Meta boxes are initialized once at page load. It is not necessary to @@ -65,14 +61,14 @@ export default function MetaBoxes( { location } ) { // // See: https://github.com/WordPress/WordPress/blob/5.1.1/wp-admin/includes/post.php#L2307-L2309 const shouldTriggerMetaboxesSave = - hasActiveMetaBoxes && + hasMetaBoxes() && wasSavingPost && - ! isSavingPost && + ! isSavingPost() && ! wasAutosavingPost; // Save current state for next inspection. - wasSavingPost = isSavingPost; - wasAutosavingPost = isAutosavingPost; + wasSavingPost = isSavingPost(); + wasAutosavingPost = isAutosavingPost(); if ( shouldTriggerMetaboxesSave ) { dispatch( editPostStore ).requestMetaBoxUpdates(); From 4f67de1a31d05829b0b46cf2d899519a6a8d8bde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ribari=C4=87?= Date: Sun, 29 Aug 2021 14:56:32 +0200 Subject: [PATCH 11/18] Use useRef instead of useState for initialized variable --- .../src/components/meta-boxes/index.js | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/packages/edit-post/src/components/meta-boxes/index.js b/packages/edit-post/src/components/meta-boxes/index.js index e191470d097260..98d6bbbcb887c0 100644 --- a/packages/edit-post/src/components/meta-boxes/index.js +++ b/packages/edit-post/src/components/meta-boxes/index.js @@ -8,7 +8,7 @@ import { map } from 'lodash'; */ import { useSelect, subscribe, dispatch } from '@wordpress/data'; import { store as editorStore } from '@wordpress/editor'; -import { useEffect, useState } from '@wordpress/element'; +import { useEffect, useRef } from '@wordpress/element'; /** * Internal dependencies @@ -18,7 +18,7 @@ import MetaBoxVisibility from './meta-box-visibility'; import { store as editPostStore } from '../../store'; export default function MetaBoxes( { location } ) { - const { isReady, metaBoxes, isVisible, postType } = useSelect( + const { isEditorReady, metaBoxes, isVisible, postType } = useSelect( ( select ) => { const { __unstableIsEditorReady, getCurrentPostType } = select( editorStore @@ -28,7 +28,7 @@ export default function MetaBoxes( { location } ) { getMetaBoxesPerLocation, } = select( editPostStore ); return { - isReady: __unstableIsEditorReady(), + isEditorReady: __unstableIsEditorReady(), metaBoxes: getMetaBoxesPerLocation( location ), isVisible: isMetaBoxLocationVisible( location ), postType: getCurrentPostType(), @@ -37,13 +37,17 @@ export default function MetaBoxes( { location } ) { ); const { isSavingPost, isAutosavingPost } = useSelect( editorStore ); const { hasMetaBoxes } = useSelect( editPostStore ); - const [ initialized, setInitialized ] = useState( false ); + const initialized = useRef( false ); // When editor is ready, initialize postboxes (wp core script) and metabox // saving. useEffect( () => { - if ( postType !== undefined && isReady && ! initialized ) { - setInitialized( true ); + if ( + postType !== undefined && + isEditorReady && + ! initialized.current + ) { + initialized.current = true; if ( window.postboxes.page !== postType ) { window.postboxes.add_postbox_toggles( postType ); @@ -75,9 +79,9 @@ export default function MetaBoxes( { location } ) { } } ); } - }, [ isReady, postType, initialized ] ); + }, [ isEditorReady, postType ] ); - if ( ! isReady ) { + if ( ! isEditorReady ) { return null; } From 521d072e191cf6e259f28fbf71e2713c06d3935a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ribari=C4=87?= Date: Sun, 29 Aug 2021 15:02:19 +0200 Subject: [PATCH 12/18] Use useRegistry --- packages/edit-post/src/components/meta-boxes/index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/edit-post/src/components/meta-boxes/index.js b/packages/edit-post/src/components/meta-boxes/index.js index 98d6bbbcb887c0..1622d19ad96167 100644 --- a/packages/edit-post/src/components/meta-boxes/index.js +++ b/packages/edit-post/src/components/meta-boxes/index.js @@ -6,7 +6,7 @@ import { map } from 'lodash'; /** * WordPress dependencies */ -import { useSelect, subscribe, dispatch } from '@wordpress/data'; +import { useSelect, useRegistry } from '@wordpress/data'; import { store as editorStore } from '@wordpress/editor'; import { useEffect, useRef } from '@wordpress/element'; @@ -18,6 +18,7 @@ import MetaBoxVisibility from './meta-box-visibility'; import { store as editPostStore } from '../../store'; export default function MetaBoxes( { location } ) { + const registry = useRegistry(); const { isEditorReady, metaBoxes, isVisible, postType } = useSelect( ( select ) => { const { __unstableIsEditorReady, getCurrentPostType } = select( @@ -57,7 +58,7 @@ export default function MetaBoxes( { location } ) { let wasAutosavingPost = isAutosavingPost(); // Save metaboxes when performing a full save on the post. - subscribe( () => { + registry.subscribe( () => { // Save metaboxes on save completion, except for autosaves that are not a post preview. // // Meta boxes are initialized once at page load. It is not necessary to @@ -75,7 +76,7 @@ export default function MetaBoxes( { location } ) { wasAutosavingPost = isAutosavingPost(); if ( shouldTriggerMetaboxesSave ) { - dispatch( editPostStore ).requestMetaBoxUpdates(); + registry.dispatch( editPostStore ).requestMetaBoxUpdates(); } } ); } From 9f19aecd974e9cec14f1d43c23718fa16307f3c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ribari=C4=87?= Date: Sun, 29 Aug 2021 15:07:10 +0200 Subject: [PATCH 13/18] Add location dependency --- packages/edit-post/src/components/meta-boxes/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/edit-post/src/components/meta-boxes/index.js b/packages/edit-post/src/components/meta-boxes/index.js index 1622d19ad96167..b286bae9a74c44 100644 --- a/packages/edit-post/src/components/meta-boxes/index.js +++ b/packages/edit-post/src/components/meta-boxes/index.js @@ -34,7 +34,8 @@ export default function MetaBoxes( { location } ) { isVisible: isMetaBoxLocationVisible( location ), postType: getCurrentPostType(), }; - } + }, + [ location ] ); const { isSavingPost, isAutosavingPost } = useSelect( editorStore ); const { hasMetaBoxes } = useSelect( editPostStore ); From 361c2afcb8e18741ad72de00697c074123db13c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ribari=C4=87?= Date: Sun, 29 Aug 2021 17:18:33 +0200 Subject: [PATCH 14/18] Meta boxes must be initialized only once --- .../data/data-core-edit-post.md | 16 +++++ .../src/components/meta-boxes/index.js | 60 +++-------------- packages/edit-post/src/store/actions.js | 65 ++++++++++++++++++- packages/edit-post/src/store/reducer.js | 17 +++++ packages/edit-post/src/store/selectors.js | 11 ++++ 5 files changed, 118 insertions(+), 51 deletions(-) diff --git a/docs/reference-guides/data/data-core-edit-post.md b/docs/reference-guides/data/data-core-edit-post.md index e8dd31780a3ad6..2946889a704b82 100644 --- a/docs/reference-guides/data/data-core-edit-post.md +++ b/docs/reference-guides/data/data-core-edit-post.md @@ -311,6 +311,18 @@ _Returns_ - `boolean`: Whether the metaboxes are being saved. +### metaBoxesInitialized + +Returns true if meta boxes are initialized. + +_Parameters_ + +- _state_ `Object`: Global application state. + +_Returns_ + +- `boolean`: Whether meta boxes are initialized. + ## Actions @@ -351,6 +363,10 @@ _Returns_ - `Object`: Action object. +### initializeMetaBoxes + +Undocumented declaration. + ### metaBoxUpdatesFailure Returns an action object used to signal a failed meta box update. diff --git a/packages/edit-post/src/components/meta-boxes/index.js b/packages/edit-post/src/components/meta-boxes/index.js index b286bae9a74c44..352ed9cad2998e 100644 --- a/packages/edit-post/src/components/meta-boxes/index.js +++ b/packages/edit-post/src/components/meta-boxes/index.js @@ -7,8 +7,7 @@ import { map } from 'lodash'; * WordPress dependencies */ import { useSelect, useRegistry } from '@wordpress/data'; -import { store as editorStore } from '@wordpress/editor'; -import { useEffect, useRef } from '@wordpress/element'; +import { useEffect } from '@wordpress/element'; /** * Internal dependencies @@ -19,71 +18,32 @@ import { store as editPostStore } from '../../store'; export default function MetaBoxes( { location } ) { const registry = useRegistry(); - const { isEditorReady, metaBoxes, isVisible, postType } = useSelect( + const { metaBoxes, isVisible, metaBoxesInitialized } = useSelect( ( select ) => { - const { __unstableIsEditorReady, getCurrentPostType } = select( - editorStore - ); const { isMetaBoxLocationVisible, getMetaBoxesPerLocation, + metaBoxesInitialized: _metaBoxesInitialized, } = select( editPostStore ); return { - isEditorReady: __unstableIsEditorReady(), metaBoxes: getMetaBoxesPerLocation( location ), isVisible: isMetaBoxLocationVisible( location ), - postType: getCurrentPostType(), + metaBoxesInitialized: _metaBoxesInitialized(), }; }, [ location ] ); - const { isSavingPost, isAutosavingPost } = useSelect( editorStore ); - const { hasMetaBoxes } = useSelect( editPostStore ); - const initialized = useRef( false ); // When editor is ready, initialize postboxes (wp core script) and metabox - // saving. + // saving. This initializes all meta box locations, not just this specific + // one. useEffect( () => { - if ( - postType !== undefined && - isEditorReady && - ! initialized.current - ) { - initialized.current = true; - - if ( window.postboxes.page !== postType ) { - window.postboxes.add_postbox_toggles( postType ); - } - - let wasSavingPost = isSavingPost(); - let wasAutosavingPost = isAutosavingPost(); - - // Save metaboxes when performing a full save on the post. - registry.subscribe( () => { - // Save metaboxes on save completion, except for autosaves that are not a post preview. - // - // Meta boxes are initialized once at page load. It is not necessary to - // account for updates on each state change. - // - // See: https://github.com/WordPress/WordPress/blob/5.1.1/wp-admin/includes/post.php#L2307-L2309 - const shouldTriggerMetaboxesSave = - hasMetaBoxes() && - wasSavingPost && - ! isSavingPost() && - ! wasAutosavingPost; - - // Save current state for next inspection. - wasSavingPost = isSavingPost(); - wasAutosavingPost = isAutosavingPost(); - - if ( shouldTriggerMetaboxesSave ) { - registry.dispatch( editPostStore ).requestMetaBoxUpdates(); - } - } ); + if ( ! metaBoxesInitialized ) { + registry.dispatch( editPostStore ).initializeMetaBoxes(); } - }, [ isEditorReady, postType ] ); + }, [ metaBoxesInitialized ] ); - if ( ! isEditorReady ) { + if ( ! metaBoxesInitialized ) { return null; } diff --git a/packages/edit-post/src/store/actions.js b/packages/edit-post/src/store/actions.js index a7672629675468..001bd82fdce676 100644 --- a/packages/edit-post/src/store/actions.js +++ b/packages/edit-post/src/store/actions.js @@ -9,7 +9,7 @@ import { castArray, reduce } from 'lodash'; import { __ } from '@wordpress/i18n'; import { apiFetch } from '@wordpress/data-controls'; import { store as interfaceStore } from '@wordpress/interface'; -import { controls } from '@wordpress/data'; +import { controls, select, subscribe, dispatch } from '@wordpress/data'; import { speak } from '@wordpress/a11y'; import { store as noticesStore } from '@wordpress/notices'; import { store as coreStore } from '@wordpress/core-data'; @@ -483,3 +483,66 @@ export function* __unstableCreateTemplate( template ) { } ); } + +let metaBoxesInitialized = false; + +export function* initializeMetaBoxes() { + const isEditorReady = yield controls.select( + editorStore, + '__unstableIsEditorReady' + ); + + if ( ! isEditorReady ) { + return; + } + + const postType = yield controls.select( editorStore, 'getCurrentPostType' ); + + // Only initialize once. + if ( metaBoxesInitialized ) { + return; + } + + if ( window.postboxes.page !== postType ) { + window.postboxes.add_postbox_toggles( postType ); + } + + metaBoxesInitialized = true; + + let wasSavingPost = yield controls.select( editorStore, 'isSavingPost' ); + let wasAutosavingPost = yield controls.select( + editorStore, + 'isAutosavingPost' + ); + const hasMetaBoxes = yield controls.select( editPostStore, 'hasMetaBoxes' ); + + // Save metaboxes when performing a full save on the post. + subscribe( () => { + const isSavingPost = select( editorStore ).isSavingPost(); + const isAutosavingPost = select( editorStore ).isAutosavingPost(); + + // Save metaboxes on save completion, except for autosaves that are not a post preview. + // + // Meta boxes are initialized once at page load. It is not necessary to + // account for updates on each state change. + // + // See: https://github.com/WordPress/WordPress/blob/5.1.1/wp-admin/includes/post.php#L2307-L2309 + const shouldTriggerMetaboxesSave = + hasMetaBoxes && + wasSavingPost && + ! isSavingPost && + ! wasAutosavingPost; + + // Save current state for next inspection. + wasSavingPost = isSavingPost; + wasAutosavingPost = isAutosavingPost; + + if ( shouldTriggerMetaboxesSave ) { + dispatch( editPostStore ).requestMetaBoxUpdates(); + } + } ); + + return { + type: 'META_BOXES_INITIALIZED', + }; +} diff --git a/packages/edit-post/src/store/reducer.js b/packages/edit-post/src/store/reducer.js index 3441c613f5f619..d3eaa38fac7c06 100644 --- a/packages/edit-post/src/store/reducer.js +++ b/packages/edit-post/src/store/reducer.js @@ -281,9 +281,26 @@ function isEditingTemplate( state = false, action ) { return state; } +/** + * Reducer tracking whether meta boxes are initialized. + * + * @param {boolean} state + * @param {Object} action + * + * @return {boolean} Updated state. + */ +function metaBoxesInitialized( state = false, action ) { + switch ( action.type ) { + case 'META_BOXES_INITIALIZED': + return true; + } + return state; +} + const metaBoxes = combineReducers( { isSaving: isSavingMetaBoxes, locations: metaBoxLocations, + initialized: metaBoxesInitialized, } ); export default combineReducers( { diff --git a/packages/edit-post/src/store/selectors.js b/packages/edit-post/src/store/selectors.js index 2d40810a3cf569..97030bd3d93525 100644 --- a/packages/edit-post/src/store/selectors.js +++ b/packages/edit-post/src/store/selectors.js @@ -367,6 +367,17 @@ export function isEditingTemplate( state ) { return state.isEditingTemplate; } +/** + * Returns true if meta boxes are initialized. + * + * @param {Object} state Global application state. + * + * @return {boolean} Whether meta boxes are initialized. + */ +export function metaBoxesInitialized( state ) { + return state.metaBoxes.initialized; +} + /** * Retrieves the template of the currently edited post. * From 342361c13b945eaa4a9d28314e413c0c5055d92f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ribari=C4=87?= Date: Sun, 29 Aug 2021 19:31:36 +0200 Subject: [PATCH 15/18] Better dispatch check --- .../edit-post/src/components/meta-boxes/index.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/edit-post/src/components/meta-boxes/index.js b/packages/edit-post/src/components/meta-boxes/index.js index 352ed9cad2998e..df3c6d835f75ca 100644 --- a/packages/edit-post/src/components/meta-boxes/index.js +++ b/packages/edit-post/src/components/meta-boxes/index.js @@ -8,6 +8,7 @@ import { map } from 'lodash'; */ import { useSelect, useRegistry } from '@wordpress/data'; import { useEffect } from '@wordpress/element'; +import { store as editorStore } from '@wordpress/editor'; /** * Internal dependencies @@ -18,8 +19,14 @@ import { store as editPostStore } from '../../store'; export default function MetaBoxes( { location } ) { const registry = useRegistry(); - const { metaBoxes, isVisible, metaBoxesInitialized } = useSelect( + const { + metaBoxes, + isVisible, + metaBoxesInitialized, + isEditorReady, + } = useSelect( ( select ) => { + const { __unstableIsEditorReady } = select( editorStore ); const { isMetaBoxLocationVisible, getMetaBoxesPerLocation, @@ -29,6 +36,7 @@ export default function MetaBoxes( { location } ) { metaBoxes: getMetaBoxesPerLocation( location ), isVisible: isMetaBoxLocationVisible( location ), metaBoxesInitialized: _metaBoxesInitialized(), + isEditorReady: __unstableIsEditorReady(), }; }, [ location ] @@ -38,10 +46,10 @@ export default function MetaBoxes( { location } ) { // saving. This initializes all meta box locations, not just this specific // one. useEffect( () => { - if ( ! metaBoxesInitialized ) { + if ( isEditorReady ) { registry.dispatch( editPostStore ).initializeMetaBoxes(); } - }, [ metaBoxesInitialized ] ); + }, [ isEditorReady ] ); if ( ! metaBoxesInitialized ) { return null; From 0d362b85c1c21b69bb4009666ff044da45ea7fcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ribari=C4=87?= Date: Sun, 29 Aug 2021 20:14:41 +0200 Subject: [PATCH 16/18] Add documentation for new action --- docs/reference-guides/data/data-core-edit-post.md | 2 +- packages/edit-post/src/store/actions.js | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/reference-guides/data/data-core-edit-post.md b/docs/reference-guides/data/data-core-edit-post.md index 2946889a704b82..8e3f10255edb55 100644 --- a/docs/reference-guides/data/data-core-edit-post.md +++ b/docs/reference-guides/data/data-core-edit-post.md @@ -365,7 +365,7 @@ _Returns_ ### initializeMetaBoxes -Undocumented declaration. +Initializes WordPress `postboxes` script and the logic for saving meta boxes. ### metaBoxUpdatesFailure diff --git a/packages/edit-post/src/store/actions.js b/packages/edit-post/src/store/actions.js index 001bd82fdce676..23d7c4613de593 100644 --- a/packages/edit-post/src/store/actions.js +++ b/packages/edit-post/src/store/actions.js @@ -486,6 +486,9 @@ export function* __unstableCreateTemplate( template ) { let metaBoxesInitialized = false; +/** + * Initializes WordPress `postboxes` script and the logic for saving meta boxes. + */ export function* initializeMetaBoxes() { const isEditorReady = yield controls.select( editorStore, From 1d64a78ee57e0fd7e40ab766c8dd35bb424b95de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ribari=C4=87?= Date: Sun, 29 Aug 2021 20:19:44 +0200 Subject: [PATCH 17/18] Better dispatch check --- packages/edit-post/src/components/meta-boxes/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/edit-post/src/components/meta-boxes/index.js b/packages/edit-post/src/components/meta-boxes/index.js index df3c6d835f75ca..4e5dfac362ae85 100644 --- a/packages/edit-post/src/components/meta-boxes/index.js +++ b/packages/edit-post/src/components/meta-boxes/index.js @@ -46,10 +46,10 @@ export default function MetaBoxes( { location } ) { // saving. This initializes all meta box locations, not just this specific // one. useEffect( () => { - if ( isEditorReady ) { + if ( isEditorReady && ! metaBoxesInitialized ) { registry.dispatch( editPostStore ).initializeMetaBoxes(); } - }, [ isEditorReady ] ); + }, [ isEditorReady, metaBoxesInitialized ] ); if ( ! metaBoxesInitialized ) { return null; From c96590a296141c6ecc3de36abb5f92a9aebac3d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Ribari=C4=87?= Date: Mon, 30 Aug 2021 10:27:16 +0200 Subject: [PATCH 18/18] Rename selector --- .../data/data-core-edit-post.md | 24 +++++++++---------- .../src/components/meta-boxes/index.js | 12 +++++----- packages/edit-post/src/store/selectors.js | 2 +- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/docs/reference-guides/data/data-core-edit-post.md b/docs/reference-guides/data/data-core-edit-post.md index 8e3f10255edb55..72a76d56fad15f 100644 --- a/docs/reference-guides/data/data-core-edit-post.md +++ b/docs/reference-guides/data/data-core-edit-post.md @@ -6,6 +6,18 @@ Namespace: `core/edit-post`. +### areMetaBoxesInitialized + +Returns true if meta boxes are initialized. + +_Parameters_ + +- _state_ `Object`: Global application state. + +_Returns_ + +- `boolean`: Whether meta boxes are initialized. + ### getActiveGeneralSidebarName Returns the current active general sidebar name, or null if there is no @@ -311,18 +323,6 @@ _Returns_ - `boolean`: Whether the metaboxes are being saved. -### metaBoxesInitialized - -Returns true if meta boxes are initialized. - -_Parameters_ - -- _state_ `Object`: Global application state. - -_Returns_ - -- `boolean`: Whether meta boxes are initialized. - ## Actions diff --git a/packages/edit-post/src/components/meta-boxes/index.js b/packages/edit-post/src/components/meta-boxes/index.js index 4e5dfac362ae85..15851ae5247873 100644 --- a/packages/edit-post/src/components/meta-boxes/index.js +++ b/packages/edit-post/src/components/meta-boxes/index.js @@ -22,7 +22,7 @@ export default function MetaBoxes( { location } ) { const { metaBoxes, isVisible, - metaBoxesInitialized, + areMetaBoxesInitialized, isEditorReady, } = useSelect( ( select ) => { @@ -30,12 +30,12 @@ export default function MetaBoxes( { location } ) { const { isMetaBoxLocationVisible, getMetaBoxesPerLocation, - metaBoxesInitialized: _metaBoxesInitialized, + areMetaBoxesInitialized: _areMetaBoxesInitialized, } = select( editPostStore ); return { metaBoxes: getMetaBoxesPerLocation( location ), isVisible: isMetaBoxLocationVisible( location ), - metaBoxesInitialized: _metaBoxesInitialized(), + areMetaBoxesInitialized: _areMetaBoxesInitialized(), isEditorReady: __unstableIsEditorReady(), }; }, @@ -46,12 +46,12 @@ export default function MetaBoxes( { location } ) { // saving. This initializes all meta box locations, not just this specific // one. useEffect( () => { - if ( isEditorReady && ! metaBoxesInitialized ) { + if ( isEditorReady && ! areMetaBoxesInitialized ) { registry.dispatch( editPostStore ).initializeMetaBoxes(); } - }, [ isEditorReady, metaBoxesInitialized ] ); + }, [ isEditorReady, areMetaBoxesInitialized ] ); - if ( ! metaBoxesInitialized ) { + if ( ! areMetaBoxesInitialized ) { return null; } diff --git a/packages/edit-post/src/store/selectors.js b/packages/edit-post/src/store/selectors.js index 97030bd3d93525..e127f6294210a8 100644 --- a/packages/edit-post/src/store/selectors.js +++ b/packages/edit-post/src/store/selectors.js @@ -374,7 +374,7 @@ export function isEditingTemplate( state ) { * * @return {boolean} Whether meta boxes are initialized. */ -export function metaBoxesInitialized( state ) { +export function areMetaBoxesInitialized( state ) { return state.metaBoxes.initialized; }