Skip to content
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

Post template: try to remove duplicate preview #60535

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 3 additions & 60 deletions packages/block-library/src/post-template/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { memo, useMemo, useState } from '@wordpress/element';
import { useMemo } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import {
BlockControls,
BlockContextProvider,
__experimentalUseBlockPreview as useBlockPreview,
useBlockProps,
useInnerBlocksProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { Spinner, ToolbarGroup } from '@wordpress/components';
import { store as coreStore } from '@wordpress/core-data';
Expand All @@ -35,42 +33,6 @@ function PostTemplateInnerBlocks() {
return <li { ...innerBlocksProps } />;
}

function PostTemplateBlockPreview( {
blocks,
blockContextId,
isHidden,
setActiveBlockContextId,
} ) {
const blockPreviewProps = useBlockPreview( {
blocks,
props: {
className: 'wp-block-post',
},
} );

const handleOnClick = () => {
setActiveBlockContextId( blockContextId );
};

const style = {
display: isHidden ? 'none' : undefined,
};

return (
<li
{ ...blockPreviewProps }
tabIndex={ 0 }
// eslint-disable-next-line jsx-a11y/no-noninteractive-element-to-interactive-role
role="button"
onClick={ handleOnClick }
onKeyPress={ handleOnClick }
style={ style }
/>
);
}

const MemoizedPostTemplateBlockPreview = memo( PostTemplateBlockPreview );

export default function PostTemplateEdit( {
setAttributes,
clientId,
Expand Down Expand Up @@ -103,11 +65,9 @@ export default function PostTemplateEdit( {
__unstableLayoutClassNames,
} ) {
const { type: layoutType, columnCount = 3 } = layout || {};
const [ activeBlockContextId, setActiveBlockContextId ] = useState();
const { posts, blocks } = useSelect(
const { posts } = useSelect(
( select ) => {
const { getEntityRecords, getTaxonomies } = select( coreStore );
const { getBlocks } = select( blockEditorStore );
const templateCategory =
inherit &&
templateSlug?.startsWith( 'category-' ) &&
Expand Down Expand Up @@ -186,7 +146,6 @@ export default function PostTemplateEdit( {
...query,
...restQueryArgs,
} ),
blocks: getBlocks( clientId ),
};
},
[
Expand Down Expand Up @@ -277,23 +236,7 @@ export default function PostTemplateEdit( {
key={ blockContext.postId }
value={ blockContext }
>
{ blockContext.postId ===
( activeBlockContextId ||
blockContexts[ 0 ]?.postId ) ? (
<PostTemplateInnerBlocks />
) : null }
<MemoizedPostTemplateBlockPreview
blocks={ blocks }
blockContextId={ blockContext.postId }
setActiveBlockContextId={
setActiveBlockContextId
}
isHidden={
blockContext.postId ===
( activeBlockContextId ||
blockContexts[ 0 ]?.postId )
}
/>
<PostTemplateInnerBlocks />
</BlockContextProvider>
) ) }
</ul>
Expand Down
Loading