diff --git a/packages/editor/src/components/commands/index.js b/packages/editor/src/components/commands/index.js index 2ed21fc832a083..9ab3ee7190f285 100644 --- a/packages/editor/src/components/commands/index.js +++ b/packages/editor/src/components/commands/index.js @@ -221,27 +221,6 @@ const getEditorCommandLoader = () => }, } ); -function useEditedEntityContextualCommands() { - const { postType, isViewable, status, link } = useSelect( ( select ) => { - const { - getCurrentPostType, - getEditedPostAttribute, - getEditedPostPreviewLink, - } = select( editorStore ); - const { getPostType } = select( coreStore ); - const isPublished = getEditedPostAttribute( 'status' ) === 'publish'; - const _postType = getCurrentPostType(); - return { - postType: _postType, - isViewable: getPostType( _postType )?.viewable ?? false, - status: getEditedPostAttribute( 'status' ), - link: isPublished - ? getEditedPostAttribute( 'link' ) - : getEditedPostPreviewLink?.() ?? '', - }; - }, [] ); - const { openModal } = useDispatch( interfaceStore ); - const commands = []; commands.push( { name: 'core/open-settings-sidebar', label: __( 'Show or hide the Settings panel.' ), @@ -292,31 +271,6 @@ function useEditedEntityContextualCommands() { ); }, } ); - } - if ( isViewable ) { - if ( postType === 'post' || postType === 'page' ) { - const isPage = postType === 'page'; - let label; - - if ( status === 'publish' ) { - label = isPage ? __( 'View page' ) : __( 'View post' ); - } else { - label = isPage ? __( 'Preview page' ) : __( 'Preview post' ); - } - - commands.push( { - name: 'core/view-link', - label, - icon: external, - callback: ( { close } ) => { - close(); - if ( link ) { - window.open( link, '_blank' ); - } - }, - } ); - } - } if ( isViewable ) { commands.push( { @@ -355,12 +309,28 @@ function useEditedEntityContextualCommands() { const getEditedEntityContextualCommands = () => function useEditedEntityContextualCommands() { - const { postType } = useSelect( ( select ) => { - const { getCurrentPostType } = select( editorStore ); - return { - postType: getCurrentPostType(), - }; - }, [] ); + const { postType, isViewable, status, link } = useSelect( + ( select ) => { + const { + getCurrentPostType, + getEditedPostAttribute, + getEditedPostPreviewLink, + } = select( editorStore ); + const { getPostType } = select( coreStore ); + const isPublished = + getEditedPostAttribute( 'status' ) === 'publish'; + const _postType = getCurrentPostType(); + return { + postType: _postType, + isViewable: getPostType( _postType )?.viewable ?? false, + status: getEditedPostAttribute( 'status' ), + link: isPublished + ? getEditedPostAttribute( 'link' ) + : getEditedPostPreviewLink?.() ?? '', + }; + }, + [] + ); const { openModal } = useDispatch( interfaceStore ); const commands = []; @@ -384,6 +354,32 @@ const getEditedEntityContextualCommands = () => }, } ); } + if ( isViewable ) { + if ( postType === 'post' || postType === 'page' ) { + const isPage = postType === 'page'; + let label; + + if ( status === 'publish' ) { + label = isPage ? __( 'View page' ) : __( 'View post' ); + } else { + label = isPage + ? __( 'Preview page' ) + : __( 'Preview post' ); + } + + commands.push( { + name: 'core/view-link', + label, + icon: external, + callback: ( { close } ) => { + close(); + if ( link ) { + window.open( link, '_blank' ); + } + }, + } ); + } + } return { isLoading: false, commands }; };