-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathinner-blocks.js
181 lines (161 loc) · 4.02 KB
/
inner-blocks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* WordPress dependencies
*/
import { useEntityBlockEditor, store as coreStore } from '@wordpress/core-data';
import {
InnerBlocks,
useInnerBlocksProps,
useSettings,
store as blockEditorStore,
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();
// Disable appending when the editing mode is 'contentOnly'. This is so that the user can't
// append into a template part when editing a page in the site editor. See
// DisableNonPageContentBlocks. Ideally instead of (mis)using editing mode there would be a
// block editor API for achieving this.
if ( blockEditingMode === 'contentOnly' ) {
return false;
}
if ( ! hasInnerBlocks ) {
return InnerBlocks.ButtonBlockAppender;
}
}
function useLayout( layout ) {
const themeSupportsLayout = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
return getSettings()?.supportsLayout;
}, [] );
const [ defaultLayout ] = useSettings( 'layout' );
if ( themeSupportsLayout ) {
return layout?.inherit ? defaultLayout || {} : layout;
}
}
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,
tagName: TagName,
blockProps,
} ) {
const onNavigateToEntityRecord = useSelect(
( select ) =>
select( blockEditorStore ).getSettings().onNavigateToEntityRecord,
[]
);
const [ blocks, onInput, onChange ] = useEntityBlockEditor(
'postType',
'wp_template_part',
{ id }
);
const innerBlocksProps = useInnerBlocksProps( blockProps, {
value: blocks,
onInput,
onChange,
renderAppender: useRenderAppender( hasInnerBlocks ),
layout: useLayout( layout ),
} );
const blockEditingMode = useBlockEditingMode();
const customProps =
blockEditingMode === 'contentOnly' && onNavigateToEntityRecord
? {
onDoubleClick: () =>
onNavigateToEntityRecord( {
postId: id,
postType: 'wp_template_part',
} ),
}
: {};
return <TagName { ...innerBlocksProps } { ...customProps } />;
}
export default function TemplatePartInnerBlocks( {
postId: id,
hasInnerBlocks,
layout,
tagName: TagName,
blockProps,
} ) {
const { canViewTemplatePart, canEditTemplatePart } = useSelect(
( select ) => {
return {
canViewTemplatePart: !! select( coreStore ).canUser( 'read', {
kind: 'postType',
name: 'wp_template_part',
id,
} ),
canEditTemplatePart: !! select( coreStore ).canUser( 'update', {
kind: 'postType',
name: 'wp_template_part',
id,
} ),
};
},
[ id ]
);
if ( ! canViewTemplatePart ) {
return null;
}
const TemplatePartInnerBlocksComponent = canEditTemplatePart
? EditableTemplatePartInnerBlocks
: NonEditableTemplatePartPreview;
return (
<TemplatePartInnerBlocksComponent
postId={ id }
hasInnerBlocks={ hasInnerBlocks }
layout={ layout }
tagName={ TagName }
blockProps={ blockProps }
/>
);
}