-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
300 lines (265 loc) · 7.04 KB
/
index.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
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
/**
* External dependencies
*/
import { size } from 'lodash';
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { speak } from '@wordpress/a11y';
import { __, _x, sprintf } from '@wordpress/i18n';
import { Dropdown, Button } from '@wordpress/components';
import { Component } from '@wordpress/element';
import { withDispatch, withSelect } from '@wordpress/data';
import { compose, ifCondition } from '@wordpress/compose';
import { createBlock, store as blocksStore } from '@wordpress/blocks';
import { plus } from '@wordpress/icons';
/**
* Internal dependencies
*/
import InserterMenu from './menu';
import QuickInserter from './quick-inserter';
import { store as blockEditorStore } from '../../store';
const defaultRenderToggle = ( {
onToggle,
disabled,
isOpen,
blockTitle,
hasSingleBlockType,
toggleProps = {},
} ) => {
let label;
if ( hasSingleBlockType ) {
label = sprintf(
// translators: %s: the name of the block when there is only one
_x( 'Add %s', 'directly add the only allowed block' ),
blockTitle
);
} else {
label = _x( 'Add block', 'Generic label for block inserter button' );
}
const { onClick, ...rest } = toggleProps;
// Handle both onClick functions from the toggle and the parent component
function handleClick( event ) {
if ( onToggle ) {
onToggle( event );
}
if ( onClick ) {
onClick( event );
}
}
return (
<Button
icon={ plus }
label={ label }
tooltipPosition="bottom"
onClick={ handleClick }
className="block-editor-inserter__toggle"
aria-haspopup={ ! hasSingleBlockType ? 'true' : false }
aria-expanded={ ! hasSingleBlockType ? isOpen : false }
disabled={ disabled }
{ ...rest }
/>
);
};
class Inserter extends Component {
constructor() {
super( ...arguments );
this.onToggle = this.onToggle.bind( this );
this.renderToggle = this.renderToggle.bind( this );
this.renderContent = this.renderContent.bind( this );
}
onToggle( isOpen ) {
const { onToggle } = this.props;
// Surface toggle callback to parent component
if ( onToggle ) {
onToggle( isOpen );
}
}
/**
* Render callback to display Dropdown toggle element.
*
* @param {Object} options
* @param {Function} options.onToggle Callback to invoke when toggle is
* pressed.
* @param {boolean} options.isOpen Whether dropdown is currently open.
*
* @return {WPElement} Dropdown toggle element.
*/
renderToggle( { onToggle, isOpen } ) {
const {
disabled,
blockTitle,
hasSingleBlockType,
toggleProps,
hasItems,
renderToggle = defaultRenderToggle,
} = this.props;
return renderToggle( {
onToggle,
isOpen,
disabled: disabled || ! hasItems,
blockTitle,
hasSingleBlockType,
toggleProps,
} );
}
/**
* Render callback to display Dropdown content element.
*
* @param {Object} options
* @param {Function} options.onClose Callback to invoke when dropdown is
* closed.
*
* @return {WPElement} Dropdown content element.
*/
renderContent( { onClose } ) {
const {
rootClientId,
clientId,
isAppender,
showInserterHelpPanel,
// This prop is experimental to give some time for the quick inserter to mature
// Feel free to make them stable after a few releases.
__experimentalIsQuick: isQuick,
} = this.props;
if ( isQuick ) {
return (
<QuickInserter
onSelect={ () => {
onClose();
} }
rootClientId={ rootClientId }
clientId={ clientId }
isAppender={ isAppender }
/>
);
}
return (
<InserterMenu
onSelect={ () => {
onClose();
} }
rootClientId={ rootClientId }
clientId={ clientId }
isAppender={ isAppender }
showInserterHelpPanel={ showInserterHelpPanel }
/>
);
}
render() {
const {
position,
hasSingleBlockType,
insertOnlyAllowedBlock,
__experimentalIsQuick: isQuick,
onSelectOrClose,
} = this.props;
if ( hasSingleBlockType ) {
return this.renderToggle( { onToggle: insertOnlyAllowedBlock } );
}
return (
<Dropdown
className="block-editor-inserter"
contentClassName={ classnames(
'block-editor-inserter__popover',
{ 'is-quick': isQuick }
) }
position={ position }
onToggle={ this.onToggle }
expandOnMobile
headerTitle={ __( 'Add a block' ) }
renderToggle={ this.renderToggle }
renderContent={ this.renderContent }
onClose={ onSelectOrClose }
/>
);
}
}
export default compose( [
withSelect( ( select, { clientId, rootClientId } ) => {
const {
getBlockRootClientId,
hasInserterItems,
__experimentalGetAllowedBlocks,
} = select( blockEditorStore );
const { getBlockVariations } = select( blocksStore );
rootClientId =
rootClientId || getBlockRootClientId( clientId ) || undefined;
const allowedBlocks = __experimentalGetAllowedBlocks( rootClientId );
const hasSingleBlockType =
size( allowedBlocks ) === 1 &&
size(
getBlockVariations( allowedBlocks[ 0 ].name, 'inserter' )
) === 0;
let allowedBlockType = false;
if ( hasSingleBlockType ) {
allowedBlockType = allowedBlocks[ 0 ];
}
return {
hasItems: hasInserterItems( rootClientId ),
hasSingleBlockType,
blockTitle: allowedBlockType ? allowedBlockType.title : '',
allowedBlockType,
rootClientId,
};
} ),
withDispatch( ( dispatch, ownProps, { select } ) => {
return {
insertOnlyAllowedBlock() {
const {
rootClientId,
clientId,
isAppender,
hasSingleBlockType,
allowedBlockType,
onSelectOrClose,
} = ownProps;
if ( ! hasSingleBlockType ) {
return;
}
function getInsertionIndex() {
const {
getBlockIndex,
getBlockSelectionEnd,
getBlockOrder,
getBlockRootClientId,
} = select( blockEditorStore );
// If the clientId is defined, we insert at the position of the block.
if ( clientId ) {
return getBlockIndex( clientId, rootClientId );
}
// If there a selected block, we insert after the selected block.
const end = getBlockSelectionEnd();
if (
! isAppender &&
end &&
getBlockRootClientId( end ) === rootClientId
) {
return getBlockIndex( end, rootClientId ) + 1;
}
// Otherwise, we insert at the end of the current rootClientId
return getBlockOrder( rootClientId ).length;
}
const { insertBlock } = dispatch( blockEditorStore );
const blockToInsert = createBlock( allowedBlockType.name );
insertBlock( blockToInsert, getInsertionIndex(), rootClientId );
if ( onSelectOrClose ) {
onSelectOrClose();
}
const message = sprintf(
// translators: %s: the name of the block that has been added
__( '%s block added' ),
allowedBlockType.title
);
speak( message );
},
};
} ),
// The global inserter should always be visible, we are using ( ! isAppender && ! rootClientId && ! clientId ) as
// a way to detect the global Inserter.
ifCondition(
( { hasItems, isAppender, rootClientId, clientId } ) =>
hasItems || ( ! isAppender && ! rootClientId && ! clientId )
),
] )( Inserter );