-
Notifications
You must be signed in to change notification settings - Fork 4k
/
quick-inserter.js
140 lines (126 loc) · 3.55 KB
/
quick-inserter.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
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useState, useEffect } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { Button, SearchControl } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
/**
* Internal dependencies
*/
import InserterSearchResults from './search-results';
import useInsertionPoint from './hooks/use-insertion-point';
import usePatternsState from './hooks/use-patterns-state';
import useBlockTypesState from './hooks/use-block-types-state';
import { store as blockEditorStore } from '../../store';
const SEARCH_THRESHOLD = 6;
const SHOWN_BLOCK_TYPES = 6;
const SHOWN_BLOCK_PATTERNS = 2;
const SHOWN_BLOCK_PATTERNS_WITH_PRIORITIZATION = 4;
export default function QuickInserter( {
onSelect,
rootClientId,
clientId,
isAppender,
prioritizePatterns,
} ) {
const [ filterValue, setFilterValue ] = useState( '' );
const [ destinationRootClientId, onInsertBlocks ] = useInsertionPoint( {
onSelect,
rootClientId,
clientId,
isAppender,
} );
const [ blockTypes ] = useBlockTypesState(
destinationRootClientId,
onInsertBlocks
);
const [ patterns ] = usePatternsState(
onInsertBlocks,
destinationRootClientId
);
const { setInserterIsOpened, insertionIndex } = useSelect(
( select ) => {
const { getSettings, getBlockIndex, getBlockCount } = select(
blockEditorStore
);
const settings = getSettings();
const index = getBlockIndex( clientId );
const blockCount = getBlockCount();
return {
setInserterIsOpened: settings.__experimentalSetIsInserterOpened,
insertionIndex: index === -1 ? blockCount : index,
};
},
[ clientId ]
);
const showPatterns =
patterns.length && ( !! filterValue || prioritizePatterns );
const showSearch =
( showPatterns && patterns.length > SEARCH_THRESHOLD ) ||
blockTypes.length > SEARCH_THRESHOLD;
useEffect( () => {
if ( setInserterIsOpened ) {
setInserterIsOpened( false );
}
}, [ setInserterIsOpened ] );
// When clicking Browse All select the appropriate block so as
// the insertion point can work as expected.
const onBrowseAll = () => {
setInserterIsOpened( { rootClientId, insertionIndex, filterValue } );
};
let maxBlockPatterns = 0;
if ( showPatterns ) {
maxBlockPatterns = prioritizePatterns
? SHOWN_BLOCK_PATTERNS_WITH_PRIORITIZATION
: SHOWN_BLOCK_PATTERNS;
}
return (
<div
className={ classnames( 'block-editor-inserter__quick-inserter', {
'has-search': showSearch,
'has-expand': setInserterIsOpened,
} ) }
>
{ showSearch && (
<SearchControl
className="block-editor-inserter__search"
value={ filterValue }
onChange={ ( value ) => {
setFilterValue( value );
} }
label={ __( 'Search for blocks and patterns' ) }
placeholder={ __( 'Search' ) }
/>
) }
<div className="block-editor-inserter__quick-inserter-results">
<InserterSearchResults
filterValue={ filterValue }
onSelect={ onSelect }
rootClientId={ rootClientId }
clientId={ clientId }
isAppender={ isAppender }
maxBlockPatterns={ maxBlockPatterns }
maxBlockTypes={ SHOWN_BLOCK_TYPES }
isDraggable={ false }
prioritizePatterns={ prioritizePatterns }
/>
</div>
{ setInserterIsOpened && (
<Button
className="block-editor-inserter__quick-inserter-expand"
onClick={ onBrowseAll }
aria-label={ __(
'Browse all. This will open the main inserter panel in the editor toolbar.'
) }
>
{ __( 'Browse all' ) }
</Button>
) }
</div>
);
}