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

Add: Delete template action. #31678

Merged
merged 8 commits into from
May 19, 2021
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* External dependencies
*/
import { pickBy } from 'lodash';

/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { MenuGroup, MenuItem } from '@wordpress/components';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { useDispatch, useSelect } from '@wordpress/data';
import { store as editorStore } from '@wordpress/editor';
import { store as coreStore } from '@wordpress/core-data';

/**
* Internal dependencies
*/
import { store as editPostStore } from '../../../store';

export default function DeleteTemplate() {
const { clearSelectedBlock } = useDispatch( blockEditorStore );
const { setIsEditingTemplate } = useDispatch( editPostStore );
const { getEditorSettings } = useSelect( editorStore );
const { updateEditorSettings, editPost } = useDispatch( editorStore );
const { deleteEntityRecord } = useDispatch( coreStore );
const { template } = useSelect( ( select ) => {
const { isEditingTemplate, getEditedPostTemplate } = select(
editPostStore
);
const _isEditing = isEditingTemplate();
return {
template: _isEditing ? getEditedPostTemplate() : null,
};
}, [] );

if ( ! template || ! template.wp_id ) {
return null;
}
let templateTitle = template.slug;
if ( template?.title ) {
templateTitle = template.title;
}

return (
jorgefilipecosta marked this conversation as resolved.
Show resolved Hide resolved
<MenuGroup className="edit-post-template-top-area__second-menu-group">
<MenuItem
isDestructive
isTertiary
isLink
aria-label={ __( 'Delete template' ) }
onClick={ () => {
jorgefilipecosta marked this conversation as resolved.
Show resolved Hide resolved
if (
// eslint-disable-next-line no-alert
window.confirm(
/* translators: %1$s: template name */
sprintf(
'Are you sure you want to delete the %s template? It may be used by other pages or posts.',
templateTitle
)
)
) {
clearSelectedBlock();
setIsEditingTemplate( false );

editPost( {
template: '',
} );
const settings = getEditorSettings();
const newAvailableTemplates = pickBy(
settings.availableTemplates,
( _title, id ) => {
return id !== template.slug;
}
);
updateEditorSettings( {
...settings,
availableTemplates: newAvailableTemplates,
} );
deleteEntityRecord(
'postType',
'wp_template',
template.id
);
}
} }
>
{ __( 'Delete template' ) }
</MenuItem>
</MenuGroup>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* External dependencies
*/
import { mapValues } from 'lodash';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { TextControl } from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import { store as editorStore } from '@wordpress/editor';
import { store as coreStore } from '@wordpress/core-data';

/**
* Internal dependencies
*/
import { store as editPostStore } from '../../../store';

export default function EditTemplateTitle() {
const { template } = useSelect( ( select ) => {
const { getEditedPostTemplate } = select( editPostStore );
return {
template: getEditedPostTemplate(),
};
}, [] );

const { editEntityRecord } = useDispatch( coreStore );
const { getEditorSettings } = useSelect( editorStore );
const { updateEditorSettings } = useDispatch( editorStore );

let templateTitle = __( 'Default' );
if ( template?.title ) {
templateTitle = template.title;
} else if ( !! template ) {
templateTitle = template.slug;
}

return (
<TextControl
label={ __( 'Title' ) }
value={ templateTitle }
onChange={ ( newTitle ) => {
const settings = getEditorSettings();
const newAvailableTemplates = mapValues(
settings.availableTemplates,
( existingTitle, id ) => {
if ( id !== template.slug ) {
return existingTitle;
}
return newTitle;
}
);
updateEditorSettings( {
...settings,
availableTemplates: newAvailableTemplates,
} );
editEntityRecord( 'postType', 'wp_template', template.id, {
title: newTitle,
} );
} }
/>
);
}
41 changes: 32 additions & 9 deletions packages/edit-post/src/components/header/template-title/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { Button, Dropdown } from '@wordpress/components';

/**
* Internal dependencies
*/
import { store as editPostStore } from '../../../store';
import DeleteTemplate from './delete-template';
import EditTemplateTitle from './edit-template-title';

function TemplateTitle() {
const { template, isEditing } = useSelect( ( select ) => {
Expand All @@ -26,19 +29,39 @@ function TemplateTitle() {
}

let templateTitle = __( 'Default' );
if ( template?.title?.raw ) {
templateTitle = template.title.raw;
if ( template?.title ) {
templateTitle = template.title;
} else if ( !! template ) {
templateTitle = template.slug;
}

return (
<span className="edit-post-template-title">
{
/* translators: 1: Template name. */
sprintf( __( 'Editing template: %s' ), templateTitle )
}
</span>
<Dropdown
position="bottom center"
className="edit-post-template-top-area"
contentClassName="edit-post-template-top-area__popover"
renderToggle={ ( { onToggle } ) => (
<>
<div className="edit-post-template-title">
{ __( 'About' ) }
jorgefilipecosta marked this conversation as resolved.
Show resolved Hide resolved
</div>
<Button
isSmall
isTertiary
onClick={ onToggle }
aria-label={ __( 'Template Options' ) }
>
{ templateTitle }
Copy link
Contributor

@carolinan carolinan May 19, 2021

Choose a reason for hiding this comment

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

What is a suitable context for this button?
What do we call the modal popover, "Template options"?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes the aria-label sounds good to me 👍

</Button>
</>
) }
renderContent={ () => (
<>
<EditTemplateTitle />
<DeleteTemplate />
</>
) }
/>
);
}

Expand Down
12 changes: 12 additions & 0 deletions packages/edit-post/src/components/header/template-title/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,15 @@
flex-grow: 1;
justify-content: center;
}

.edit-post-template-top-area {
display: flex;
flex-direction: column;
align-content: space-between;
width: 100%;
align-items: center;
}

.edit-post-template-top-area__popover .components-popover__content {
min-width: 360px;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function TemplateSummary() {

<FlexBlock>
<h2 className="edit-post-template-summary__title">
{ template?.title?.raw || template?.slug }
{ template?.title || template?.slug }
</h2>
<p>{ template?.description }</p>
</FlexBlock>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function TemplatePanel() {
panelTitle = sprintf(
/* translators: %s: template title */
__( 'Template: %s' ),
template?.title?.raw ?? template.slug
template?.title ?? template.slug
);
}

Expand Down
10 changes: 9 additions & 1 deletion packages/edit-post/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,17 @@ export const getEditedPostTemplate = createRegistrySelector(
'template'
);
if ( currentTemplate ) {
return select( coreStore )
const templateWithSameSlug = select( coreStore )
.getEntityRecords( 'postType', 'wp_template' )
?.find( ( template ) => template.slug === currentTemplate );
if ( ! templateWithSameSlug ) {
return templateWithSameSlug;
}
return select( coreStore ).getEditedEntityRecord(
'postType',
'wp_template',
templateWithSameSlug.id
);
}

const post = select( editorStore ).getCurrentPost();
Expand Down