-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.native.js
483 lines (442 loc) · 12.6 KB
/
index.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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/**
* External dependencies
*/
import { identity } from 'lodash';
import { View, Platform, TouchableWithoutFeedback } from 'react-native';
/**
* WordPress dependencies
*/
import { Component, createContext } from '@wordpress/element';
import { withDispatch, withSelect } from '@wordpress/data';
import { compose, withPreferredColorScheme } from '@wordpress/compose';
import { createBlock } from '@wordpress/blocks';
import {
KeyboardAwareFlatList,
ReadableContentView,
WIDE_ALIGNMENTS,
alignmentHelpers,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import styles from './style.scss';
import BlockListAppender from '../block-list-appender';
import BlockListItem from './block-list-item.native';
import { store as blockEditorStore } from '../../store';
const BlockListContext = createContext();
export const OnCaretVerticalPositionChange = createContext();
const stylesMemo = {};
const getStyles = (
isRootList,
isStackedHorizontally,
horizontalAlignment
) => {
if ( isRootList ) {
return;
}
const styleName = `${ isStackedHorizontally }-${ horizontalAlignment }`;
if ( stylesMemo[ styleName ] ) {
return stylesMemo[ styleName ];
}
const computedStyles = [
isStackedHorizontally && styles.horizontal,
horizontalAlignment && styles[ `is-aligned-${ horizontalAlignment }` ],
];
stylesMemo[ styleName ] = computedStyles;
return computedStyles;
};
export class BlockList extends Component {
constructor() {
super( ...arguments );
this.extraData = {
parentWidth: this.props.parentWidth,
renderFooterAppender: this.props.renderFooterAppender,
renderAppender: this.props.renderAppender,
onDeleteBlock: this.props.onDeleteBlock,
contentStyle: this.props.contentStyle,
};
this.renderItem = this.renderItem.bind( this );
this.renderBlockListFooter = this.renderBlockListFooter.bind( this );
this.onCaretVerticalPositionChange = this.onCaretVerticalPositionChange.bind(
this
);
this.scrollViewInnerRef = this.scrollViewInnerRef.bind( this );
this.addBlockToEndOfPost = this.addBlockToEndOfPost.bind( this );
this.shouldFlatListPreventAutomaticScroll = this.shouldFlatListPreventAutomaticScroll.bind(
this
);
this.shouldShowInnerBlockAppender = this.shouldShowInnerBlockAppender.bind(
this
);
this.renderEmptyList = this.renderEmptyList.bind( this );
this.getExtraData = this.getExtraData.bind( this );
this.onLayout = this.onLayout.bind( this );
this.state = {
blockWidth: this.props.blockWidth || 0,
};
}
addBlockToEndOfPost( newBlock ) {
this.props.insertBlock( newBlock, this.props.blockCount );
}
onCaretVerticalPositionChange( targetId, caretY, previousCaretY ) {
KeyboardAwareFlatList.handleCaretVerticalPositionChange(
this.scrollViewRef,
targetId,
caretY,
previousCaretY
);
}
scrollViewInnerRef( ref ) {
this.scrollViewRef = ref;
}
shouldFlatListPreventAutomaticScroll() {
return this.props.isBlockInsertionPointVisible;
}
shouldShowInnerBlockAppender() {
const { blockClientIds, renderAppender } = this.props;
return renderAppender && blockClientIds.length > 0;
}
renderEmptyList() {
return (
<EmptyListComponentCompose
rootClientId={ this.props.rootClientId }
renderAppender={ this.props.renderAppender }
renderFooterAppender={ this.props.renderFooterAppender }
/>
);
}
getExtraData() {
const {
parentWidth,
renderFooterAppender,
onDeleteBlock,
contentStyle,
renderAppender,
} = this.props;
const { blockWidth } = this.state;
if (
this.extraData.parentWidth !== parentWidth ||
this.extraData.renderFooterAppender !== renderFooterAppender ||
this.extraData.onDeleteBlock !== onDeleteBlock ||
this.extraData.contentStyle !== contentStyle ||
this.extraData.renderAppender !== renderAppender ||
this.extraData.blockWidth !== blockWidth
) {
this.extraData = {
parentWidth,
renderFooterAppender,
onDeleteBlock,
contentStyle,
renderAppender,
blockWidth,
};
}
return this.extraData;
}
onLayout( { nativeEvent } ) {
const { layout } = nativeEvent;
const { blockWidth } = this.state;
const { isRootList, maxWidth } = this.props;
const layoutWidth = Math.floor( layout.width );
if ( isRootList && blockWidth !== layoutWidth ) {
this.setState( {
blockWidth: Math.min( layoutWidth, maxWidth ),
} );
} else if ( ! isRootList && ! blockWidth ) {
this.setState( { blockWidth: Math.min( layoutWidth, maxWidth ) } );
}
}
render() {
const { isRootList } = this.props;
// Use of Context to propagate the main scroll ref to its children e.g InnerBlocks
const blockList = isRootList ? (
<BlockListContext.Provider value={ this.scrollViewRef }>
{ this.renderList() }
</BlockListContext.Provider>
) : (
<BlockListContext.Consumer>
{ ( ref ) =>
this.renderList( {
parentScrollRef: ref,
} )
}
</BlockListContext.Consumer>
);
return (
<OnCaretVerticalPositionChange.Provider
value={ this.onCaretVerticalPositionChange }
>
{ blockList }
</OnCaretVerticalPositionChange.Provider>
);
}
renderList( extraProps = {} ) {
const {
clearSelectedBlock,
blockClientIds,
title,
header,
isReadOnly,
isRootList,
horizontal,
marginVertical = styles.defaultBlock.marginTop,
marginHorizontal = styles.defaultBlock.marginLeft,
isFloatingToolbarVisible,
isStackedHorizontally,
horizontalAlignment,
contentResizeMode,
blockWidth,
} = this.props;
const { parentScrollRef } = extraProps;
const {
blockToolbar,
blockBorder,
headerToolbar,
floatingToolbar,
} = styles;
const containerStyle = {
flex: isRootList ? 1 : 0,
// We set negative margin in the parent to remove the edge spacing between parent block and child block in ineer blocks
marginVertical: isRootList ? 0 : -marginVertical,
marginHorizontal: isRootList ? 0 : -marginHorizontal,
};
const isContentStretch = contentResizeMode === 'stretch';
const isMultiBlocks = blockClientIds.length > 1;
const { isWider } = alignmentHelpers;
return (
<View
style={ containerStyle }
onAccessibilityEscape={ clearSelectedBlock }
onLayout={ this.onLayout }
testID="block-list-wrapper"
>
<KeyboardAwareFlatList
{ ...( Platform.OS === 'android'
? { removeClippedSubviews: false }
: {} ) } // Disable clipping on Android to fix focus losing. See https://github.com/wordpress-mobile/gutenberg-mobile/pull/741#issuecomment-472746541
accessibilityLabel="block-list"
autoScroll={ this.props.autoScroll }
innerRef={ ( ref ) => {
this.scrollViewInnerRef( parentScrollRef || ref );
} }
extraScrollHeight={
blockToolbar.height + blockBorder.width
}
inputAccessoryViewHeight={
headerToolbar.height +
( isFloatingToolbarVisible
? floatingToolbar.height
: 0 )
}
keyboardShouldPersistTaps="always"
scrollViewStyle={ [
{ flex: isRootList ? 1 : 0 },
! isRootList && styles.overflowVisible,
] }
horizontal={ horizontal }
numColumns={ 1 }
extraData={ this.getExtraData() }
scrollEnabled={ isRootList }
contentContainerStyle={ [
horizontal && styles.horizontalContentContainer,
isWider( blockWidth, 'medium' ) &&
( isContentStretch && isMultiBlocks
? styles.horizontalContentContainerStretch
: styles.horizontalContentContainerCenter ),
] }
style={ getStyles(
isRootList,
isStackedHorizontally,
horizontalAlignment
) }
data={ blockClientIds }
keyExtractor={ identity }
renderItem={ this.renderItem }
shouldPreventAutomaticScroll={
this.shouldFlatListPreventAutomaticScroll
}
title={ title }
ListHeaderComponent={ header }
ListEmptyComponent={ ! isReadOnly && this.renderEmptyList }
ListFooterComponent={ this.renderBlockListFooter }
/>
{ this.shouldShowInnerBlockAppender() && (
<View
style={ {
marginHorizontal:
marginHorizontal -
styles.innerAppender.marginLeft,
} }
>
<BlockListAppender
rootClientId={ this.props.rootClientId }
renderAppender={ this.props.renderAppender }
showSeparator
/>
</View>
) }
</View>
);
}
renderItem( { item: clientId } ) {
const {
contentResizeMode,
contentStyle,
onAddBlock,
onDeleteBlock,
rootClientId,
isStackedHorizontally,
parentWidth,
marginVertical = styles.defaultBlock.marginTop,
marginHorizontal = styles.defaultBlock.marginLeft,
} = this.props;
const { blockWidth } = this.state;
return (
<BlockListItem
isStackedHorizontally={ isStackedHorizontally }
rootClientId={ rootClientId }
clientId={ clientId }
parentWidth={ parentWidth }
contentResizeMode={ contentResizeMode }
contentStyle={ contentStyle }
onAddBlock={ onAddBlock }
marginVertical={ marginVertical }
marginHorizontal={ marginHorizontal }
onDeleteBlock={ onDeleteBlock }
shouldShowInnerBlockAppender={
this.shouldShowInnerBlockAppender
}
blockWidth={ blockWidth }
/>
);
}
renderBlockListFooter() {
const paragraphBlock = createBlock( 'core/paragraph' );
const {
isReadOnly,
withFooter = true,
renderFooterAppender,
} = this.props;
if ( ! isReadOnly && withFooter ) {
return (
<>
<TouchableWithoutFeedback
accessibilityLabel={ __( 'Add paragraph block' ) }
testID={ __( 'Add paragraph block' ) }
onPress={ () => {
this.addBlockToEndOfPost( paragraphBlock );
} }
>
<View style={ styles.blockListFooter } />
</TouchableWithoutFeedback>
</>
);
} else if ( renderFooterAppender ) {
return renderFooterAppender();
}
return null;
}
}
export default compose( [
withSelect(
( select, { rootClientId, orientation, filterInnerBlocks } ) => {
const {
getBlockCount,
getBlockOrder,
getSelectedBlockClientId,
isBlockInsertionPointVisible,
getSettings,
} = select( blockEditorStore );
const isStackedHorizontally = orientation === 'horizontal';
const selectedBlockClientId = getSelectedBlockClientId();
let blockClientIds = getBlockOrder( rootClientId );
// Display only block which fulfill the condition in passed `filterInnerBlocks` function
if ( filterInnerBlocks ) {
blockClientIds = filterInnerBlocks( blockClientIds );
}
const { maxWidth } = getSettings();
const isReadOnly = getSettings().readOnly;
const blockCount = getBlockCount();
const hasRootInnerBlocks = !! blockCount;
const isFloatingToolbarVisible =
!! selectedBlockClientId && hasRootInnerBlocks;
return {
blockClientIds,
blockCount,
isBlockInsertionPointVisible:
Platform.OS === 'ios' && isBlockInsertionPointVisible(),
isReadOnly,
isRootList: rootClientId === undefined,
isFloatingToolbarVisible,
isStackedHorizontally,
maxWidth,
};
}
),
withDispatch( ( dispatch ) => {
const { insertBlock, replaceBlock, clearSelectedBlock } = dispatch(
blockEditorStore
);
return {
clearSelectedBlock,
insertBlock,
replaceBlock,
};
} ),
withPreferredColorScheme,
] )( BlockList );
class EmptyListComponent extends Component {
render() {
const {
shouldShowInsertionPoint,
rootClientId,
renderAppender,
renderFooterAppender,
} = this.props;
if ( renderFooterAppender ) {
return null;
}
return (
<View style={ styles.defaultAppender }>
<ReadableContentView
align={
renderAppender
? WIDE_ALIGNMENTS.alignments.full
: undefined
}
>
<BlockListAppender
rootClientId={ rootClientId }
renderAppender={ renderAppender }
showSeparator={ shouldShowInsertionPoint }
/>
</ReadableContentView>
</View>
);
}
}
const EmptyListComponentCompose = compose( [
withSelect( ( select, { rootClientId, orientation } ) => {
const {
getBlockOrder,
getBlockInsertionPoint,
isBlockInsertionPointVisible,
} = select( blockEditorStore );
const isStackedHorizontally = orientation === 'horizontal';
const blockClientIds = getBlockOrder( rootClientId );
const insertionPoint = getBlockInsertionPoint();
const blockInsertionPointIsVisible = isBlockInsertionPointVisible();
const shouldShowInsertionPoint =
! isStackedHorizontally &&
blockInsertionPointIsVisible &&
insertionPoint.rootClientId === rootClientId &&
// if list is empty, show the insertion point (via the default appender)
( blockClientIds.length === 0 ||
// or if the insertion point is right before the denoted block
! blockClientIds[ insertionPoint.index ] );
return {
shouldShowInsertionPoint,
};
} ),
] )( EmptyListComponent );