diff --git a/packages/edit-post/src/components/header/template-title/delete-template.js b/packages/edit-post/src/components/header/template-title/delete-template.js new file mode 100644 index 0000000000000..7a3f5d5c38207 --- /dev/null +++ b/packages/edit-post/src/components/header/template-title/delete-template.js @@ -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 ( + + { + 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' ) } + + + ); +} diff --git a/packages/edit-post/src/components/header/template-title/edit-template-title.js b/packages/edit-post/src/components/header/template-title/edit-template-title.js new file mode 100644 index 0000000000000..feabafcb9fdd0 --- /dev/null +++ b/packages/edit-post/src/components/header/template-title/edit-template-title.js @@ -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 ( + { + 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, + } ); + } } + /> + ); +} diff --git a/packages/edit-post/src/components/header/template-title/index.js b/packages/edit-post/src/components/header/template-title/index.js index 4cc78782ab620..907badf27de95 100644 --- a/packages/edit-post/src/components/header/template-title/index.js +++ b/packages/edit-post/src/components/header/template-title/index.js @@ -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 ) => { @@ -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 ( - - { - /* translators: 1: Template name. */ - sprintf( __( 'Editing template: %s' ), templateTitle ) - } - + ( + <> +
+ { __( 'About' ) } +
+ + + ) } + renderContent={ () => ( + <> + + + + ) } + /> ); } diff --git a/packages/edit-post/src/components/header/template-title/style.scss b/packages/edit-post/src/components/header/template-title/style.scss index b4d6132c08975..40262e0573ee8 100644 --- a/packages/edit-post/src/components/header/template-title/style.scss +++ b/packages/edit-post/src/components/header/template-title/style.scss @@ -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; +} diff --git a/packages/edit-post/src/components/sidebar/template-summary/index.js b/packages/edit-post/src/components/sidebar/template-summary/index.js index ec29d428f0ac8..f9e9f93cee270 100644 --- a/packages/edit-post/src/components/sidebar/template-summary/index.js +++ b/packages/edit-post/src/components/sidebar/template-summary/index.js @@ -29,7 +29,7 @@ function TemplateSummary() {

- { template?.title?.raw || template?.slug } + { template?.title || template?.slug }

{ template?.description }

diff --git a/packages/edit-post/src/components/sidebar/template/index.js b/packages/edit-post/src/components/sidebar/template/index.js index b8308afefa857..205812819d6bc 100644 --- a/packages/edit-post/src/components/sidebar/template/index.js +++ b/packages/edit-post/src/components/sidebar/template/index.js @@ -79,7 +79,7 @@ export function TemplatePanel() { panelTitle = sprintf( /* translators: %s: template title */ __( 'Template: %s' ), - template?.title?.raw ?? template.slug + template?.title ?? template.slug ); } diff --git a/packages/edit-post/src/store/selectors.js b/packages/edit-post/src/store/selectors.js index a1db999ea1266..7dabddec5e90d 100644 --- a/packages/edit-post/src/store/selectors.js +++ b/packages/edit-post/src/store/selectors.js @@ -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();