-
Notifications
You must be signed in to change notification settings - Fork 4k
/
menu.js
242 lines (227 loc) · 5.69 KB
/
menu.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
/**
* WordPress dependencies
*/
import {
forwardRef,
useState,
useCallback,
useMemo,
useImperativeHandle,
useRef,
} from '@wordpress/element';
import { VisuallyHidden, SearchControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
/**
* Internal dependencies
*/
import Tips from './tips';
import InserterPreviewPanel from './preview-panel';
import BlockTypesTab from './block-types-tab';
import BlockPatternsTabs from './block-patterns-tab';
import ReusableBlocksTab from './reusable-blocks-tab';
import InserterSearchResults from './search-results';
import useInsertionPoint from './hooks/use-insertion-point';
import InserterTabs from './tabs';
import { store as blockEditorStore } from '../../store';
function InserterMenu(
{
rootClientId,
clientId,
isAppender,
__experimentalInsertionIndex,
onSelect,
showInserterHelpPanel,
showMostUsedBlocks,
__experimentalFilterValue = '',
shouldFocusBlock = true,
prioritizePatterns,
},
ref
) {
const [ filterValue, setFilterValue ] = useState(
__experimentalFilterValue
);
const [ hoveredItem, setHoveredItem ] = useState( null );
const [ selectedPatternCategory, setSelectedPatternCategory ] = useState(
null
);
const [
destinationRootClientId,
onInsertBlocks,
onToggleInsertionPoint,
] = useInsertionPoint( {
rootClientId,
clientId,
isAppender,
insertionIndex: __experimentalInsertionIndex,
shouldFocusBlock,
} );
const { showPatterns, hasReusableBlocks } = useSelect(
( select ) => {
const { __experimentalGetAllowedPatterns, getSettings } = select(
blockEditorStore
);
return {
showPatterns: !! __experimentalGetAllowedPatterns(
destinationRootClientId
).length,
hasReusableBlocks: !! getSettings().__experimentalReusableBlocks
?.length,
};
},
[ destinationRootClientId ]
);
const onInsert = useCallback(
( blocks, meta, shouldForceFocusBlock ) => {
onInsertBlocks( blocks, meta, shouldForceFocusBlock );
onSelect();
},
[ onInsertBlocks, onSelect ]
);
const onInsertPattern = useCallback(
( blocks, patternName ) => {
onInsertBlocks( blocks, { patternName } );
onSelect();
},
[ onInsertBlocks, onSelect ]
);
const onHover = useCallback(
( item ) => {
onToggleInsertionPoint( !! item );
setHoveredItem( item );
},
[ onToggleInsertionPoint, setHoveredItem ]
);
const onClickPatternCategory = useCallback(
( patternCategory ) => {
setSelectedPatternCategory( patternCategory );
},
[ setSelectedPatternCategory ]
);
const blocksTab = useMemo(
() => (
<>
<div className="block-editor-inserter__block-list">
<BlockTypesTab
rootClientId={ destinationRootClientId }
onInsert={ onInsert }
onHover={ onHover }
showMostUsedBlocks={ showMostUsedBlocks }
/>
</div>
{ showInserterHelpPanel && (
<div className="block-editor-inserter__tips">
<VisuallyHidden as="h2">
{ __( 'A tip for using the block editor' ) }
</VisuallyHidden>
<Tips />
</div>
) }
</>
),
[
destinationRootClientId,
onInsert,
onHover,
filterValue,
showMostUsedBlocks,
showInserterHelpPanel,
]
);
const patternsTab = useMemo(
() => (
<BlockPatternsTabs
rootClientId={ destinationRootClientId }
onInsert={ onInsertPattern }
onClickCategory={ onClickPatternCategory }
selectedCategory={ selectedPatternCategory }
/>
),
[
destinationRootClientId,
onInsertPattern,
onClickPatternCategory,
selectedPatternCategory,
]
);
const reusableBlocksTab = useMemo(
() => (
<ReusableBlocksTab
rootClientId={ destinationRootClientId }
onInsert={ onInsert }
onHover={ onHover }
/>
),
[ destinationRootClientId, onInsert, onHover ]
);
const getCurrentTab = useCallback(
( tab ) => {
if ( tab.name === 'blocks' ) {
return blocksTab;
} else if ( tab.name === 'patterns' ) {
return patternsTab;
}
return reusableBlocksTab;
},
[ blocksTab, patternsTab, reusableBlocksTab ]
);
const searchRef = useRef();
useImperativeHandle( ref, () => ( {
focusSearch: () => {
searchRef.current.focus();
},
} ) );
return (
<div className="block-editor-inserter__menu">
<div className="block-editor-inserter__main-area">
{ /* The following div is necessary to fix the sticky position of the search form. */ }
<div className="block-editor-inserter__content">
<SearchControl
className="block-editor-inserter__search"
onChange={ ( value ) => {
if ( hoveredItem ) setHoveredItem( null );
setFilterValue( value );
} }
value={ filterValue }
label={ __( 'Search for blocks and patterns' ) }
placeholder={ __( 'Search' ) }
ref={ searchRef }
/>
{ !! filterValue && (
<InserterSearchResults
filterValue={ filterValue }
onSelect={ onSelect }
onHover={ onHover }
rootClientId={ rootClientId }
clientId={ clientId }
isAppender={ isAppender }
__experimentalInsertionIndex={
__experimentalInsertionIndex
}
showBlockDirectory
shouldFocusBlock={ shouldFocusBlock }
/>
) }
{ ! filterValue && ( showPatterns || hasReusableBlocks ) && (
<InserterTabs
showPatterns={ showPatterns }
showReusableBlocks={ hasReusableBlocks }
prioritizePatterns={ prioritizePatterns }
>
{ getCurrentTab }
</InserterTabs>
) }
{ ! filterValue &&
! showPatterns &&
! hasReusableBlocks &&
blocksTab }
</div>
</div>
{ showInserterHelpPanel && hoveredItem && (
<InserterPreviewPanel item={ hoveredItem } />
) }
</div>
);
}
export default forwardRef( InserterMenu );