Skip to content

Commit 792b205

Browse files
Render non-editable preview of template part when user does not have capability to edit template part (#60326)
Co-authored-by: fabiankaegy <[email protected]> Co-authored-by: youknowriad <[email protected]>
1 parent 24e550f commit 792b205

File tree

1 file changed

+98
-2
lines changed

1 file changed

+98
-2
lines changed

packages/block-library/src/template-part/edit/inner-blocks.js

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* WordPress dependencies
33
*/
4-
import { useEntityBlockEditor } from '@wordpress/core-data';
4+
import { useEntityBlockEditor, store as coreStore } from '@wordpress/core-data';
55
import {
66
InnerBlocks,
77
useInnerBlocksProps,
@@ -10,6 +10,8 @@ import {
1010
useBlockEditingMode,
1111
} from '@wordpress/block-editor';
1212
import { useSelect } from '@wordpress/data';
13+
import { useMemo } from '@wordpress/element';
14+
import { parse } from '@wordpress/blocks';
1315

1416
function useRenderAppender( hasInnerBlocks ) {
1517
const blockEditingMode = useBlockEditingMode();
@@ -36,7 +38,62 @@ function useLayout( layout ) {
3638
}
3739
}
3840

39-
export default function TemplatePartInnerBlocks( {
41+
function NonEditableTemplatePartPreview( {
42+
postId: id,
43+
layout,
44+
tagName: TagName,
45+
blockProps,
46+
} ) {
47+
useBlockEditingMode( 'disabled' );
48+
49+
const { content, editedBlocks } = useSelect(
50+
( select ) => {
51+
if ( ! id ) {
52+
return {};
53+
}
54+
const { getEditedEntityRecord } = select( coreStore );
55+
const editedRecord = getEditedEntityRecord(
56+
'postType',
57+
'wp_template_part',
58+
id,
59+
{ context: 'view' }
60+
);
61+
return {
62+
editedBlocks: editedRecord.blocks,
63+
content: editedRecord.content,
64+
};
65+
},
66+
[ id ]
67+
);
68+
69+
const blocks = useMemo( () => {
70+
if ( ! id ) {
71+
return undefined;
72+
}
73+
74+
if ( editedBlocks ) {
75+
return editedBlocks;
76+
}
77+
78+
if ( ! content || typeof content !== 'string' ) {
79+
return [];
80+
}
81+
82+
return parse( content );
83+
}, [ id, editedBlocks, content ] );
84+
85+
const innerBlocksProps = useInnerBlocksProps( blockProps, {
86+
value: blocks,
87+
onInput: () => {},
88+
onChange: () => {},
89+
renderAppender: false,
90+
layout: useLayout( layout ),
91+
} );
92+
93+
return <TagName { ...innerBlocksProps } />;
94+
}
95+
96+
function EditableTemplatePartInnerBlocks( {
4097
postId: id,
4198
hasInnerBlocks,
4299
layout,
@@ -59,3 +116,42 @@ export default function TemplatePartInnerBlocks( {
59116

60117
return <TagName { ...innerBlocksProps } />;
61118
}
119+
120+
export default function TemplatePartInnerBlocks( {
121+
postId: id,
122+
hasInnerBlocks,
123+
layout,
124+
tagName: TagName,
125+
blockProps,
126+
} ) {
127+
const { canViewTemplatePart, canEditTemplatePart } = useSelect(
128+
( select ) => {
129+
return {
130+
canViewTemplatePart:
131+
select( coreStore ).canUser( 'read', 'templates' ) ?? false,
132+
canEditTemplatePart:
133+
select( coreStore ).canUser( 'create', 'templates' ) ??
134+
false,
135+
};
136+
},
137+
[]
138+
);
139+
140+
if ( ! canViewTemplatePart ) {
141+
return null;
142+
}
143+
144+
const TemplatePartInnerBlocksComponent = canEditTemplatePart
145+
? EditableTemplatePartInnerBlocks
146+
: NonEditableTemplatePartPreview;
147+
148+
return (
149+
<TemplatePartInnerBlocksComponent
150+
postId={ id }
151+
hasInnerBlocks={ hasInnerBlocks }
152+
layout={ layout }
153+
tagName={ TagName }
154+
blockProps={ blockProps }
155+
/>
156+
);
157+
}

0 commit comments

Comments
 (0)