-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
[Site Editor - Page Inspector]: Add ability to switch templates #51477
Changes from all commits
b3a4b69
b07f0f7
9fb10fd
54ebe82
650b9ba
32259b5
21e5769
6beefed
13cfba0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 === | ||
ntsekouras marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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; | ||
} |
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 } ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we using undoIngore in these edits? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because the specific There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess that means we need to solve that to solve the undo as well #51477 (comment) |
||
onClick(); | ||
await setPage( { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the idea here to cause a refresh of the template or something? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exactly. |
||
context: { postType, postId }, | ||
} ); | ||
} } | ||
> | ||
{ __( 'Reset' ) } | ||
</MenuItem> | ||
</MenuGroup> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,37 @@ | ||
.edit-site-page-panels__edit-template-preview { | ||
border: 1px solid $gray-200; | ||
height: 200px; | ||
max-height: 200px; | ||
overflow: hidden; | ||
.edit-site-swap-template-modal { | ||
z-index: z-index(".edit-site-swap-template-modal"); | ||
} | ||
|
||
.edit-site-page-panels__edit-template-button { | ||
justify-content: center; | ||
.edit-site-page-panels__swap-template__confirm-modal__actions { | ||
margin-top: $grid-unit-30; | ||
} | ||
|
||
.edit-site-page-panels__swap-template__modal-content .block-editor-block-patterns-list { | ||
column-count: 2; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I'm not wrong, this CSS is repeated in a lot of places, I wonder if we should have this in the component itself (like gridColumns prop or something like that) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's true, but I focused for now with the technical implementation for the first round of reviews. |
||
column-gap: $grid-unit-30; | ||
|
||
// Small top padding required to avoid cutting off the visible outline when hovering items | ||
padding-top: $border-width-focus-fallback; | ||
|
||
@include break-medium() { | ||
column-count: 3; | ||
} | ||
|
||
@include break-wide() { | ||
column-count: 4; | ||
} | ||
|
||
.block-editor-block-patterns-list__list-item { | ||
break-inside: avoid-column; | ||
} | ||
|
||
.block-editor-block-patterns-list__item { | ||
// Avoid to override the BlockPatternList component | ||
// default hover and focus styles. | ||
&:not(:focus):not(:hover) .block-editor-block-preview__container { | ||
box-shadow: 0 0 0 1px $gray-300; | ||
} | ||
} | ||
} | ||
|
||
.edit-site-change-status__content { | ||
|
@@ -36,15 +61,21 @@ | |
|
||
.edit-site-summary-field { | ||
.components-dropdown { | ||
flex-grow: 1; | ||
width: 70%; | ||
} | ||
ntsekouras marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.edit-site-summary-field__trigger { | ||
width: 100%; | ||
max-width: 100%; | ||
|
||
// Truncate | ||
display: block; | ||
text-align: left; | ||
white-space: nowrap; | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
} | ||
|
||
.edit-site-summary-field__label { | ||
width: 30%; | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: Do we really need this hook? Feels like a very light shortcut.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that you're using this pattern for several hooks. Personally I wonder if this indirection is necessary to be honest.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This pattern is going to increase the number of useSelect we have. While I didn't measure anything here. I wonder if a single useSelect is in general more performant than multiple ones.
All of this is still a small thing though. I'm fine if you think this makes the code clearer/better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mostly did this because of the different dependencies of other
useSelect
in the code needed.