/
index.js
305 lines (278 loc) · 8.24 KB
/
index.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
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useMergeRefs } from '@wordpress/compose';
import { forwardRef, useMemo } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import {
getBlockSupport,
store as blocksStore,
__unstableGetInnerBlocksProps as getInnerBlocksProps,
} from '@wordpress/blocks';
/**
* Internal dependencies
*/
import ButtonBlockAppender from './button-block-appender';
import DefaultBlockAppender from './default-block-appender';
import useNestedSettingsUpdate from './use-nested-settings-update';
import useInnerBlockTemplateSync from './use-inner-block-template-sync';
import useBlockContext from './use-block-context';
import { BlockListItems } from '../block-list';
import { BlockContextProvider } from '../block-context';
import { useBlockEditContext } from '../block-edit/context';
import useBlockSync from '../provider/use-block-sync';
import { store as blockEditorStore } from '../../store';
import useBlockDropZone from '../use-block-drop-zone';
import { useSettings } from '../use-settings';
const EMPTY_OBJECT = {};
function BlockContext( { children, clientId } ) {
const context = useBlockContext( clientId );
return (
<BlockContextProvider value={ context }>
{ children }
</BlockContextProvider>
);
}
/**
* InnerBlocks is a component which allows a single block to have multiple blocks
* as children. The UncontrolledInnerBlocks component is used whenever the inner
* blocks are not controlled by another entity. In other words, it is normally
* used for inner blocks in the post editor
*
* @param {Object} props The component props.
*/
function UncontrolledInnerBlocks( props ) {
const {
clientId,
allowedBlocks,
prioritizedInserterBlocks,
defaultBlock,
directInsert,
__experimentalDefaultBlock,
__experimentalDirectInsert,
template,
templateLock,
wrapperRef,
templateInsertUpdatesSelection,
__experimentalCaptureToolbars: captureToolbars,
__experimentalAppenderTagName,
renderAppender,
orientation,
placeholder,
layout,
name,
blockType,
innerBlocks,
parentLock,
} = props;
useNestedSettingsUpdate(
clientId,
parentLock,
allowedBlocks,
prioritizedInserterBlocks,
defaultBlock,
directInsert,
__experimentalDefaultBlock,
__experimentalDirectInsert,
templateLock,
captureToolbars,
orientation,
layout
);
useInnerBlockTemplateSync(
clientId,
innerBlocks,
template,
templateLock,
templateInsertUpdatesSelection
);
const defaultLayoutBlockSupport =
getBlockSupport( name, 'layout' ) ||
getBlockSupport( name, '__experimentalLayout' ) ||
EMPTY_OBJECT;
const { allowSizingOnChildren = false } = defaultLayoutBlockSupport;
const [ defaultLayout ] = useSettings( 'layout' );
const usedLayout = layout || defaultLayoutBlockSupport;
const memoedLayout = useMemo(
() => ( {
// Default layout will know about any content/wide size defined by the theme.
...defaultLayout,
...usedLayout,
...( allowSizingOnChildren && {
allowSizingOnChildren: true,
} ),
} ),
[ defaultLayout, usedLayout, allowSizingOnChildren ]
);
const items = (
<BlockListItems
rootClientId={ clientId }
renderAppender={ renderAppender }
__experimentalAppenderTagName={ __experimentalAppenderTagName }
layout={ memoedLayout }
wrapperRef={ wrapperRef }
placeholder={ placeholder }
/>
);
if ( Object.keys( blockType.providesContext ).length === 0 ) {
return items;
}
return <BlockContext clientId={ clientId }>{ items }</BlockContext>;
}
/**
* The controlled inner blocks component wraps the uncontrolled inner blocks
* component with the blockSync hook. This keeps the innerBlocks of the block in
* the block-editor store in sync with the blocks of the controlling entity. An
* example of an inner block controller is a template part block, which provides
* its own blocks from the template part entity data source.
*
* @param {Object} props The component props.
*/
function ControlledInnerBlocks( props ) {
useBlockSync( props );
return <UncontrolledInnerBlocks { ...props } />;
}
const ForwardedInnerBlocks = forwardRef( ( props, ref ) => {
const innerBlocksProps = useInnerBlocksProps( { ref }, props );
return (
<div className="block-editor-inner-blocks">
<div { ...innerBlocksProps } />
</div>
);
} );
/**
* This hook is used to lightly mark an element as an inner blocks wrapper
* element. Call this hook and pass the returned props to the element to mark as
* an inner blocks wrapper, automatically rendering inner blocks as children. If
* you define a ref for the element, it is important to pass the ref to this
* hook, which the hook in turn will pass to the component through the props it
* returns. Optionally, you can also pass any other props through this hook, and
* they will be merged and returned.
*
* @param {Object} props Optional. Props to pass to the element. Must contain
* the ref if one is defined.
* @param {Object} options Optional. Inner blocks options.
*
* @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/block-editor/src/components/inner-blocks/README.md
*/
export function useInnerBlocksProps( props = {}, options = {} ) {
const {
__unstableDisableLayoutClassNames,
__unstableDisableDropZone,
dropZoneElement,
} = options;
const {
clientId,
layout = null,
__unstableLayoutClassNames: layoutClassNames = '',
} = useBlockEditContext();
const {
__experimentalCaptureToolbars,
hasOverlay,
name,
blockType,
innerBlocks,
parentLock,
parentClientId,
isDropZoneDisabled,
} = useSelect(
( select ) => {
if ( ! clientId ) {
return {};
}
const {
getBlockName,
isBlockSelected,
hasSelectedInnerBlock,
__unstableGetEditorMode,
getBlocks,
getTemplateLock,
getBlockRootClientId,
__unstableIsWithinBlockOverlay,
__unstableHasActiveBlockOverlayActive,
getBlockEditingMode,
} = select( blockEditorStore );
const { hasBlockSupport, getBlockType } = select( blocksStore );
const blockName = getBlockName( clientId );
const enableClickThrough =
__unstableGetEditorMode() === 'navigation';
const blockEditingMode = getBlockEditingMode( clientId );
const _parentClientId = getBlockRootClientId( clientId );
return {
__experimentalCaptureToolbars: hasBlockSupport(
blockName,
'__experimentalExposeControlsToChildren',
false
),
hasOverlay:
blockName !== 'core/template' &&
! isBlockSelected( clientId ) &&
! hasSelectedInnerBlock( clientId, true ) &&
enableClickThrough,
name: blockName,
blockType: getBlockType( blockName ),
innerBlocks: getBlocks( clientId ),
parentLock: getTemplateLock( _parentClientId ),
parentClientId: _parentClientId,
isDropZoneDisabled:
blockEditingMode !== 'default' ||
__unstableHasActiveBlockOverlayActive( clientId ) ||
__unstableIsWithinBlockOverlay( clientId ),
};
},
[ clientId ]
);
const blockDropZoneRef = useBlockDropZone( {
dropZoneElement,
rootClientId: clientId,
parentClientId,
isDisabled: isDropZoneDisabled,
} );
const ref = useMergeRefs( [
props.ref,
__unstableDisableDropZone ? null : blockDropZoneRef,
] );
const innerBlocksProps = {
__experimentalCaptureToolbars,
layout,
name,
blockType,
innerBlocks,
parentLock,
...options,
};
const InnerBlocks =
innerBlocksProps.value && innerBlocksProps.onChange
? ControlledInnerBlocks
: UncontrolledInnerBlocks;
return {
...props,
ref,
className: classnames(
props.className,
'block-editor-block-list__layout',
__unstableDisableLayoutClassNames ? '' : layoutClassNames,
{
'has-overlay': hasOverlay,
}
),
children: clientId ? (
<InnerBlocks { ...innerBlocksProps } clientId={ clientId } />
) : (
<BlockListItems { ...options } />
),
};
}
useInnerBlocksProps.save = getInnerBlocksProps;
// Expose default appender placeholders as components.
ForwardedInnerBlocks.DefaultBlockAppender = DefaultBlockAppender;
ForwardedInnerBlocks.ButtonBlockAppender = ButtonBlockAppender;
ForwardedInnerBlocks.Content = () => useInnerBlocksProps.save().children;
/**
* @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/block-editor/src/components/inner-blocks/README.md
*/
export default ForwardedInnerBlocks;