Skip to content

Commit

Permalink
Site Editor: Add a fallback template showing the title and content fo…
Browse files Browse the repository at this point in the history
…r the post only mode (#56509)
  • Loading branch information
youknowriad authored Nov 27, 2023
1 parent 2d4000f commit 0adb73f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { DropdownMenu, MenuGroup, MenuItem } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { store as coreStore } from '@wordpress/core-data';
import { check } from '@wordpress/icons';
import { store as blockEditorStore } from '@wordpress/block-editor';
import {
privateApis as editorPrivateApis,
store as editorStore,
Expand All @@ -20,7 +19,6 @@ import { store as editSiteStore } from '../../../store';
import SwapTemplateButton from './swap-template-button';
import ResetDefaultTemplate from './reset-default-template';
import { unlock } from '../../../lock-unlock';
import { PAGE_CONTENT_BLOCK_TYPES } from '../../../utils/constants';

const { PostPanelRow } = unlock( editorPrivateApis );

Expand All @@ -30,22 +28,17 @@ const POPOVER_PROPS = {
};

export default function EditTemplate() {
const { hasPostContentBlocks, hasResolved, template, isTemplateHidden } =
useSelect( ( select ) => {
const { hasResolved, template, isTemplateHidden } = useSelect(
( select ) => {
const { getEditedPostContext, getEditedPostType, getEditedPostId } =
select( editSiteStore );
const { getRenderingMode } = unlock( select( editorStore ) );
const { getEditedEntityRecord, hasFinishedResolution } =
select( coreStore );
const { __experimentalGetGlobalBlocksByName } =
select( blockEditorStore );
const _context = getEditedPostContext();
const _postType = getEditedPostType();
const queryArgs = [ 'postType', _postType, getEditedPostId() ];
return {
hasPostContentBlocks: !! __experimentalGetGlobalBlocksByName(
Object.keys( PAGE_CONTENT_BLOCK_TYPES )
).length,
context: _context,
hasResolved: hasFinishedResolution(
'getEditedEntityRecord',
Expand All @@ -55,7 +48,9 @@ export default function EditTemplate() {
isTemplateHidden: getRenderingMode() === 'post-only',
postType: _postType,
};
}, [] );
},
[]
);

const { setRenderingMode } = useDispatch( editorStore );

Expand Down Expand Up @@ -90,25 +85,21 @@ export default function EditTemplate() {
<SwapTemplateButton onClick={ onClose } />
</MenuGroup>
<ResetDefaultTemplate onClick={ onClose } />
{ hasPostContentBlocks && (
<MenuGroup>
<MenuItem
icon={
! isTemplateHidden ? check : undefined
}
isPressed={ ! isTemplateHidden }
onClick={ () => {
setRenderingMode(
isTemplateHidden
? 'template-locked'
: 'post-only'
);
} }
>
{ __( 'Template preview' ) }
</MenuItem>
</MenuGroup>
) }
<MenuGroup>
<MenuItem
icon={ ! isTemplateHidden ? check : undefined }
isPressed={ ! isTemplateHidden }
onClick={ () => {
setRenderingMode(
isTemplateHidden
? 'template-locked'
: 'post-only'
);
} }
>
{ __( 'Template preview' ) }
</MenuItem>
</MenuGroup>
</>
) }
</DropdownMenu>
Expand Down
10 changes: 0 additions & 10 deletions packages/edit-site/src/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,6 @@ export const FOCUSABLE_ENTITIES = [
PATTERN_TYPES.user,
];

/**
* Block types that are considered to be page content. These are the only blocks
* editable when the page is focused.
*/
export const PAGE_CONTENT_BLOCK_TYPES = {
'core/post-title': true,
'core/post-featured-image': true,
'core/post-content': true,
};

export const POST_TYPE_LABELS = {
[ TEMPLATE_POST_TYPE ]: __( 'Template' ),
[ TEMPLATE_PART_POST_TYPE ]: __( 'Template part' ),
Expand Down
13 changes: 10 additions & 3 deletions packages/editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ function useBlockEditorProps( post, template, mode ) {
}

if ( mode === 'post-only' ) {
const postContentBlocks =
extractPageContentBlockTypesFromTemplateBlocks(
templateBlocks
);
return [
createBlock(
'core/group',
Expand All @@ -138,9 +142,12 @@ function useBlockEditorProps( post, template, mode ) {
},
},
},
extractPageContentBlockTypesFromTemplateBlocks(
templateBlocks
)
postContentBlocks.length
? postContentBlocks
: [
createBlock( 'core/post-title' ),
createBlock( 'core/post-content' ),
]
),
];
}
Expand Down

0 comments on commit 0adb73f

Please sign in to comment.