/
menu.native.js
225 lines (202 loc) · 6.69 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
224
225
/**
* External dependencies
*/
import { FlatList, View, Text, TouchableHighlight } from 'react-native';
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import {
createBlock,
isUnmodifiedDefaultBlock,
} from '@wordpress/blocks';
import { withDispatch, withSelect } from '@wordpress/data';
import { withInstanceId, compose, withPreferredColorScheme } from '@wordpress/compose';
import { BottomSheet, Icon } from '@wordpress/components';
/**
* Internal dependencies
*/
import styles from './style.scss';
export class InserterMenu extends Component {
constructor() {
super( ...arguments );
this.onLayout = this.onLayout.bind( this );
this.state = {
numberOfColumns: this.calculateNumberOfColumns(),
};
}
componentDidMount() {
this.onOpen();
}
componentWillUnmount() {
this.onClose();
}
calculateNumberOfColumns() {
const bottomSheetWidth = BottomSheet.getWidth();
const { paddingLeft: itemPaddingLeft, paddingRight: itemPaddingRight } = styles.modalItem;
const { paddingLeft: containerPaddingLeft, paddingRight: containerPaddingRight } = styles.content;
const { width: itemWidth } = styles.modalIconWrapper;
const itemTotalWidth = itemWidth + itemPaddingLeft + itemPaddingRight;
const containerTotalWidth = bottomSheetWidth - ( containerPaddingLeft + containerPaddingRight );
return Math.floor( containerTotalWidth / itemTotalWidth );
}
onOpen() {
this.props.showInsertionPoint();
}
onClose() {
this.props.hideInsertionPoint();
}
onLayout() {
const numberOfColumns = this.calculateNumberOfColumns();
this.setState( { numberOfColumns } );
}
render() {
const { getStylesFromColorScheme } = this.props;
const bottomPadding = styles.contentBottomPadding;
const modalIconWrapperStyle = getStylesFromColorScheme( styles.modalIconWrapper, styles.modalIconWrapperDark );
const modalIconStyle = getStylesFromColorScheme( styles.modalIcon, styles.modalIconDark );
const modalItemLabelStyle = getStylesFromColorScheme( styles.modalItemLabel, styles.modalItemLabelDark );
return (
<BottomSheet
isVisible={ true }
onClose={ this.props.onDismiss }
contentStyle={ [ styles.content, bottomPadding ] }
hideHeader
>
<FlatList
onLayout={ this.onLayout }
scrollEnabled={ false }
key={ `InserterUI-${ this.state.numberOfColumns }` } //re-render when numberOfColumns changes
keyboardShouldPersistTaps="always"
numColumns={ this.state.numberOfColumns }
data={ this.props.items }
ItemSeparatorComponent={ () =>
<View style={ styles.rowSeparator } />
}
keyExtractor={ ( item ) => item.name }
renderItem={ ( { item } ) =>
<TouchableHighlight
style={ styles.touchableArea }
underlayColor="transparent"
activeOpacity={ .5 }
accessibilityLabel={ item.title }
onPress={ () => this.props.onSelect( item ) }>
<View style={ styles.modalItem }>
<View style={ modalIconWrapperStyle }>
<View style={ modalIconStyle }>
<Icon icon={ item.icon.src } fill={ modalIconStyle.fill } size={ modalIconStyle.width } />
</View>
</View>
<Text style={ modalItemLabelStyle }>{ item.title }</Text>
</View>
</TouchableHighlight>
}
/>
</BottomSheet>
);
}
}
export default compose(
withSelect( ( select, { clientId, isAppender, rootClientId } ) => {
const {
getInserterItems,
getBlockName,
getBlockRootClientId,
getBlockSelectionEnd,
} = select( 'core/block-editor' );
const {
getChildBlockNames,
} = select( 'core/blocks' );
let destinationRootClientId = rootClientId;
if ( ! destinationRootClientId && ! clientId && ! isAppender ) {
const end = getBlockSelectionEnd();
if ( end ) {
destinationRootClientId = getBlockRootClientId( end ) || undefined;
}
}
const destinationRootBlockName = getBlockName( destinationRootClientId );
return {
rootChildBlocks: getChildBlockNames( destinationRootBlockName ),
items: getInserterItems( destinationRootClientId ),
destinationRootClientId,
};
} ),
withDispatch( ( dispatch, ownProps, { select } ) => {
const {
showInsertionPoint,
hideInsertionPoint,
} = dispatch( 'core/block-editor' );
// To avoid duplication, getInsertionIndex is extracted and used in two event handlers
// This breaks the withDispatch not containing any logic rule.
// Since it's a function only called when the event handlers are called,
// it's fine to extract it.
// eslint-disable-next-line no-restricted-syntax
function getInsertionIndex() {
const {
getBlock,
getBlockIndex,
getBlockSelectionEnd,
getBlockOrder,
} = select( 'core/block-editor' );
const {
isPostTitleSelected,
} = select( 'core/editor' );
const { clientId, destinationRootClientId, isAppender } = ownProps;
// if post title is selected insert as first block
if ( isPostTitleSelected() ) {
return 0;
}
// If the clientId is defined, we insert at the position of the block.
if ( clientId ) {
return getBlockIndex( clientId, destinationRootClientId );
}
// If there a selected block,
const end = getBlockSelectionEnd();
// `end` argument (id) can refer to the component which is removed
// due to pressing `undo` button, that's why we need to check
// if `getBlock( end) is valid, otherwise `null` is passed
if ( ! isAppender && end && getBlock( end ) ) {
// and the last selected block is unmodified (empty), it will be replaced
if ( isUnmodifiedDefaultBlock( getBlock( end ) ) ) {
return getBlockIndex( end, destinationRootClientId );
}
// we insert after the selected block.
return getBlockIndex( end, destinationRootClientId ) + 1;
}
// Otherwise, we insert at the end of the current rootClientId
return getBlockOrder( destinationRootClientId ).length;
}
return {
showInsertionPoint() {
const index = getInsertionIndex();
showInsertionPoint( ownProps.destinationRootClientId, index );
},
hideInsertionPoint,
onSelect( item ) {
const {
replaceBlocks,
insertBlock,
} = dispatch( 'core/block-editor' );
const {
getSelectedBlock,
} = select( 'core/block-editor' );
const { isAppender } = ownProps;
const { name, initialAttributes } = item;
const selectedBlock = getSelectedBlock();
const insertedBlock = createBlock( name, initialAttributes );
if ( ! isAppender && selectedBlock && isUnmodifiedDefaultBlock( selectedBlock ) ) {
replaceBlocks( selectedBlock.clientId, insertedBlock );
} else {
insertBlock(
insertedBlock,
getInsertionIndex(),
ownProps.destinationRootClientId
);
}
ownProps.onSelect();
},
};
} ),
withInstanceId,
withPreferredColorScheme,
)( InserterMenu );