Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Move command into core commands
  • Loading branch information
ramonjd committed Sep 19, 2024
commit 0bc7c1b5d498b6e50f7640eb20cc60e8b2aa9893
1 change: 1 addition & 0 deletions packages/core-commands/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@wordpress/html-entities": "file:../html-entities",
"@wordpress/i18n": "file:../i18n",
"@wordpress/icons": "file:../icons",
"@wordpress/notices": "file:../notices",
"@wordpress/private-apis": "file:../private-apis",
"@wordpress/router": "file:../router",
"@wordpress/url": "file:../url"
Expand Down
85 changes: 78 additions & 7 deletions packages/core-commands/src/admin-navigation-commands.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,83 @@
/**
* WordPress dependencies
*/
import { useCommand } from '@wordpress/commands';
import { useCommand, useCommandLoader } from '@wordpress/commands';
import { __ } from '@wordpress/i18n';
import { plus } from '@wordpress/icons';
import { getPath } from '@wordpress/url';
import { store as coreStore } from '@wordpress/core-data';
import { useDispatch } from '@wordpress/data';
import { useMemo } from '@wordpress/element';
import { store as noticesStore } from '@wordpress/notices';
import { privateApis as routerPrivateApis } from '@wordpress/router';

/**
* Internal dependencies
*/
import { unlock } from './lock-unlock';

const { useHistory } = unlock( routerPrivateApis );

function useAddNewPageCommand() {
const isSiteEditor = getPath( window.location.href )?.includes(
'site-editor.php'
);
const history = useHistory();
const { saveEntityRecord } = useDispatch( coreStore );
const { createErrorNotice } = useDispatch( noticesStore );

const createPageEntity = async ( { close } ) => {
try {
const page = await saveEntityRecord(
'postType',
'page',
{
status: 'draft',
},
{
throwOnError: true,
}
);
if ( page?.id ) {
history.push( {
postId: page.id,
postType: 'page',
canvas: 'edit',
} );
}
} catch ( error ) {
const errorMessage =
error.message && error.code !== 'unknown_error'
? error.message
: __( 'An error occurred while creating the item.' );

createErrorNotice( errorMessage, {
type: 'snackbar',
} );
} finally {
close();
}
};

const commands = useMemo( () => {
const addNewPage = isSiteEditor
? createPageEntity
: () => ( document.location.href = 'post-new.php?post_type=page' );
return [
{
name: 'core/add-new-page',
label: __( 'Add new page' ),
icon: plus,
callback: addNewPage,
},
];
}, [ createPageEntity, isSiteEditor ] );

return {
isLoading: false,
commands,
};
}

export function useAdminNavigationCommands() {
useCommand( {
Expand All @@ -14,12 +88,9 @@ export function useAdminNavigationCommands() {
document.location.href = 'post-new.php';
},
} );
useCommand( {

useCommandLoader( {
name: 'core/add-new-page',
label: __( 'Add new page' ),
icon: plus,
callback: () => {
document.location.href = 'post-new.php?post_type=page';
},
hook: useAddNewPageCommand,
} );
}
77 changes: 2 additions & 75 deletions packages/edit-site/src/hooks/commands/use-edit-mode-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,11 @@
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { __, sprintf, isRTL } from '@wordpress/i18n';
import {
trash,
rotateLeft,
rotateRight,
layout,
page,
plus,
} from '@wordpress/icons';
import { useCommandLoader, store as commandsStore } from '@wordpress/commands';
import { trash, rotateLeft, rotateRight, layout, page } from '@wordpress/icons';
import { useCommandLoader } from '@wordpress/commands';
import { decodeEntities } from '@wordpress/html-entities';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { store as editorStore } from '@wordpress/editor';
import { store as coreStore } from '@wordpress/core-data';
import { store as noticesStore } from '@wordpress/notices';
import { useMemo } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -168,70 +158,7 @@ function useManipulateDocumentCommands() {
};
}

function useAddNewPageCommand() {
const history = useHistory();
const { saveEntityRecord } = useDispatch( coreStore );
const { createErrorNotice } = useDispatch( noticesStore );

const createPageEntity = async ( { close } ) => {
try {
const _page = await saveEntityRecord(
'postType',
'page',
{
status: 'draft',
},
{
throwOnError: true,
}
);
if ( _page?.id ) {
history.push( {
postId: _page.id,
postType: 'page',
canvas: 'edit',
} );
}
} catch ( error ) {
const errorMessage =
error.message && error.code !== 'unknown_error'
? error.message
: __( 'An error occurred while creating the item.' );

createErrorNotice( errorMessage, {
type: 'snackbar',
} );
} finally {
close();
}
};

const commands = useMemo( () => {
return [
{
name: 'core/add-new-page',
label: __( 'Add new page' ),
icon: plus,
callback: createPageEntity,
},
];
}, [ createPageEntity ] );

return {
isLoading: false,
commands,
};
}

export function useEditModeCommands() {
const { unregisterCommand } = useDispatch( commandsStore );
unregisterCommand( 'core/add-new-page' );

useCommandLoader( {
name: 'core/add-new-page',
hook: useAddNewPageCommand,
} );

useCommandLoader( {
name: 'core/edit-site/page-content-focus',
hook: usePageContentFocusCommands,
Expand Down