-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
edit.js
316 lines (280 loc) · 7.36 KB
/
edit.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/**
* External dependencies
*/
import clsx from 'clsx';
/**
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { useRef, useMemo, useEffect } from '@wordpress/element';
import {
useEntityRecord,
store as coreStore,
useEntityBlockEditor,
} from '@wordpress/core-data';
import {
Placeholder,
Spinner,
ToolbarButton,
ToolbarGroup,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import {
useInnerBlocksProps,
RecursionProvider,
useHasRecursion,
useBlockProps,
Warning,
privateApis as blockEditorPrivateApis,
store as blockEditorStore,
BlockControls,
} from '@wordpress/block-editor';
import { privateApis as patternsPrivateApis } from '@wordpress/patterns';
import { store as blocksStore } from '@wordpress/blocks';
/**
* Internal dependencies
*/
import { name as patternBlockName } from './index';
import { unlock } from '../lock-unlock';
const { useLayoutClasses } = unlock( blockEditorPrivateApis );
const { isOverridableBlock, hasOverridableBlocks } =
unlock( patternsPrivateApis );
const fullAlignments = [ 'full', 'wide', 'left', 'right' ];
const useInferredLayout = ( blocks, parentLayout ) => {
const initialInferredAlignmentRef = useRef();
return useMemo( () => {
// Exit early if the pattern's blocks haven't loaded yet.
if ( ! blocks?.length ) {
return {};
}
let alignment = initialInferredAlignmentRef.current;
// Only track the initial alignment so that temporarily removed
// alignments can be reapplied.
if ( alignment === undefined ) {
const isConstrained = parentLayout?.type === 'constrained';
const hasFullAlignment = blocks.some( ( block ) =>
fullAlignments.includes( block.attributes.align )
);
alignment = isConstrained && hasFullAlignment ? 'full' : null;
initialInferredAlignmentRef.current = alignment;
}
const layout = alignment ? parentLayout : undefined;
return { alignment, layout };
}, [ blocks, parentLayout ] );
};
function setBlockEditMode( setEditMode, blocks, mode ) {
blocks.forEach( ( block ) => {
const editMode =
mode ||
( isOverridableBlock( block ) ? 'contentOnly' : 'disabled' );
setEditMode( block.clientId, editMode );
setBlockEditMode(
setEditMode,
block.innerBlocks,
// Disable editing for nested patterns.
block.name === patternBlockName ? 'disabled' : mode
);
} );
}
function RecursionWarning() {
const blockProps = useBlockProps();
return (
<div { ...blockProps }>
<Warning>
{ __( 'Block cannot be rendered inside itself.' ) }
</Warning>
</div>
);
}
const NOOP = () => {};
// Wrap the main Edit function for the pattern block with a recursion wrapper
// that allows short-circuiting rendering as early as possible, before any
// of the other effects in the block edit have run.
export default function ReusableBlockEditRecursionWrapper( props ) {
const { ref } = props.attributes;
const hasAlreadyRendered = useHasRecursion( ref );
if ( hasAlreadyRendered ) {
return <RecursionWarning />;
}
return (
<RecursionProvider uniqueId={ ref }>
<ReusableBlockEdit { ...props } />
</RecursionProvider>
);
}
function ReusableBlockControl( {
recordId,
canOverrideBlocks,
hasContent,
handleEditOriginal,
resetContent,
} ) {
const canUserEdit = useSelect(
( select ) =>
!! select( coreStore ).canUser( 'update', {
kind: 'postType',
name: 'wp_block',
id: recordId,
} ),
[ recordId ]
);
return (
<>
{ canUserEdit && !! handleEditOriginal && (
<BlockControls>
<ToolbarGroup>
<ToolbarButton onClick={ handleEditOriginal }>
{ __( 'Edit original' ) }
</ToolbarButton>
</ToolbarGroup>
</BlockControls>
) }
{ canOverrideBlocks && (
<BlockControls>
<ToolbarGroup>
<ToolbarButton
onClick={ resetContent }
disabled={ ! hasContent }
>
{ __( 'Reset' ) }
</ToolbarButton>
</ToolbarGroup>
</BlockControls>
) }
</>
);
}
function ReusableBlockEdit( {
name,
attributes: { ref, content },
__unstableParentLayout: parentLayout,
clientId: patternClientId,
setAttributes,
} ) {
const { record, hasResolved } = useEntityRecord(
'postType',
'wp_block',
ref
);
const [ blocks ] = useEntityBlockEditor( 'postType', 'wp_block', {
id: ref,
} );
const isMissing = hasResolved && ! record;
const { setBlockEditingMode, __unstableMarkLastChangeAsPersistent } =
useDispatch( blockEditorStore );
const {
innerBlocks,
onNavigateToEntityRecord,
editingMode,
hasPatternOverridesSource,
} = useSelect(
( select ) => {
const {
getBlocks,
getSettings,
getBlockEditingMode: _getBlockEditingMode,
} = select( blockEditorStore );
const { getBlockBindingsSource } = unlock( select( blocksStore ) );
// For editing link to the site editor if the theme and user permissions support it.
return {
innerBlocks: getBlocks( patternClientId ),
getBlockEditingMode: _getBlockEditingMode,
onNavigateToEntityRecord:
getSettings().onNavigateToEntityRecord,
editingMode: _getBlockEditingMode( patternClientId ),
hasPatternOverridesSource: !! getBlockBindingsSource(
'core/pattern-overrides'
),
};
},
[ patternClientId ]
);
// Sync the editing mode of the pattern block with the inner blocks.
useEffect( () => {
setBlockEditMode(
setBlockEditingMode,
innerBlocks,
// Disable editing if the pattern itself is disabled.
editingMode === 'disabled' || ! hasPatternOverridesSource
? 'disabled'
: undefined
);
}, [
editingMode,
innerBlocks,
setBlockEditingMode,
hasPatternOverridesSource,
] );
const canOverrideBlocks = useMemo(
() => hasPatternOverridesSource && hasOverridableBlocks( blocks ),
[ hasPatternOverridesSource, blocks ]
);
const { alignment, layout } = useInferredLayout( blocks, parentLayout );
const layoutClasses = useLayoutClasses( { layout }, name );
const blockProps = useBlockProps( {
className: clsx(
'block-library-block__reusable-block-container',
layout && layoutClasses,
{ [ `align${ alignment }` ]: alignment }
),
} );
// Use `blocks` variable until `innerBlocks` is populated, which has the proper clientIds.
const innerBlocksProps = useInnerBlocksProps( blockProps, {
templateLock: 'all',
layout,
value: innerBlocks.length > 0 ? innerBlocks : blocks,
onInput: NOOP,
onChange: NOOP,
renderAppender: blocks?.length ? undefined : blocks.ButtonBlockAppender,
} );
const handleEditOriginal = () => {
onNavigateToEntityRecord( {
postId: ref,
postType: 'wp_block',
} );
};
const resetContent = () => {
if ( content ) {
// Make sure any previous changes are persisted before resetting.
__unstableMarkLastChangeAsPersistent();
setAttributes( { content: undefined } );
}
};
let children = null;
if ( isMissing ) {
children = (
<Warning>
{ __( 'Block has been deleted or is unavailable.' ) }
</Warning>
);
}
if ( ! hasResolved ) {
children = (
<Placeholder>
<Spinner />
</Placeholder>
);
}
return (
<>
{ hasResolved && (
<ReusableBlockControl
recordId={ ref }
canOverrideBlocks={ canOverrideBlocks }
hasContent={ !! content }
handleEditOriginal={
onNavigateToEntityRecord
? handleEditOriginal
: undefined
}
resetContent={ resetContent }
/>
) }
{ children === null ? (
<div { ...innerBlocksProps } />
) : (
<div { ...blockProps }>{ children }</div>
) }
</>
);
}