-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Expand file tree
/
Copy pathindex.js
More file actions
537 lines (509 loc) · 15.5 KB
/
Copy pathindex.js
File metadata and controls
537 lines (509 loc) · 15.5 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import {
getBlockType,
getUnregisteredTypeHandlerName,
store as blocksStore,
} from '@wordpress/blocks';
import {
ToggleControl,
__experimentalSpacer as Spacer,
__unstableMotion as motion,
} from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import { useRef } from '@wordpress/element';
/**
* Internal dependencies
*/
import EditContents from './edit-contents';
import SkipToSelectedBlock from '../skip-to-selected-block';
import BlockCard from '../block-card';
import MultiSelectionInspector from '../multi-selection-inspector';
import BlockVariationTransforms from '../block-variation-transforms';
import useBlockDisplayInformation from '../use-block-display-information';
import { store as blockEditorStore } from '../../store';
import BlockStyles from '../block-styles';
import { ListViewContentPopover } from '../inspector-controls/list-view-content-popover';
import InspectorControls from '../inspector-controls';
import { BlockInspectorPreTabsSlot } from './inspector-pre-tabs-slot-fill';
import { default as InspectorControlsTabs } from '../inspector-controls-tabs';
import useInspectorControlsTabs from '../inspector-controls-tabs/use-inspector-controls-tabs';
import InspectorControlsLastItem from '../inspector-controls/last-item';
import AdvancedControls from '../inspector-controls-tabs/advanced-controls-panel';
import PositionControls from '../inspector-controls-tabs/position-controls-panel';
import useBlockInspectorAnimationSettings from './useBlockInspectorAnimationSettings';
import { useBorderPanelLabel } from '../../hooks/border';
import { BlockStateBadges, BlockStatesControl } from '../../hooks/states';
import ContentTab from '../inspector-controls-tabs/content-tab';
import ViewportVisibilityInfo from '../block-visibility/viewport-visibility-info';
import { unlock } from '../../lock-unlock';
import {
hasPseudoBlockStyleState,
hasViewportBlockStyleState,
isDefaultBlockStyleState,
} from '../../hooks/block-style-state';
import { onViewportStateChangeKey } from '../../store/private-keys';
function StyleInspectorSlots( {
blockName,
showAdvancedControls = true,
showPositionControls = true,
showBindingsControls = true,
} ) {
const borderPanelLabel = useBorderPanelLabel( { blockName } );
return (
<>
<InspectorControls.Slot />
<InspectorControls.Slot
group="typography"
label={ __( 'Typography' ) }
/>
<InspectorControls.Slot
group="color"
label={ __( 'Color' ) }
className="color-block-support-panel__inner-wrapper"
/>
<InspectorControls.Slot
group="background"
label={ __( 'Background' ) }
className="background-block-support-panel__inner-wrapper"
/>
<InspectorControls.Slot group="layout" label={ __( 'Layout' ) } />
<InspectorControls.Slot
group="dimensions"
label={ __( 'Dimensions' ) }
/>
<InspectorControls.Slot group="border" label={ borderPanelLabel } />
<InspectorControls.Slot
group="elements"
label={ __( 'Elements' ) }
className="elements-block-support-panel__inner-wrapper"
/>
{ showPositionControls && <PositionControls /> }
<InspectorControls.Slot group="styles" />
{ showBindingsControls && (
<InspectorControls.Slot group="bindings" />
) }
{ showAdvancedControls && (
<div>
<AdvancedControls />
</div>
) }
</>
);
}
function StyleStateInspectorSlots( { blockName, selectedBlockStyleState } ) {
const borderPanelLabel = useBorderPanelLabel( { blockName } );
const showLayoutControls =
hasViewportBlockStyleState( selectedBlockStyleState ) &&
! hasPseudoBlockStyleState( selectedBlockStyleState );
return (
<>
<InspectorControls.Slot
group="typography"
label={ __( 'Typography' ) }
/>
<InspectorControls.Slot
group="color"
label={ __( 'Color' ) }
className="color-block-support-panel__inner-wrapper"
/>
<InspectorControls.Slot
group="background"
label={ __( 'Background' ) }
className="background-block-support-panel__inner-wrapper"
/>
{ showLayoutControls && (
<InspectorControls.Slot
group="layout"
label={ __( 'Layout' ) }
/>
) }
<InspectorControls.Slot
group="dimensions"
label={ __( 'Dimensions' ) }
/>
<InspectorControls.Slot group="border" label={ borderPanelLabel } />
<InspectorControls.Slot
group="elements"
label={ __( 'Elements' ) }
className="elements-block-support-panel__inner-wrapper"
/>
</>
);
}
function BlockInspector() {
const {
selectedBlockCount,
renderedBlockName,
renderedBlockClientId,
blockType,
isSectionBlock,
isSectionBlockInSelection,
hasBlockStyles,
editedContentOnlySection,
blockEditingMode,
selectedBlockStyleState,
showStateOnCanvas,
onViewportStateChange,
} = useSelect( ( select ) => {
const {
getSelectedBlockClientId,
getSelectedBlockClientIds,
getSelectedBlockCount,
getBlockName,
getParentSectionBlock,
isSectionBlock: _isSectionBlock,
getEditedContentOnlySection,
isWithinEditedContentOnlySection,
getBlockEditingMode,
getSelectedBlockStyleState,
isSelectedBlockStyleStateShownOnCanvas,
} = unlock( select( blockEditorStore ) );
const blockEditorSettings = select( blockEditorStore ).getSettings();
const { getBlockStyles } = select( blocksStore );
const _selectedBlockClientId = getSelectedBlockClientId();
const isWithinEditedSection = isWithinEditedContentOnlySection(
_selectedBlockClientId
);
const _renderedBlockClientId = isWithinEditedSection
? _selectedBlockClientId
: getParentSectionBlock( _selectedBlockClientId ) ||
_selectedBlockClientId;
const _renderedBlockName =
_renderedBlockClientId && getBlockName( _renderedBlockClientId );
const _blockType =
_renderedBlockName && getBlockType( _renderedBlockName );
const selectedBlockClientIds = getSelectedBlockClientIds();
const _isSectionBlockInSelection = selectedBlockClientIds.some(
( id ) => _isSectionBlock( id )
);
const blockStyles =
_renderedBlockName && getBlockStyles( _renderedBlockName );
const _hasBlockStyles = blockStyles && blockStyles.length > 0;
return {
selectedBlockCount: getSelectedBlockCount(),
renderedBlockClientId: _renderedBlockClientId,
renderedBlockName: _renderedBlockName,
blockType: _blockType,
isSectionBlockInSelection: _isSectionBlockInSelection,
isSectionBlock: _isSectionBlock( _renderedBlockClientId ),
hasBlockStyles: _hasBlockStyles,
editedContentOnlySection: getEditedContentOnlySection(),
blockEditingMode: getBlockEditingMode( _renderedBlockClientId ),
selectedBlockStyleState: getSelectedBlockStyleState(
_renderedBlockClientId
),
showStateOnCanvas: isSelectedBlockStyleStateShownOnCanvas(
_renderedBlockClientId
),
onViewportStateChange:
blockEditorSettings?.[ onViewportStateChangeKey ],
};
}, [] );
// Separate useSelect for contentClientIds with proper dependencies
const contentClientIds = useSelect(
( select ) => {
if ( ! isSectionBlock || ! renderedBlockClientId ) {
return [];
}
const {
getClientIdsOfDescendants,
getBlockEditingMode,
shouldRenderBlockListView,
} = unlock( select( blockEditorStore ) );
const descendants = getClientIdsOfDescendants(
renderedBlockClientId
);
// Exclude items from the content tab that are already present in the
// List View tab.
const listViewDescendants = new Set();
descendants.forEach( ( clientId ) => {
if ( shouldRenderBlockListView( clientId ) ) {
const listViewChildren =
getClientIdsOfDescendants( clientId );
listViewChildren.forEach( ( childId ) =>
listViewDescendants.add( childId )
);
}
} );
return descendants.filter( ( current ) => {
return (
! listViewDescendants.has( current ) &&
getBlockEditingMode( current ) === 'contentOnly'
);
} );
},
[ isSectionBlock, renderedBlockClientId ]
);
const availableTabs = useInspectorControlsTabs(
blockType?.name,
contentClientIds,
isSectionBlock,
hasBlockStyles
);
const hasMultipleTabs = availableTabs?.length > 1;
// The block inspector animation settings will be completely
// removed in the future to create an API which allows the block
// inspector to transition between what it
// displays based on the relationship between the selected block
// and its parent, and only enable it if the parent is controlling
// its children blocks.
const blockInspectorAnimationSettings =
useBlockInspectorAnimationSettings( blockType );
const hasSelectedBlocks = selectedBlockCount > 1;
const isBlockStyleStateSelected = ! isDefaultBlockStyleState(
selectedBlockStyleState
);
if ( hasSelectedBlocks && ! isSectionBlockInSelection ) {
return (
<div className="block-editor-block-inspector">
<MultiSelectionInspector />
{ hasMultipleTabs ? (
<InspectorControlsTabs tabs={ availableTabs } />
) : (
<StyleInspectorSlots
blockName={ renderedBlockName }
showAdvancedControls={ false }
showPositionControls={ false }
showBindingsControls={ false }
/>
) }
</div>
);
}
if ( hasSelectedBlocks && isSectionBlockInSelection ) {
return (
<div className="block-editor-block-inspector">
<MultiSelectionInspector />
</div>
);
}
const isRenderedBlockUnregistered =
renderedBlockName === getUnregisteredTypeHandlerName();
/*
* If the rendered block is of an unregistered type, avoid showing it as an actual selection
* because we want the user to focus on the unregistered block warning, not block settings.
*/
const shouldShowWarning =
! blockType || ! renderedBlockClientId || isRenderedBlockUnregistered;
if ( shouldShowWarning ) {
return (
<span className="block-editor-block-inspector__no-blocks">
{ __( 'No block selected.' ) }
</span>
);
}
return (
<BlockInspectorSingleBlockWrapper
animate={ blockInspectorAnimationSettings }
wrapper={ ( children ) => (
<AnimatedContainer
blockInspectorAnimationSettings={
blockInspectorAnimationSettings
}
renderedBlockClientId={ renderedBlockClientId }
>
{ children }
</AnimatedContainer>
) }
>
<BlockInspectorSingleBlock
renderedBlockClientId={ renderedBlockClientId }
blockName={ blockType.name }
isSectionBlock={ isSectionBlock }
availableTabs={ availableTabs }
contentClientIds={ contentClientIds }
hasBlockStyles={ hasBlockStyles }
editedContentOnlySection={ editedContentOnlySection }
blockEditingMode={ blockEditingMode }
selectedBlockStyleState={ selectedBlockStyleState }
showStateOnCanvas={ showStateOnCanvas }
onViewportStateChange={ onViewportStateChange }
isBlockStyleStateSelected={ isBlockStyleStateSelected }
/>
</BlockInspectorSingleBlockWrapper>
);
}
const BlockInspectorSingleBlockWrapper = ( { animate, wrapper, children } ) => {
return animate ? wrapper( children ) : children;
};
const AnimatedContainer = ( {
blockInspectorAnimationSettings,
renderedBlockClientId,
children,
} ) => {
const animationOrigin =
blockInspectorAnimationSettings &&
blockInspectorAnimationSettings.enterDirection === 'leftToRight'
? -50
: 50;
return (
<motion.div
animate={ {
x: 0,
opacity: 1,
transition: {
ease: 'easeInOut',
duration: 0.14,
},
} }
initial={ {
x: animationOrigin,
opacity: 0,
} }
key={ renderedBlockClientId }
>
{ children }
</motion.div>
);
};
const BlockInspectorSingleBlock = ( {
// The block that is displayed in the inspector. This is the block whose
// controls and information are shown to the user.
renderedBlockClientId,
blockName,
isSectionBlock,
availableTabs,
contentClientIds,
hasBlockStyles,
editedContentOnlySection,
blockEditingMode,
selectedBlockStyleState,
showStateOnCanvas,
onViewportStateChange,
isBlockStyleStateSelected,
} ) => {
const listViewRef = useRef( null );
const hasMultipleTabs = availableTabs?.length > 1;
const hasParentChildBlockCards =
editedContentOnlySection &&
editedContentOnlySection !== renderedBlockClientId;
const parentBlockInformation = useBlockDisplayInformation(
editedContentOnlySection
);
const blockInformation = useBlockDisplayInformation(
renderedBlockClientId
);
const isBlockSynced = blockInformation.isSynced;
const {
setSelectedBlockStyleState,
setSelectedBlockStyleStateCanvasPreview,
} = unlock( useDispatch( blockEditorStore ) );
const onBlockStyleStateChange = ( value ) => {
const nextSelectedBlockStyleState = {
...selectedBlockStyleState,
...value,
};
setSelectedBlockStyleState(
renderedBlockClientId,
nextSelectedBlockStyleState
);
if ( value.viewport ) {
onViewportStateChange?.( {
viewport: nextSelectedBlockStyleState.viewport,
showStateOnCanvas,
} );
}
};
const onShowStateOnCanvasChange = ( value ) => {
setSelectedBlockStyleStateCanvasPreview( renderedBlockClientId, value );
if ( value ) {
onViewportStateChange?.( {
viewport: selectedBlockStyleState.viewport,
showStateOnCanvas: value,
} );
}
};
return (
<div className="block-editor-block-inspector">
{ hasParentChildBlockCards && (
<BlockCard
{ ...parentBlockInformation }
className={
parentBlockInformation?.isSynced && 'is-synced'
}
parentClientId={ editedContentOnlySection }
/>
) }
<BlockCard
{ ...blockInformation }
allowParentNavigation
className={ isBlockSynced && 'is-synced' }
isChild={ hasParentChildBlockCards }
clientId={ renderedBlockClientId }
controls={
blockEditingMode === 'default' && (
<BlockStatesControl
name={ blockName }
value={ selectedBlockStyleState }
onChange={ onBlockStyleStateChange }
/>
)
}
/>
{ blockEditingMode === 'default' && isBlockStyleStateSelected && (
<Spacer paddingX={ 4 } paddingY={ 2 }>
<ToggleControl
label={ __( 'Show state on canvas' ) }
checked={ showStateOnCanvas }
onChange={ onShowStateOnCanvasChange }
/>
<BlockStateBadges
name={ blockName }
value={ selectedBlockStyleState }
/>
</Spacer>
) }
<ViewportVisibilityInfo clientId={ renderedBlockClientId } />
<EditContents clientId={ renderedBlockClientId } />
{ ! isBlockStyleStateSelected && (
<BlockVariationTransforms
blockClientId={ renderedBlockClientId }
/>
) }
<BlockInspectorPreTabsSlot />
{ isBlockStyleStateSelected && ! isSectionBlock && (
<StyleStateInspectorSlots
blockName={ blockName }
selectedBlockStyleState={ selectedBlockStyleState }
/>
) }
{ ! isBlockStyleStateSelected && hasMultipleTabs && (
<>
<InspectorControlsTabs
hasBlockStyles={ hasBlockStyles }
clientId={ renderedBlockClientId }
blockName={ blockName }
tabs={ availableTabs }
isSectionBlock={ isSectionBlock }
contentClientIds={ contentClientIds }
/>
</>
) }
{ ! isBlockStyleStateSelected && ! hasMultipleTabs && (
<>
{ hasBlockStyles && (
<BlockStyles clientId={ renderedBlockClientId } />
) }
<ContentTab contentClientIds={ contentClientIds } />
<InspectorControls.Slot group="content" />
<InspectorControls.Slot group="list" ref={ listViewRef } />
<ListViewContentPopover listViewRef={ listViewRef } />
{ ! isSectionBlock && (
<StyleInspectorSlots blockName={ blockName } />
) }
</>
) }
{ ! isBlockStyleStateSelected && (
<InspectorControlsLastItem.Slot />
) }
<SkipToSelectedBlock key="back" />
</div>
);
};
/**
* @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/block-editor/src/components/block-inspector/README.md
*/
export default BlockInspector;