Skip to content

Commit

Permalink
This commit:
Browse files Browse the repository at this point in the history
- Resets the page content focus type if the canvas switches from edit mode
- Refactors use-page-content-blocks.js so that we can check in the editor if there are any page content blocks in the editor. If not, we do not render the show/hide template.
  • Loading branch information
ramonjd committed Sep 21, 2023
1 parent aa8ec5b commit 34806f9
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,18 @@ export default function DefaultBlockEditorProvider( { children } ) {
'postType',
templateType
);
const pageContentBlock = usePageContentBlocks( blocks, isTemplateHidden );
const pageContentBlocks = usePageContentBlocks( {
blocks,
isPageContentFocused: isTemplateHidden,
wrapPageContent: true,
} );

return (
<ExperimentalBlockEditorProvider
settings={ settings }
value={
isTemplateHidden && pageContentBlock.length
? pageContentBlock
isTemplateHidden && pageContentBlocks.length
? pageContentBlocks
: blocks
}
onInput={ isTemplateHidden ? noop : onInput }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,38 @@ function flattenBlocks( blocks, transform ) {

/**
* Returns a memoized array of blocks that contain only page content blocks,
* surrounded by a group block to mimic the post editor.
*
* @param {Array} blocks Block list.
* @param {boolean} isPageContentFocused Whether the page content has focus. If `true` return page content blocks. Default `false`.
* surrounded by an optional group block to mimic the post editor.
*
* @param {Object} props The argument for the function.
* @param {Array} props.blocks Block list.
* @param {boolean} props.isPageContentFocused Whether the page content has focus (and the surrounding template is inert). If `true` return page content blocks. Default `false`.
* @param {boolean} props.wrapPageContent Whether to wrap the page content blocks in a group block to mimic the post editor. Default `false`.
* @return {Array} Page content blocks.
*/
export default function usePageContentBlocks(
blocks,
isPageContentFocused = false
) {
export default function usePageContentBlocks( {
blocks = [],
isPageContentFocused = false,
wrapPageContent = false,
} ) {
return useMemo( () => {
if ( ! isPageContentFocused || ! blocks || ! blocks.length ) {
if ( ! isPageContentFocused || ! blocks?.length ) {
return [];
}

const innerBlocks = flattenBlocks( blocks, ( block ) =>
PAGE_CONTENT_BLOCK_TYPES[ block.name ]
? createBlock( block.name )
: undefined
);

if ( ! innerBlocks.length ) {
return [];
}

if ( ! wrapPageContent ) {
return innerBlocks;
}

return [
createBlock(
'core/group',
Expand All @@ -66,11 +83,7 @@ export default function usePageContentBlocks(
},
},
},
flattenBlocks( blocks, ( block ) => {
if ( PAGE_CONTENT_BLOCK_TYPES[ block.name ] ) {
return createBlock( block.name );
}
} )
innerBlocks
),
];
}, [ blocks, isPageContentFocused ] );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,49 @@
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';

import { useSelect, useDispatch } from '@wordpress/data';
import { useEffect } from '@wordpress/element';
/**
* Internal dependencies
*/
import { store as editSiteStore } from '../../store';
import DisableNonPageContentBlocks from './disable-non-page-content-blocks';
import EditTemplateNotification from './edit-template-notification';
import BackToPageNotification from './back-to-page-notification';
import { unlock } from '../../lock-unlock';

export default function PageContentFocusManager( { contentRef } ) {
const hasPageContentFocus = useSelect(
( select ) => select( editSiteStore ).hasPageContentFocus(),
const { hasPageContentFocus, pageContentFocusType, canvasMode } = useSelect(
( select ) => {
const { getPageContentFocusType, getCanvasMode } = unlock(
select( editSiteStore )
);
const _canvasMode = getCanvasMode();
return {
canvasMode: _canvasMode,
pageContentFocusType: getPageContentFocusType(),
shouldDisplayPageContentTemplates:
getCanvasMode() !== 'edit' &&
getPageContentFocusType() !== 'disableTemplate',
hasPageContentFocus:
select( editSiteStore ).hasPageContentFocus(),
};
},
[]
);
const { setPageContentFocusType } = unlock( useDispatch( editSiteStore ) );

/*
* Ensure that the page content focus type is set to `disableTemplate` when
* the canvas mode is not `edit`. This makes the experience consistent with
* refreshing the page, which resets the page content focus type.
*/
useEffect( () => {
if ( canvasMode !== 'edit' && !! pageContentFocusType ) {
setPageContentFocusType( null );
}
}, [ canvasMode, pageContentFocusType ] );

return (
<>
{ hasPageContentFocus && <DisableNonPageContentBlocks /> }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand All @@ -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 );
Expand All @@ -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(
Expand All @@ -53,15 +56,23 @@ export default function EditTemplate() {
isTemplateHidden:
getCanvasMode() === 'edit' &&
getPageContentFocusType() === 'hideTemplate',
postType: _postType,
};
},
[]
);

const [ blocks ] = useEntityBlockEditor( 'postType', postType );

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;
Expand Down Expand Up @@ -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>
Expand Down

0 comments on commit 34806f9

Please sign in to comment.