1
1
/**
2
2
* WordPress dependencies
3
3
*/
4
- import { useEntityBlockEditor } from '@wordpress/core-data' ;
4
+ import { useEntityBlockEditor , store as coreStore } from '@wordpress/core-data' ;
5
5
import {
6
6
InnerBlocks ,
7
7
useInnerBlocksProps ,
@@ -10,6 +10,8 @@ import {
10
10
useBlockEditingMode ,
11
11
} from '@wordpress/block-editor' ;
12
12
import { useSelect } from '@wordpress/data' ;
13
+ import { useMemo } from '@wordpress/element' ;
14
+ import { parse } from '@wordpress/blocks' ;
13
15
14
16
function useRenderAppender ( hasInnerBlocks ) {
15
17
const blockEditingMode = useBlockEditingMode ( ) ;
@@ -36,7 +38,62 @@ function useLayout( layout ) {
36
38
}
37
39
}
38
40
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 ( {
40
97
postId : id ,
41
98
hasInnerBlocks,
42
99
layout,
@@ -59,3 +116,42 @@ export default function TemplatePartInnerBlocks( {
59
116
60
117
return < TagName { ...innerBlocksProps } /> ;
61
118
}
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