Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only show Page Content Focus commands when in edit mode #51888

Merged
Merged
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
91 changes: 53 additions & 38 deletions packages/edit-site/src/hooks/commands/use-edit-mode-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,51 +30,63 @@ import { unlock } from '../../lock-unlock';

const { useHistory } = unlock( routerPrivateApis );

function useEditModeCommandLoader() {
const { isLoaded, record: template } = useEditedEntityRecord();
const { removeTemplate, revertTemplate, setHasPageContentFocus } =
useDispatch( editSiteStore );
const history = useHistory();
const { isPage, hasPageContentFocus } = useSelect(
function usePageContentFocusCommands() {
const { isPage, canvasMode, hasPageContentFocus } = useSelect(
( select ) => ( {
isPage: select( editSiteStore ).isPage(),
canvasMode: unlock( select( editSiteStore ) ).getCanvasMode(),
hasPageContentFocus: select( editSiteStore ).hasPageContentFocus(),
} ),
[]
);
const { setHasPageContentFocus } = useDispatch( editSiteStore );

if ( ! isLoaded ) {
return { isLoading: true, commands: [] };
if ( ! isPage || canvasMode !== 'edit' ) {
return { isLoading: false, commands: [] };
}

const commands = [];

if ( isPage ) {
if ( hasPageContentFocus ) {
commands.push( {
name: 'core/switch-to-template-focus',
label: __( 'Edit template' ),
icon: layout,
context: 'site-editor-edit',
callback: ( { close } ) => {
setHasPageContentFocus( false );
close();
},
} );
} else {
commands.push( {
name: 'core/switch-to-page-focus',
label: __( 'Back to page' ),
icon: page,
context: 'site-editor-edit',
callback: ( { close } ) => {
setHasPageContentFocus( true );
close();
},
} );
}
if ( hasPageContentFocus ) {
commands.push( {
name: 'core/switch-to-template-focus',
label: __( 'Edit template' ),
icon: layout,
callback: ( { close } ) => {
setHasPageContentFocus( false );
close();
},
} );
} else {
commands.push( {
name: 'core/switch-to-page-focus',
label: __( 'Back to page' ),
icon: page,
callback: ( { close } ) => {
setHasPageContentFocus( true );
close();
},
} );
}

return { isLoading: false, commands };
}

function useManipulateDocumentCommands() {
const { isLoaded, record: template } = useEditedEntityRecord();
const { removeTemplate, revertTemplate } = useDispatch( editSiteStore );
const history = useHistory();
const hasPageContentFocus = useSelect(
( select ) => select( editSiteStore ).hasPageContentFocus(),
[]
);

if ( ! isLoaded ) {
return { isLoading: true, commands: [] };
}

const commands = [];

if ( isTemplateRevertable( template ) && ! hasPageContentFocus ) {
const label =
template.type === 'wp_template'
Expand All @@ -100,7 +112,6 @@ function useEditModeCommandLoader() {
name: 'core/remove-template',
label,
icon: trash,
context: 'site-editor-edit',
callback: ( { close } ) => {
removeTemplate( template );
// Navigate to the template list
Expand All @@ -118,13 +129,11 @@ function useEditModeCommandLoader() {
};
}

function useEditUICommandLoader() {
function useEditUICommands() {
const { openGeneralSidebar, closeGeneralSidebar, switchEditorMode } =
useDispatch( editSiteStore );
const { canvasMode, editorMode, activeSidebar } = useSelect(
( select ) => ( {
isPage: select( editSiteStore ).isPage(),
hasPageContentFocus: select( editSiteStore ).hasPageContentFocus(),
canvasMode: unlock( select( editSiteStore ) ).getCanvasMode(),
editorMode: select( editSiteStore ).getEditorMode(),
activeSidebar: select( interfaceStore ).getActiveComplementaryArea(
Expand Down Expand Up @@ -206,14 +215,20 @@ function useEditUICommandLoader() {
}

export function useEditModeCommands() {
useCommandLoader( {
name: 'core/edit-site/page-content-focus',
hook: usePageContentFocusCommands,
context: 'site-editor-edit',
} );

useCommandLoader( {
name: 'core/edit-site/manipulate-document',
hook: useEditModeCommandLoader,
hook: useManipulateDocumentCommands,
context: 'site-editor-edit',
} );

useCommandLoader( {
name: 'core/edit-site/edit-ui',
hook: useEditUICommandLoader,
hook: useEditUICommands,
} );
}