-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathmenu.native.js
223 lines (196 loc) · 5.43 KB
/
menu.native.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
/**
* External dependencies
*/
import { View } from 'react-native';
/**
* WordPress dependencies
*/
import { useEffect, useState, useCallback } from '@wordpress/element';
import { useSelect, useDispatch } from '@wordpress/data';
import {
createBlock,
rawHandler,
store as blocksStore,
} from '@wordpress/blocks';
import {
BottomSheet,
BottomSheetConsumer,
getClipboard,
} from '@wordpress/components';
/**
* Internal dependencies
*/
import InserterSearchResults from './search-results';
import InserterSearchForm from './search-form';
import { store as blockEditorStore } from '../../store';
import { searchItems } from './search-items';
const MIN_ITEMS_FOR_SEARCH = 2;
function InserterMenu( {
onSelect,
onDismiss,
rootClientId,
clientId,
isAppender,
shouldReplaceBlock,
insertionIndex,
} ) {
const [ filterValue, setFilterValue ] = useState( '' );
const [ searchFormHeight, setSearchFormHeight ] = useState( 0 );
// eslint-disable-next-line no-undef
const [ showSearchForm, setShowSearchForm ] = useState( __DEV__ );
const {
showInsertionPoint,
hideInsertionPoint,
clearSelectedBlock,
insertBlock,
removeBlock,
resetBlocks,
insertDefaultBlock,
} = useDispatch( blockEditorStore );
const {
items,
destinationRootClientId,
getBlockOrder,
getBlockCount,
canInsertBlockType,
} = useSelect( ( select ) => {
const {
getInserterItems,
getBlockRootClientId,
getBlockSelectionEnd,
...selectBlockEditorStore
} = select( blockEditorStore );
let targetRootClientId = rootClientId;
if ( ! targetRootClientId && ! clientId && ! isAppender ) {
const end = getBlockSelectionEnd();
if ( end ) {
targetRootClientId = getBlockRootClientId( end ) || undefined;
}
}
return {
items: getInserterItems( targetRootClientId ),
destinationRootClientId: targetRootClientId,
getBlockOrder: selectBlockEditorStore.getBlockOrder,
getBlockCount: selectBlockEditorStore.getBlockCount,
canInsertBlockType: selectBlockEditorStore.canInsertBlockType,
};
} );
const { getBlockType } = useSelect( ( select ) => select( blocksStore ) );
useEffect( () => {
// Show/Hide insertion point on Mount/Dismount
if ( shouldReplaceBlock ) {
const count = getBlockCount();
// Check if there is a rootClientId because that means it is a nested replaceable block
// and we don't want to clear/reset all blocks.
if ( count === 1 && ! rootClientId ) {
// Removing the last block is not possilble with `removeBlock` action.
// It always inserts a default block if the last of the blocks have been removed.
clearSelectedBlock();
resetBlocks( [] );
} else {
const blockToReplace = getBlockOrder( destinationRootClientId )[
insertionIndex
];
removeBlock( blockToReplace, false );
}
}
showInsertionPoint( destinationRootClientId, insertionIndex );
// Show search form if there are enough items to filter.
if ( getItems()?.length < MIN_ITEMS_FOR_SEARCH ) {
setShowSearchForm( false );
}
return hideInsertionPoint;
}, [] );
const onClose = useCallback( () => {
// if should replace but didn't insert any block
// re-insert default block
if ( shouldReplaceBlock ) {
insertDefaultBlock( {}, destinationRootClientId, insertionIndex );
}
onDismiss();
}, [ shouldReplaceBlock, destinationRootClientId, insertionIndex ] );
const onInsert = useCallback(
( item ) => {
const { name, initialAttributes, innerBlocks } = item;
const newBlock = createBlock(
name,
initialAttributes,
innerBlocks
);
insertBlock( newBlock, insertionIndex, destinationRootClientId );
},
[ insertBlock, destinationRootClientId, insertionIndex ]
);
/**
* Processes the inserter items to check
* if there's any copied block in the clipboard
* to add it as an extra item
*/
function getItems() {
// Filter out reusable blocks (they will be added in another tab)
let itemsToDisplay = items.filter(
( { name } ) => name !== 'core/block'
);
itemsToDisplay = searchItems( itemsToDisplay, filterValue );
const clipboard = getClipboard();
let clipboardBlock = rawHandler( { HTML: clipboard } )[ 0 ];
const canAddClipboardBlock = canInsertBlockType(
clipboardBlock?.name,
destinationRootClientId
);
if ( ! canAddClipboardBlock ) {
return itemsToDisplay;
}
const { icon, name } = getBlockType( clipboardBlock.name );
const { attributes: initialAttributes, innerBlocks } = clipboardBlock;
clipboardBlock = {
id: 'clipboard',
name,
icon,
initialAttributes,
innerBlocks,
};
return [ clipboardBlock, ...itemsToDisplay ];
}
return (
<BottomSheet
isVisible={ true }
onClose={ onClose }
hideHeader
hasNavigation
setMinHeightToMaxHeight={ showSearchForm }
>
<BottomSheetConsumer>
{ ( { listProps, safeAreaBottomInset } ) => (
<View>
{ showSearchForm && (
<InserterSearchForm
onChange={ ( value ) => {
setFilterValue( value );
} }
value={ filterValue }
onLayout={ ( event ) => {
const { height } = event.nativeEvent.layout;
setSearchFormHeight( height );
} }
/>
) }
<InserterSearchResults
items={ getItems() }
onSelect={ ( item ) => {
onInsert( item );
onSelect( item );
} }
{ ...{
listProps,
safeAreaBottomInset,
searchFormHeight,
} }
/>
</View>
) }
</BottomSheetConsumer>
</BottomSheet>
);
}
export default InserterMenu;