Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 47 additions & 22 deletions packages/core-commands/src/site-editor-navigation-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,23 +280,53 @@ const getSiteEditorBasicNavigationCommands = () =>
const isSiteEditor = getPath( window.location.href )?.includes(
'site-editor.php'
);
const { isBlockBasedTheme, canCreateTemplate } = useSelect(
( select ) => {
const { isSupportEditorStyles, isBlockBasedTheme, canCreateTemplate } =
useSelect( ( select ) => {
return {
isSupportEditorStyles:
select( coreStore ).getCurrentTheme().theme_supports[
'editor-styles'
],
isBlockBasedTheme:
select( coreStore ).getCurrentTheme()?.is_block_theme,
canCreateTemplate: select( coreStore ).canUser( 'create', {
kind: 'postType',
name: 'wp_template',
} ),
};
},
[]
);
}, [] );
const commands = useMemo( () => {
const result = [];

if ( canCreateTemplate && isBlockBasedTheme ) {
result.push( {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I believe, there's a minor scope for refactoring this useMemo implementation. We can push patterns to results immediately after its declaration and bail out early if ! canCreateTemplate is true. This should remove one layer of nesting and improve overall readability.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @yogeshbhutkar,
Thank you for the suggestion! I’ve simplified the code and applied early return logic to improve clarity.

name: 'core/edit-site/open-patterns',
label: __( 'Patterns' ),
icon: symbol,
callback: ( { close } ) => {
if ( canCreateTemplate ) {
if ( isSiteEditor ) {
history.navigate( '/pattern' );
} else {
document.location = addQueryArgs(
'site-editor.php',
{
p: '/pattern',
}
);
}
close();
} else {
// If a user cannot access the site editor
document.location.href = 'edit.php?post_type=wp_block';
}
},
} );

if ( ! canCreateTemplate ) {
return result;
}

if ( isBlockBasedTheme ) {
result.push( {
name: 'core/edit-site/open-navigation',
label: __( 'Navigation' ),
Expand Down Expand Up @@ -372,31 +402,26 @@ const getSiteEditorBasicNavigationCommands = () =>
close();
},
} );
}

result.push( {
name: 'core/edit-site/open-patterns',
label: __( 'Patterns' ),
icon: symbol,
callback: ( { close } ) => {
if ( canCreateTemplate ) {
} else if ( isSupportEditorStyles ) {
result.push( {
name: 'core/edit-site/open-stylebook',
label: __( 'Stylebook' ),
icon: styles,
callback: ( { close } ) => {
if ( isSiteEditor ) {
history.navigate( '/pattern' );
history.navigate( '/stylebook' );
} else {
document.location = addQueryArgs(
'site-editor.php',
{
p: '/pattern',
p: '/stylebook',
}
);
}
close();
} else {
// If a user cannot access the site editor
document.location.href = 'edit.php?post_type=wp_block';
}
},
} );
},
} );
}

return result;
}, [ history, isSiteEditor, canCreateTemplate, isBlockBasedTheme ] );
Expand Down
24 changes: 17 additions & 7 deletions packages/editor/src/components/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,22 @@ const getEditorCommandLoader = () =>
const { openModal, enableComplementaryArea, disableComplementaryArea } =
useDispatch( interfaceStore );
const { getCurrentPostId } = useSelect( editorStore );
const { isBlockBasedTheme, canCreateTemplate } = useSelect(
( select ) => {
const { isSupportEditorStyles, isBlockBasedTheme, canCreateTemplate } =
useSelect( ( select ) => {
return {
isSupportEditorStyles:
select( coreStore ).getCurrentTheme().theme_supports[
'editor-styles'
],
isBlockBasedTheme:
select( coreStore ).getCurrentTheme()?.is_block_theme,
canCreateTemplate: select( coreStore ).canUser( 'create', {
kind: 'postType',
name: 'wp_template',
} ),
};
},
[]
);
}, [] );

const allowSwitchEditorMode =
isCodeEditingEnabled && isRichEditingEnabled;

Expand Down Expand Up @@ -285,14 +288,21 @@ const getEditorCommandLoader = () =>
},
} );
}
if ( canCreateTemplate && isBlockBasedTheme ) {
if (
canCreateTemplate &&
( isBlockBasedTheme || isSupportEditorStyles )
) {
const label = isBlockBasedTheme
? __( 'Open Site Editor' )
: __( 'Open Design' );

const isSiteEditor = getPath( window.location.href )?.includes(
'site-editor.php'
);
if ( ! isSiteEditor ) {
commands.push( {
name: 'core/go-to-site-editor',
label: __( 'Open Site Editor' ),
label,
callback: ( { close } ) => {
close();
document.location = 'site-editor.php';
Expand Down
Loading