-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Site Editor - Page Inspector]: Add ability to switch templates (#51477)
* [Site Editor - Page Inspector]: Add ability to switch templates * try setting the page/template from edited entity record * Update label * Truncate * update z-index of templates swap modal * fix typo * add + update tests * fix suggestions per template's `postTypes` and test * change order of calling in `onTemplateSelect` --------- Co-authored-by: James Koster <james@jameskoster.co.uk>
- Loading branch information
1 parent
7d054a2
commit bba4bbf
Showing
14 changed files
with
460 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
packages/edit-site/src/components/sidebar-edit-mode/page-panels/hooks.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
import { useMemo } from '@wordpress/element'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as editSiteStore } from '../../../store'; | ||
|
||
export function useEditedPostContext() { | ||
return useSelect( | ||
( select ) => select( editSiteStore ).getEditedPostContext(), | ||
[] | ||
); | ||
} | ||
|
||
export function useIsPostsPage() { | ||
const { postId } = useEditedPostContext(); | ||
return useSelect( | ||
( select ) => | ||
+postId === | ||
select( coreStore ).getEntityRecord( 'root', 'site' ) | ||
?.page_for_posts, | ||
[ postId ] | ||
); | ||
} | ||
|
||
function useTemplates() { | ||
return useSelect( | ||
( select ) => | ||
select( coreStore ).getEntityRecords( 'postType', 'wp_template', { | ||
per_page: -1, | ||
post_type: 'page', | ||
} ), | ||
[] | ||
); | ||
} | ||
|
||
export function useAvailableTemplates() { | ||
const currentTemplateSlug = useCurrentTemplateSlug(); | ||
const isPostsPage = useIsPostsPage(); | ||
const templates = useTemplates(); | ||
return useMemo( | ||
() => | ||
// The posts page template cannot be changed. | ||
! isPostsPage && | ||
templates?.filter( | ||
( template ) => | ||
template.is_custom && | ||
template.slug !== currentTemplateSlug && | ||
!! template.content.raw // Skip empty templates. | ||
), | ||
[ templates, currentTemplateSlug, isPostsPage ] | ||
); | ||
} | ||
|
||
export function useCurrentTemplateSlug() { | ||
const { postType, postId } = useEditedPostContext(); | ||
const templates = useTemplates(); | ||
const entityTemplate = useSelect( | ||
( select ) => { | ||
const post = select( coreStore ).getEditedEntityRecord( | ||
'postType', | ||
postType, | ||
postId | ||
); | ||
return post?.template; | ||
}, | ||
[ postType, postId ] | ||
); | ||
|
||
if ( ! entityTemplate ) { | ||
return; | ||
} | ||
// If a page has a `template` set and is not included in the list | ||
// of the theme's templates, do not return it, in order to resolve | ||
// to the current theme's default template. | ||
return templates?.find( ( template ) => template.slug === entityTemplate ) | ||
?.slug; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
packages/edit-site/src/components/sidebar-edit-mode/page-panels/reset-default-template.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useDispatch } from '@wordpress/data'; | ||
import { MenuGroup, MenuItem } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { useEntityRecord } from '@wordpress/core-data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
useCurrentTemplateSlug, | ||
useEditedPostContext, | ||
useIsPostsPage, | ||
} from './hooks'; | ||
import { store as editSiteStore } from '../../../store'; | ||
|
||
export default function ResetDefaultTemplate( { onClick } ) { | ||
const currentTemplateSlug = useCurrentTemplateSlug(); | ||
const isPostsPage = useIsPostsPage(); | ||
const { postType, postId } = useEditedPostContext(); | ||
const entity = useEntityRecord( 'postType', postType, postId ); | ||
const { setPage } = useDispatch( editSiteStore ); | ||
// The default template in a post is indicated by an empty string. | ||
if ( ! currentTemplateSlug || isPostsPage ) { | ||
return null; | ||
} | ||
return ( | ||
<MenuGroup> | ||
<MenuItem | ||
onClick={ async () => { | ||
entity.edit( { template: '' }, { undoIgnore: true } ); | ||
onClick(); | ||
await setPage( { | ||
context: { postType, postId }, | ||
} ); | ||
} } | ||
> | ||
{ __( 'Reset' ) } | ||
</MenuItem> | ||
</MenuGroup> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.