-
Notifications
You must be signed in to change notification settings - Fork 4k
/
inner-blocks.js
135 lines (121 loc) · 3.3 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
/**
* WordPress dependencies
*/
import { useEntityBlockEditor } from '@wordpress/core-data';
import {
useInnerBlocksProps,
__experimentalBlockContentOverlay as BlockContentOverlay,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import { useMemo } from '@wordpress/element';
/**
* Internal dependencies
*/
import PlaceholderPreview from './placeholder/placeholder-preview';
const ALLOWED_BLOCKS = [
'core/navigation-link',
'core/search',
'core/social-links',
'core/page-list',
'core/spacer',
'core/home-link',
'core/site-title',
'core/site-logo',
'core/navigation-submenu',
];
const DEFAULT_BLOCK = [ 'core/navigation-link' ];
const LAYOUT = {
type: 'default',
alignments: [],
};
export default function NavigationInnerBlocks( {
isVisible,
clientId,
appender: CustomAppender,
hasCustomPlaceholder,
orientation,
} ) {
const {
isImmediateParentOfSelectedBlock,
selectedBlockHasDescendants,
isSelected,
} = useSelect(
( select ) => {
const {
getClientIdsOfDescendants,
hasSelectedInnerBlock,
getSelectedBlockClientId,
} = select( blockEditorStore );
const selectedBlockId = getSelectedBlockClientId();
return {
isImmediateParentOfSelectedBlock: hasSelectedInnerBlock(
clientId,
false
),
selectedBlockHasDescendants: !! getClientIdsOfDescendants( [
selectedBlockId,
] )?.length,
// This prop is already available but computing it here ensures it's
// fresh compared to isImmediateParentOfSelectedBlock
isSelected: selectedBlockId === clientId,
};
},
[ clientId ]
);
const [ blocks, onInput, onChange ] = useEntityBlockEditor(
'postType',
'wp_navigation'
);
const shouldDirectInsert = useMemo(
() =>
blocks.every(
( { name } ) =>
name === 'core/navigation-link' ||
name === 'core/navigation-submenu' ||
name === 'core/page-list'
),
[ blocks ]
);
// When the block is selected itself or has a top level item selected that
// doesn't itself have children, show the standard appender. Else show no
// appender.
const parentOrChildHasSelection =
isSelected ||
( isImmediateParentOfSelectedBlock && ! selectedBlockHasDescendants );
const appender = isVisible && parentOrChildHasSelection ? undefined : false;
const placeholder = useMemo( () => <PlaceholderPreview />, [] );
const innerBlocksProps = useInnerBlocksProps(
{
className: 'wp-block-navigation__container',
},
{
value: blocks,
onInput,
onChange,
allowedBlocks: ALLOWED_BLOCKS,
__experimentalDefaultBlock: DEFAULT_BLOCK,
__experimentalDirectInsert: shouldDirectInsert,
orientation,
renderAppender: CustomAppender || appender,
// Ensure block toolbar is not too far removed from item
// being edited when in vertical mode.
// see: https://github.com/WordPress/gutenberg/pull/34615.
__experimentalCaptureToolbars: orientation !== 'vertical',
// Template lock set to false here so that the Nav
// Block on the experimental menus screen does not
// inherit templateLock={ 'all' }.
templateLock: false,
__experimentalLayout: LAYOUT,
placeholder:
! isVisible || hasCustomPlaceholder ? undefined : placeholder,
}
);
return (
<BlockContentOverlay
clientId={ clientId }
tagName={ 'div' }
wrapperProps={ innerBlocksProps }
/>
);
}