Skip to content

Commit

Permalink
Add rename template part command
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrobertshaw committed Mar 22, 2024
1 parent 8ca9c7f commit 0627b18
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/edit-site/src/components/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import CanvasLoader from '../canvas-loader';
import { unlock } from '../../lock-unlock';
import useEditedEntityRecord from '../use-edited-entity-record';
import PatternModal from '../pattern-modal';
import TemplatePartModal from '../template-part-modal';
import { POST_TYPE_LABELS, TEMPLATE_POST_TYPE } from '../../utils/constants';
import SiteEditorCanvas from '../block-editor/site-editor-canvas';
import TemplatePartConverter from '../template-part-converter';
Expand Down Expand Up @@ -227,6 +228,7 @@ export default function Editor( { isLoading, onClick } ) {
) }
<SiteEditorCanvas onClick={ onClick } />
<PatternModal />
<TemplatePartModal />
</>
) }
{ editorMode === 'text' && isEditMode && (
Expand Down
19 changes: 19 additions & 0 deletions packages/edit-site/src/components/template-part-modal/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Internal dependencies
*/
import TemplatePartRenameModal from './rename';

export const TEMPLATE_PART_MODALS = {
rename: 'edit-site/template-part-rename',
duplicate: 'edit-site/template-part-duplicate',
};

export default function TemplatePartModal() {
// Duplication command and modal is in separate follow-up.
return (
<>
<TemplatePartRenameModal />
{ /* <PatternDuplicateModal /> */ }
</>
);
}
31 changes: 31 additions & 0 deletions packages/edit-site/src/components/template-part-modal/rename.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* WordPress dependencies
*/
import { useDispatch, useSelect } from '@wordpress/data';
import { store as interfaceStore } from '@wordpress/interface';

/**
* Internal dependencies
*/
import RenameTemplatePartModal from '../rename-template-part-modal';
import { TEMPLATE_PART_MODALS } from './';
import useEditedEntityRecord from '../use-edited-entity-record';

export default function TemplatePartRenameModal() {
const { record } = useEditedEntityRecord();
const { closeModal } = useDispatch( interfaceStore );
const isActive = useSelect( ( select ) =>
select( interfaceStore ).isModalActive( TEMPLATE_PART_MODALS.rename )
);

if ( ! isActive ) {
return null;
}

return (
<RenameTemplatePartModal
onClose={ closeModal }
templatePart={ record }
/>
);
}
28 changes: 25 additions & 3 deletions packages/edit-site/src/hooks/commands/use-edit-mode-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ import isTemplateRevertable from '../../utils/is-template-revertable';
import { KEYBOARD_SHORTCUT_HELP_MODAL_NAME } from '../../components/keyboard-shortcut-help-modal';
import { PREFERENCES_MODAL_NAME } from '../../components/preferences-modal';
import { PATTERN_MODALS } from '../../components/pattern-modal';
import { TEMPLATE_PART_MODALS } from '../../components/template-part-modal';
import { unlock } from '../../lock-unlock';
import { TEMPLATE_POST_TYPE } from '../../utils/constants';
import {
PATTERN_TYPES,
TEMPLATE_ORIGINS,
TEMPLATE_PART_POST_TYPE,
TEMPLATE_POST_TYPE,
} from '../../utils/constants';
import { useLink } from '../../components/routes/link';

const { useHistory } = unlock( routerPrivateApis );
Expand Down Expand Up @@ -249,7 +255,7 @@ function useEditUICommands() {
}

function usePatternCommands() {
const { isLoaded, record: pattern } = useEditedEntityRecord();
const { isLoaded, record } = useEditedEntityRecord();
const { openModal } = useDispatch( interfaceStore );

if ( ! isLoaded ) {
Expand All @@ -258,7 +264,7 @@ function usePatternCommands() {

const commands = [];

if ( pattern?.type === 'wp_block' ) {
if ( record?.type === PATTERN_TYPES.user ) {
commands.push( {
name: 'core/rename-pattern',
label: __( 'Rename pattern' ),
Expand All @@ -279,6 +285,22 @@ function usePatternCommands() {
} );
}

if ( record?.type === TEMPLATE_PART_POST_TYPE ) {
if ( record?.source === TEMPLATE_ORIGINS.custom ) {
commands.push( {
name: 'core/rename-template-part',
label: __( 'Rename template part' ),
icon: symbol,
callback: ( { close } ) => {
openModal( TEMPLATE_PART_MODALS.rename );
close();
},
} );
}

// All template parts will be eligible for duplication in a follow-up.
}

return { isLoading: false, commands };
}

Expand Down

0 comments on commit 0627b18

Please sign in to comment.