-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathblock.js
392 lines (365 loc) · 10.7 KB
/
block.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { useState, createContext, useMemo } from '@wordpress/element';
import {
getBlockType,
getSaveElement,
isReusableBlock,
isUnmodifiedDefaultBlock,
getUnregisteredTypeHandlerName,
hasBlockSupport,
getBlockDefaultClassName,
} from '@wordpress/blocks';
import { withFilters } from '@wordpress/components';
import {
withDispatch,
withSelect,
useSelect,
useDispatch,
} from '@wordpress/data';
import { withViewportMatch } from '@wordpress/viewport';
import { compose, pure, ifCondition } from '@wordpress/compose';
/**
* Internal dependencies
*/
import BlockEdit from '../block-edit';
import BlockInvalidWarning from './block-invalid-warning';
import BlockCrashWarning from './block-crash-warning';
import BlockCrashBoundary from './block-crash-boundary';
import BlockHtml from './block-html';
import { Block } from './block-wrapper';
export const BlockListBlockContext = createContext();
function BlockListBlock( {
mode,
isFocusMode,
isLocked,
clientId,
rootClientId,
isSelected,
isMultiSelected,
isPartOfMultiSelection,
isFirstMultiSelected,
isLastMultiSelected,
isTypingWithinBlock,
isAncestorOfSelectedBlock,
isSelectionEnabled,
className,
name,
isValid,
attributes,
wrapperProps,
setAttributes,
onReplace,
onInsertBlocksAfter,
onMerge,
toggleSelection,
index,
enableAnimation,
} ) {
// In addition to withSelect, we should favor using useSelect in this
// component going forward to avoid leaking new props to the public API
// (editor.BlockListBlock filter)
const { isDragging, isHighlighted } = useSelect(
( select ) => {
const { isDraggingBlocks, isBlockHighlighted } = select(
'core/block-editor'
);
return {
isDragging: isDraggingBlocks(),
isHighlighted: isBlockHighlighted( clientId ),
};
},
[ clientId ]
);
const { removeBlock } = useDispatch( 'core/block-editor' );
const onRemove = () => removeBlock( clientId );
// Handling the error state
const [ hasError, setErrorState ] = useState( false );
const onBlockError = () => setErrorState( true );
const blockType = getBlockType( name );
const lightBlockWrapper = hasBlockSupport(
blockType,
'lightBlockWrapper',
false
);
const isUnregisteredBlock = name === getUnregisteredTypeHandlerName();
// Determine whether the block has props to apply to the wrapper.
if ( blockType.getEditWrapperProps ) {
wrapperProps = {
...wrapperProps,
...blockType.getEditWrapperProps( attributes ),
};
}
const generatedClassName =
lightBlockWrapper && hasBlockSupport( blockType, 'className', true )
? getBlockDefaultClassName( name )
: null;
const customClassName = lightBlockWrapper ? attributes.className : null;
const isAligned = wrapperProps && !! wrapperProps[ 'data-align' ];
// The wp-block className is important for editor styles.
// Generate the wrapper class names handling the different states of the
// block.
const wrapperClassName = classnames(
generatedClassName,
customClassName,
'block-editor-block-list__block',
{
'wp-block': ! isAligned,
'has-warning': ! isValid || !! hasError || isUnregisteredBlock,
'is-selected': isSelected,
'is-highlighted': isHighlighted,
'is-multi-selected': isMultiSelected,
'is-reusable': isReusableBlock( blockType ),
'is-dragging':
isDragging && ( isSelected || isPartOfMultiSelection ),
'is-typing': isTypingWithinBlock,
'is-focused':
isFocusMode && ( isSelected || isAncestorOfSelectedBlock ),
'is-focus-mode': isFocusMode,
'has-child-selected': isAncestorOfSelectedBlock,
},
className
);
// We wrap the BlockEdit component in a div that hides it when editing in
// HTML mode. This allows us to render all of the ancillary pieces
// (InspectorControls, etc.) which are inside `BlockEdit` but not
// `BlockHTML`, even in HTML mode.
let blockEdit = (
<BlockEdit
name={ name }
isSelected={ isSelected }
attributes={ attributes }
setAttributes={ setAttributes }
insertBlocksAfter={ isLocked ? undefined : onInsertBlocksAfter }
onReplace={ isLocked ? undefined : onReplace }
onRemove={ isLocked ? undefined : onRemove }
mergeBlocks={ isLocked ? undefined : onMerge }
clientId={ clientId }
isSelectionEnabled={ isSelectionEnabled }
toggleSelection={ toggleSelection }
/>
);
// For aligned blocks, provide a wrapper element so the block can be
// positioned relative to the block column.
if ( isAligned ) {
const alignmentWrapperProps = {
'data-align': wrapperProps[ 'data-align' ],
};
blockEdit = (
<div className="wp-block" { ...alignmentWrapperProps }>
{ blockEdit }
</div>
);
}
if ( mode !== 'visual' ) {
blockEdit = <div style={ { display: 'none' } }>{ blockEdit }</div>;
}
const value = {
clientId,
rootClientId,
isSelected,
isFirstMultiSelected,
isLastMultiSelected,
isPartOfMultiSelection,
enableAnimation,
index,
className: wrapperClassName,
isLocked,
name,
mode,
blockTitle: blockType.title,
wrapperProps,
};
const memoizedValue = useMemo( () => value, Object.values( value ) );
return (
<BlockListBlockContext.Provider value={ memoizedValue }>
<BlockCrashBoundary onError={ onBlockError }>
{ isValid && lightBlockWrapper && (
<>
{ blockEdit }
{ mode === 'html' && (
<Block.div __unstableIsHtml>
<BlockHtml clientId={ clientId } />
</Block.div>
) }
</>
) }
{ isValid && ! lightBlockWrapper && (
<Block.div { ...wrapperProps }>
{ blockEdit }
{ mode === 'html' && (
<BlockHtml clientId={ clientId } />
) }
</Block.div>
) }
{ ! isValid && (
<Block.div>
<BlockInvalidWarning clientId={ clientId } />
<div>{ getSaveElement( blockType, attributes ) }</div>
</Block.div>
) }
</BlockCrashBoundary>
{ !! hasError && (
<Block.div>
<BlockCrashWarning />
</Block.div>
) }
</BlockListBlockContext.Provider>
);
}
const applyWithSelect = withSelect(
( select, { clientId, rootClientId, isLargeViewport } ) => {
const {
isBlockSelected,
isAncestorMultiSelected,
isBlockMultiSelected,
isFirstMultiSelectedBlock,
getLastMultiSelectedBlockClientId,
isTyping,
getBlockMode,
isSelectionEnabled,
getSettings,
hasSelectedInnerBlock,
getTemplateLock,
__unstableGetBlockWithoutInnerBlocks,
getMultiSelectedBlockClientIds,
} = select( 'core/block-editor' );
const block = __unstableGetBlockWithoutInnerBlocks( clientId );
const isSelected = isBlockSelected( clientId );
const { focusMode, isRTL } = getSettings();
const templateLock = getTemplateLock( rootClientId );
const checkDeep = true;
// "ancestor" is the more appropriate label due to "deep" check
const isAncestorOfSelectedBlock = hasSelectedInnerBlock(
clientId,
checkDeep
);
// The fallback to `{}` is a temporary fix.
// This function should never be called when a block is not present in
// the state. It happens now because the order in withSelect rendering
// is not correct.
const { name, attributes, isValid } = block || {};
const isFirstMultiSelected = isFirstMultiSelectedBlock( clientId );
// Do not add new properties here, use `useSelect` instead to avoid
// leaking new props to the public API (editor.BlockListBlock filter).
return {
isMultiSelected: isBlockMultiSelected( clientId ),
isPartOfMultiSelection:
isBlockMultiSelected( clientId ) ||
isAncestorMultiSelected( clientId ),
isFirstMultiSelected,
isLastMultiSelected:
getLastMultiSelectedBlockClientId() === clientId,
multiSelectedClientIds: isFirstMultiSelected
? getMultiSelectedBlockClientIds()
: undefined,
// We only care about this prop when the block is selected
// Thus to avoid unnecessary rerenders we avoid updating the prop if
// the block is not selected.
isTypingWithinBlock:
( isSelected || isAncestorOfSelectedBlock ) && isTyping(),
mode: getBlockMode( clientId ),
isSelectionEnabled: isSelectionEnabled(),
isLocked: !! templateLock,
isFocusMode: focusMode && isLargeViewport,
isRTL,
// Users of the editor.BlockListBlock filter used to be able to
// access the block prop.
// Ideally these blocks would rely on the clientId prop only.
// This is kept for backward compatibility reasons.
block,
name,
attributes,
isValid,
isSelected,
isAncestorOfSelectedBlock,
};
}
);
const applyWithDispatch = withDispatch( ( dispatch, ownProps, { select } ) => {
const {
updateBlockAttributes,
insertBlocks,
mergeBlocks,
replaceBlocks,
toggleSelection,
__unstableMarkLastChangeAsPersistent,
} = dispatch( 'core/block-editor' );
// Do not add new properties here, use `useDispatch` instead to avoid
// leaking new props to the public API (editor.BlockListBlock filter).
return {
setAttributes( newAttributes ) {
const {
clientId,
isFirstMultiSelected,
multiSelectedClientIds,
} = ownProps;
const clientIds = isFirstMultiSelected
? multiSelectedClientIds
: [ clientId ];
updateBlockAttributes( clientIds, newAttributes );
},
onInsertBlocks( blocks, index ) {
const { rootClientId } = ownProps;
insertBlocks( blocks, index, rootClientId );
},
onInsertBlocksAfter( blocks ) {
const { clientId, rootClientId } = ownProps;
const { getBlockIndex } = select( 'core/block-editor' );
const index = getBlockIndex( clientId, rootClientId );
insertBlocks( blocks, index + 1, rootClientId );
},
onMerge( forward ) {
const { clientId } = ownProps;
const { getPreviousBlockClientId, getNextBlockClientId } = select(
'core/block-editor'
);
if ( forward ) {
const nextBlockClientId = getNextBlockClientId( clientId );
if ( nextBlockClientId ) {
mergeBlocks( clientId, nextBlockClientId );
}
} else {
const previousBlockClientId = getPreviousBlockClientId(
clientId
);
if ( previousBlockClientId ) {
mergeBlocks( previousBlockClientId, clientId );
}
}
},
onReplace( blocks, indexToSelect, initialPosition ) {
if (
blocks.length &&
! isUnmodifiedDefaultBlock( blocks[ blocks.length - 1 ] )
) {
__unstableMarkLastChangeAsPersistent();
}
replaceBlocks(
[ ownProps.clientId ],
blocks,
indexToSelect,
initialPosition
);
},
toggleSelection( selectionEnabled ) {
toggleSelection( selectionEnabled );
},
};
} );
export default compose(
pure,
withViewportMatch( { isLargeViewport: 'medium' } ),
applyWithSelect,
applyWithDispatch,
// block is sometimes not mounted at the right time, causing it be undefined
// see issue for more info
// https://github.com/WordPress/gutenberg/issues/17013
ifCondition( ( { block } ) => !! block ),
withFilters( 'editor.BlockListBlock' )
)( BlockListBlock );