Skip to content

Commit

Permalink
Render non-editable preview of template part when user does not have …
Browse files Browse the repository at this point in the history
…capability to edit template part (WordPress#60326)

Co-authored-by: fabiankaegy <[email protected]>
Co-authored-by: youknowriad <[email protected]>
  • Loading branch information
3 people authored and cbravobernal committed Apr 9, 2024
1 parent 72d6761 commit 988454b
Showing 1 changed file with 98 additions and 2 deletions.
100 changes: 98 additions & 2 deletions packages/block-library/src/template-part/edit/inner-blocks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useEntityBlockEditor } from '@wordpress/core-data';
import { useEntityBlockEditor, store as coreStore } from '@wordpress/core-data';
import {
InnerBlocks,
useInnerBlocksProps,
Expand All @@ -10,6 +10,8 @@ import {
useBlockEditingMode,
} from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import { useMemo } from '@wordpress/element';
import { parse } from '@wordpress/blocks';

function useRenderAppender( hasInnerBlocks ) {
const blockEditingMode = useBlockEditingMode();
Expand All @@ -36,7 +38,62 @@ function useLayout( layout ) {
}
}

export default function TemplatePartInnerBlocks( {
function NonEditableTemplatePartPreview( {
postId: id,
layout,
tagName: TagName,
blockProps,
} ) {
useBlockEditingMode( 'disabled' );

const { content, editedBlocks } = useSelect(
( select ) => {
if ( ! id ) {
return {};
}
const { getEditedEntityRecord } = select( coreStore );
const editedRecord = getEditedEntityRecord(
'postType',
'wp_template_part',
id,
{ context: 'view' }
);
return {
editedBlocks: editedRecord.blocks,
content: editedRecord.content,
};
},
[ id ]
);

const blocks = useMemo( () => {
if ( ! id ) {
return undefined;
}

if ( editedBlocks ) {
return editedBlocks;
}

if ( ! content || typeof content !== 'string' ) {
return [];
}

return parse( content );
}, [ id, editedBlocks, content ] );

const innerBlocksProps = useInnerBlocksProps( blockProps, {
value: blocks,
onInput: () => {},
onChange: () => {},
renderAppender: false,
layout: useLayout( layout ),
} );

return <TagName { ...innerBlocksProps } />;
}

function EditableTemplatePartInnerBlocks( {
postId: id,
hasInnerBlocks,
layout,
Expand All @@ -59,3 +116,42 @@ export default function TemplatePartInnerBlocks( {

return <TagName { ...innerBlocksProps } />;
}

export default function TemplatePartInnerBlocks( {
postId: id,
hasInnerBlocks,
layout,
tagName: TagName,
blockProps,
} ) {
const { canViewTemplatePart, canEditTemplatePart } = useSelect(
( select ) => {
return {
canViewTemplatePart:
select( coreStore ).canUser( 'read', 'templates' ) ?? false,
canEditTemplatePart:
select( coreStore ).canUser( 'create', 'templates' ) ??
false,
};
},
[]
);

if ( ! canViewTemplatePart ) {
return null;
}

const TemplatePartInnerBlocksComponent = canEditTemplatePart
? EditableTemplatePartInnerBlocks
: NonEditableTemplatePartPreview;

return (
<TemplatePartInnerBlocksComponent
postId={ id }
hasInnerBlocks={ hasInnerBlocks }
layout={ layout }
tagName={ TagName }
blockProps={ blockProps }
/>
);
}

0 comments on commit 988454b

Please sign in to comment.