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

Template Parts: Add duplicate template part command #55342

Draft
wants to merge 1 commit into
base: try/rename-template-part-command
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export function CreateTemplatePartModalContents( {
defaultArea = TEMPLATE_PART_AREA_DEFAULT_CATEGORY,
blocks = [],
confirmLabel = __( 'Create' ),
content,
closeModal,
onCreate,
onError,
Expand Down Expand Up @@ -95,7 +96,7 @@ export function CreateTemplatePartModalContents( {
{
slug: cleanSlug,
title: uniqueTitle,
content: serialize( blocks ),
content: content || serialize( blocks ),
area,
},
{ throwOnError: true }
Expand Down
93 changes: 93 additions & 0 deletions packages/edit-site/src/components/template-part-modal/duplicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* WordPress dependencies
*/
import { useDispatch, useSelect } from '@wordpress/data';
import { __, sprintf } from '@wordpress/i18n';
import { store as interfaceStore } from '@wordpress/interface';
import { store as noticesStore } from '@wordpress/notices';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { getQueryArgs } from '@wordpress/url';

/**
* Internal dependencies
*/
import CreateTemplatePartModal from '../create-template-part-modal';
import useEditedEntityRecord from '../use-edited-entity-record';
import { TEMPLATE_PART_MODALS } from './';
import { unlock } from '../../lock-unlock';
import { TEMPLATE_PART_POST_TYPE } from '../../utils/constants';

const { useHistory } = unlock( routerPrivateApis );

function DuplicateModal( { templatePart, onClose, onSuccess } ) {
const { createSuccessNotice } = useDispatch( noticesStore );

function handleOnSuccess( newTemplatePart ) {
createSuccessNotice(
sprintf(
// translators: %s: The new template part's title e.g. 'Call to action (copy)'.
__( '"%s" duplicated.' ),
templatePart.title
),
{
type: 'snackbar',
id: 'template-parts-create',
}
);

onSuccess?.( newTemplatePart );
}

return (
<CreateTemplatePartModal
content={ templatePart.content }
closeModal={ onClose }
confirmLabel={ __( 'Duplicate' ) }
defaultArea={ templatePart.area }
defaultTitle={ sprintf(
/* translators: %s: Existing template part title */
__( '%s (Copy)' ),
typeof templatePart.title === 'string'
? templatePart.title
: templatePart.title.raw
) }
modalTitle={ __( 'Duplicate template part' ) }
onCreate={ handleOnSuccess }
onError={ onClose }
/>
);
}

export default function TemplatePartDuplicateModal() {
const { record } = useEditedEntityRecord();
const { categoryType, categoryId } = getQueryArgs( window.location.href );
const { closeModal } = useDispatch( interfaceStore );
const history = useHistory();

const isActive = useSelect( ( select ) =>
select( interfaceStore ).isModalActive( TEMPLATE_PART_MODALS.duplicate )
);

if ( ! isActive ) {
return null;
}

function onSuccess( newTemplatePart ) {
history.push( {
postType: TEMPLATE_PART_POST_TYPE,
postId: newTemplatePart.id,
categoryType,
categoryId,
} );

closeModal();
}

return (
<DuplicateModal
onClose={ closeModal }
onSuccess={ onSuccess }
templatePart={ record }
/>
);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* Internal dependencies
*/
import TemplatePartDuplicateModal from './duplicate';
import TemplatePartRenameModal from './rename';

export const TEMPLATE_PART_MODALS = {
Expand All @@ -9,11 +10,10 @@ export const TEMPLATE_PART_MODALS = {
};

export default function TemplatePartModal() {
// Duplication command and modal is in separate follow-up.
return (
<>
<TemplatePartRenameModal />
{ /* <PatternDuplicateModal /> */ }
<TemplatePartDuplicateModal />
</>
);
}
12 changes: 10 additions & 2 deletions packages/edit-site/src/hooks/commands/use-edit-mode-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,15 +290,23 @@ function usePatternCommands() {
commands.push( {
name: 'core/rename-template-part',
label: __( 'Rename template part' ),
icon: symbol,
icon: edit,
callback: ( { close } ) => {
openModal( TEMPLATE_PART_MODALS.rename );
close();
},
} );
}

// All template parts will be eligible for duplication in a follow-up.
commands.push( {
name: 'core/duplicate-template-part',
label: __( 'Duplicate template part' ),
icon: symbol,
callback: ( { close } ) => {
openModal( TEMPLATE_PART_MODALS.duplicate );
close();
},
} );
}

return { isLoading: false, commands };
Expand Down
Loading