Skip to content

Commit

Permalink
Post Content: Fix display of block support styles (#66003)
Browse files Browse the repository at this point in the history
Co-authored-by: aaronrobertshaw <[email protected]>
Co-authored-by: ntsekouras <[email protected]>
Co-authored-by: andrewserong <[email protected]>
Co-authored-by: ramonjd <[email protected]>
Co-authored-by: peterwilsoncc <[email protected]>
Co-authored-by: jasmussen <[email protected]>
Co-authored-by: carolinan <[email protected]>
Co-authored-by: ndiego <[email protected]>
Co-authored-by: colorful-tones <[email protected]>
  • Loading branch information
10 people authored and gutenbergplugin committed Oct 11, 2024
1 parent 7e52f6e commit 0ca8d56
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion packages/block-library/src/post-content/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,24 @@ import {
RecursionProvider,
useHasRecursion,
Warning,
__experimentalUseBlockPreview as useBlockPreview,
} from '@wordpress/block-editor';
import { parse } from '@wordpress/blocks';
import {
useEntityProp,
useEntityBlockEditor,
store as coreStore,
} from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import { useMemo } from '@wordpress/element';

/**
* Internal dependencies
*/
import { useCanEditEntity } from '../utils/hooks';

function ReadOnlyContent( {
parentLayout,
layoutClassNames,
userCanEdit,
postType,
Expand All @@ -33,7 +38,28 @@ function ReadOnlyContent( {
postId
);
const blockProps = useBlockProps( { className: layoutClassNames } );
return content?.protected && ! userCanEdit ? (
const blocks = useMemo( () => {
return content?.raw ? parse( content.raw ) : [];
}, [ content?.raw ] );
const blockPreviewProps = useBlockPreview( {
blocks,
props: blockProps,
layout: parentLayout,
} );

if ( userCanEdit ) {
/*
* Rendering the block preview using the raw content blocks allows for
* block support styles to be generated and applied by the editor.
*
* The preview using the raw blocks can only be presented to users with
* edit permissions for the post to prevent potential exposure of private
* block content.
*/
return <div { ...blockPreviewProps }></div>;
}

return content?.protected ? (
<div { ...blockProps }>
<Warning>{ __( 'This content is password protected.' ) }</Warning>
</div>
Expand Down Expand Up @@ -96,6 +122,7 @@ function Content( props ) {
<EditableContent { ...props } />
) : (
<ReadOnlyContent
parentLayout={ props.parentLayout }
layoutClassNames={ layoutClassNames }
userCanEdit={ userCanEdit }
postType={ postType }
Expand Down Expand Up @@ -141,6 +168,7 @@ function RecursionError() {
export default function PostContentEdit( {
context,
__unstableLayoutClassNames: layoutClassNames,
__unstableParentLayout: parentLayout,
} ) {
const { postId: contextPostId, postType: contextPostType } = context;
const hasAlreadyRendered = useHasRecursion( contextPostId );
Expand All @@ -154,6 +182,7 @@ export default function PostContentEdit( {
{ contextPostId && contextPostType ? (
<Content
context={ context }
parentLayout={ parentLayout }
layoutClassNames={ layoutClassNames }
/>
) : (
Expand Down

0 comments on commit 0ca8d56

Please sign in to comment.