Skip to content

Commit

Permalink
[Core Commands]: Pass options to useCommands
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Jul 26, 2023
1 parent 1503530 commit df307a2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 24 deletions.
20 changes: 6 additions & 14 deletions packages/core-commands/src/admin-navigation-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,14 @@ import { unlock } from './lock-unlock';

const { useHistory } = unlock( routerPrivateApis );

export function useAdminNavigationCommands() {
export function useAdminNavigationCommands( options ) {
const history = useHistory();
const { isBlockTheme } = options;

const { isBlockTheme, canAccessSiteEditor } = useSelect( ( select ) => {
return {
isBlockTheme:
// To avoid making core-commands dependent on block-editor using store string literal name.
// eslint-disable-next-line @wordpress/data-no-store-string-literals
select( 'core/block-editor' )?.getSettings()
.__unstableIsBlockBasedTheme,
canAccessSiteEditor: select( coreStore ).canUser(
'read',
'templates'
),
};
}, [] );
const canAccessSiteEditor = useSelect(
( select ) => select( coreStore ).canUser( 'read', 'templates' ),
[]
);

const isSiteEditor = getPath( window.location.href )?.includes(
'site-editor.php'
Expand Down
12 changes: 9 additions & 3 deletions packages/core-commands/src/private-apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@ import { useAdminNavigationCommands } from './admin-navigation-commands';
import { useSiteEditorNavigationCommands } from './site-editor-navigation-commands';
import { lock } from './lock-unlock';

function useCommands() {
useAdminNavigationCommands();
useSiteEditorNavigationCommands();
/**
* @typedef CommandOptions
*
* @property {boolean} [isBlockTheme] Whether the current theme is a block theme.
*/

function useCommands( options = {} ) {
useAdminNavigationCommands( options );
useSiteEditorNavigationCommands( options );
}

export const privateApis = {};
Expand Down
11 changes: 7 additions & 4 deletions packages/core-commands/src/site-editor-navigation-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,16 @@ const useTemplateNavigationCommandLoader =
const useTemplatePartNavigationCommandLoader =
getNavigationCommandLoaderPerPostType( 'wp_template_part' );

function useSiteEditorBasicNavigationCommands() {
function useSiteEditorBasicNavigationCommands( options ) {
const history = useHistory();
const isSiteEditor = getPath( window.location.href )?.includes(
'site-editor.php'
);
const commands = useMemo( () => {
const result = [];
if ( ! options.isBlockTheme ) {
return result;
}
result.push( {
name: 'core/edit-site/open-navigation',
label: __( 'Open navigation' ),
Expand Down Expand Up @@ -198,15 +201,15 @@ function useSiteEditorBasicNavigationCommands() {
} );

return result;
}, [ history, isSiteEditor ] );
}, [ history, isSiteEditor, options.isBlockTheme ] );

return {
commands,
isLoading: false,
};
}

export function useSiteEditorNavigationCommands() {
export function useSiteEditorNavigationCommands( options ) {
useCommandLoader( {
name: 'core/edit-site/navigate-pages',
hook: usePageNavigationCommandLoader,
Expand All @@ -225,7 +228,7 @@ export function useSiteEditorNavigationCommands() {
} );
useCommandLoader( {
name: 'core/edit-site/basic-navigation',
hook: useSiteEditorBasicNavigationCommands,
hook: useSiteEditorBasicNavigationCommands.bind( null, options ),
context: 'site-editor',
} );
}
9 changes: 8 additions & 1 deletion packages/edit-post/src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import { store as blocksStore } from '@wordpress/blocks';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { useSelect, useDispatch } from '@wordpress/data';
import {
ErrorBoundary,
Expand Down Expand Up @@ -30,8 +31,14 @@ const { ExperimentalEditorProvider } = unlock( editorPrivateApis );
const { useCommands } = unlock( coreCommandsPrivateApis );

function Editor( { postId, postType, settings, initialEdits, ...props } ) {
useCommands();
useCommonCommands();
const isBlockTheme = useSelect(
( select ) =>
select( blockEditorStore ).getSettings()
.__unstableIsBlockBasedTheme,
[]
);
useCommands( { isBlockTheme } );
const {
hasFixedToolbar,
focusMode,
Expand Down
13 changes: 11 additions & 2 deletions packages/edit-site/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ import {
privateApis as commandsPrivateApis,
} from '@wordpress/commands';
import { store as preferencesStore } from '@wordpress/preferences';
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';
import {
store as blockEditorStore,
privateApis as blockEditorPrivateApis,
} from '@wordpress/block-editor';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { privateApis as coreCommandsPrivateApis } from '@wordpress/core-commands';

Expand Down Expand Up @@ -63,7 +66,13 @@ export default function Layout() {
// This ensures the edited entity id and type are initialized properly.
useInitEditedEntityFromURL();
useSyncCanvasModeWithURL();
useCommands();
const isBlockTheme = useSelect(
( select ) =>
select( blockEditorStore ).getSettings()
.__unstableIsBlockBasedTheme,
[]
);
useCommands( { isBlockTheme } );
useEditModeCommands();
useCommonCommands();

Expand Down

0 comments on commit df307a2

Please sign in to comment.