-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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: Reset 'Show template' toggle when leaving edit mode #54679
Changes from 2 commits
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 |
---|---|---|
|
@@ -11,7 +11,7 @@ import { | |
__experimentalText as Text, | ||
} from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { store as coreStore, useEntityBlockEditor } from '@wordpress/core-data'; | ||
import { check } from '@wordpress/icons'; | ||
|
||
/** | ||
|
@@ -21,14 +21,15 @@ import { store as editSiteStore } from '../../../store'; | |
import SwapTemplateButton from './swap-template-button'; | ||
import ResetDefaultTemplate from './reset-default-template'; | ||
import { unlock } from '../../../lock-unlock'; | ||
import usePageContentBlocks from '../../block-editor/block-editor-provider/use-page-content-blocks'; | ||
|
||
const POPOVER_PROPS = { | ||
className: 'edit-site-page-panels-edit-template__dropdown', | ||
placement: 'bottom-start', | ||
}; | ||
|
||
export default function EditTemplate() { | ||
const { hasResolved, template, isTemplateHidden } = useSelect( | ||
const { hasResolved, template, isTemplateHidden, postType } = useSelect( | ||
( select ) => { | ||
const { getEditedPostContext, getEditedPostType, getEditedPostId } = | ||
select( editSiteStore ); | ||
|
@@ -38,11 +39,13 @@ export default function EditTemplate() { | |
const { getEditedEntityRecord, hasFinishedResolution } = | ||
select( coreStore ); | ||
const _context = getEditedPostContext(); | ||
const _postType = getEditedPostType(); | ||
const queryArgs = [ | ||
'postType', | ||
getEditedPostType(), | ||
getEditedPostId(), | ||
]; | ||
|
||
return { | ||
context: _context, | ||
hasResolved: hasFinishedResolution( | ||
|
@@ -53,15 +56,23 @@ export default function EditTemplate() { | |
isTemplateHidden: | ||
getCanvasMode() === 'edit' && | ||
getPageContentFocusType() === 'hideTemplate', | ||
postType: _postType, | ||
}; | ||
}, | ||
[] | ||
); | ||
|
||
const [ blocks ] = useEntityBlockEditor( 'postType', postType ); | ||
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 it's an edge case, I'm happy to remove this if it's not performant, especially if the blockList is large. 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. From my perspective, I think this is probably okay, as we're only rendering it when we're in |
||
|
||
const { setHasPageContentFocus } = useDispatch( editSiteStore ); | ||
// Disable reason: `useDispatch` can't be called conditionally. | ||
// eslint-disable-next-line @wordpress/no-unused-vars-before-return | ||
const { setPageContentFocusType } = unlock( useDispatch( editSiteStore ) ); | ||
// Check if there are any post content block types in the blocks tree. | ||
const pageContentBlocks = usePageContentBlocks( { | ||
blocks, | ||
isPageContentFocused: true, | ||
} ); | ||
|
||
if ( ! hasResolved ) { | ||
return null; | ||
|
@@ -97,20 +108,24 @@ export default function EditTemplate() { | |
<SwapTemplateButton onClick={ onClose } /> | ||
</MenuGroup> | ||
<ResetDefaultTemplate onClick={ onClose } /> | ||
<MenuGroup> | ||
<MenuItem | ||
icon={ ! isTemplateHidden ? check : undefined } | ||
onClick={ () => { | ||
setPageContentFocusType( | ||
isTemplateHidden | ||
? 'disableTemplate' | ||
: 'hideTemplate' | ||
); | ||
} } | ||
> | ||
{ __( 'Template preview' ) } | ||
</MenuItem> | ||
</MenuGroup> | ||
{ !! pageContentBlocks?.length && ( | ||
<MenuGroup> | ||
<MenuItem | ||
icon={ | ||
! isTemplateHidden ? check : undefined | ||
} | ||
onClick={ () => { | ||
setPageContentFocusType( | ||
isTemplateHidden | ||
? 'disableTemplate' | ||
: 'hideTemplate' | ||
); | ||
} } | ||
> | ||
{ __( 'Template preview' ) } | ||
</MenuItem> | ||
</MenuGroup> | ||
) } | ||
</> | ||
) } | ||
</DropdownMenu> | ||
|
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.
Is this being used anywhere? It doesn't appear to be expanded in the above
const { ... } = useSelect
line, so I think these lines can be removed?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 left that in there to make sure you were paying attention.
Seriously, thanks for noticing 😄