-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Copy pathblock.native.js
220 lines (196 loc) · 5.48 KB
/
block.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
/**
* External dependencies
*/
import {
View,
Text,
TouchableWithoutFeedback,
} from 'react-native';
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { withDispatch, withSelect } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { getBlockType } from '@wordpress/blocks';
import { __, sprintf } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import styles from './block.scss';
import BlockEdit from '../block-edit';
import BlockInvalidWarning from './block-invalid-warning';
import BlockMobileToolbar from './block-mobile-toolbar';
class BlockListBlock extends Component {
constructor() {
super( ...arguments );
this.insertBlocksAfter = this.insertBlocksAfter.bind( this );
this.onFocus = this.onFocus.bind( this );
this.state = {
isFullyBordered: false,
};
}
onFocus() {
if ( ! this.props.isSelected ) {
this.props.onSelect();
}
}
insertBlocksAfter( blocks ) {
this.props.onInsertBlocks( blocks, this.props.order + 1 );
if ( blocks[ 0 ] ) {
// focus on the first block inserted
this.props.onSelect( blocks[ 0 ].clientId );
}
}
getBlockForType() {
return (
<BlockEdit
name={ this.props.name }
isSelected={ this.props.isSelected }
attributes={ this.props.attributes }
setAttributes={ this.props.onChange }
onFocus={ this.onFocus }
onReplace={ this.props.onReplace }
insertBlocksAfter={ this.insertBlocksAfter }
mergeBlocks={ this.props.mergeBlocks }
onCaretVerticalPositionChange={ this.props.onCaretVerticalPositionChange }
clientId={ this.props.clientId }
/>
);
}
renderBlockTitle() {
return (
<View style={ styles.blockTitle }>
<Text>BlockType: { this.props.name }</Text>
</View>
);
}
getAccessibilityLabel() {
const { attributes, name, order, title, getAccessibilityLabelExtra } = this.props;
let blockName = '';
if ( name === 'core/missing' ) { // is the block unrecognized?
blockName = title;
} else {
blockName = sprintf(
/* translators: accessibility text. %s: block name. */
__( '%s Block' ),
title, //already localized
);
}
blockName += '. ' + sprintf( __( 'Row %d.' ), order + 1 );
if ( getAccessibilityLabelExtra ) {
const blockAccessibilityLabel = getAccessibilityLabelExtra( attributes );
blockName += blockAccessibilityLabel ? ' ' + blockAccessibilityLabel : '';
}
return blockName;
}
render() {
const {
borderStyle,
clientId,
focusedBorderColor,
icon,
isSelected,
isValid,
showTitle,
title,
} = this.props;
const borderColor = isSelected ? focusedBorderColor : 'transparent';
const accessibilityLabel = this.getAccessibilityLabel();
return (
<TouchableWithoutFeedback
onPress={ this.onFocus }
accessible={ ! isSelected }
accessibilityRole={ 'button' }
>
<View style={ [ styles.blockHolder, borderStyle, { borderColor } ] }>
{ showTitle && this.renderBlockTitle() }
<View
accessibilityLabel={ accessibilityLabel }
style={ [ ! isSelected && styles.blockContainer, isSelected && styles.blockContainerFocused ] }
>
{ isValid && this.getBlockForType() }
{ ! isValid &&
<BlockInvalidWarning blockTitle={ title } icon={ icon } />
}
</View>
{ isSelected && <BlockMobileToolbar clientId={ clientId } /> }
</View>
</TouchableWithoutFeedback>
);
}
}
export default compose( [
withSelect( ( select, { clientId, rootClientId } ) => {
const {
getBlockIndex,
getBlocks,
isBlockSelected,
__unstableGetBlockWithoutInnerBlocks,
} = select( 'core/block-editor' );
const order = getBlockIndex( clientId, rootClientId );
const isSelected = isBlockSelected( clientId );
const isFirstBlock = order === 0;
const isLastBlock = order === getBlocks().length - 1;
const block = __unstableGetBlockWithoutInnerBlocks( clientId );
const { name, attributes, isValid } = block || {};
const blockType = getBlockType( name || 'core/missing' );
const title = blockType.title;
const icon = blockType.icon;
const getAccessibilityLabelExtra = blockType.__experimentalGetAccessibilityLabel;
return {
icon,
name: name || 'core/missing',
order,
title,
attributes,
blockType,
isFirstBlock,
isLastBlock,
isSelected,
isValid,
getAccessibilityLabelExtra,
};
} ),
withDispatch( ( dispatch, ownProps, { select } ) => {
const {
insertBlocks,
mergeBlocks,
replaceBlocks,
selectBlock,
updateBlockAttributes,
} = dispatch( 'core/block-editor' );
return {
mergeBlocks( 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 );
}
}
},
onInsertBlocks( blocks, index ) {
insertBlocks( blocks, index, ownProps.rootClientId );
},
onSelect( clientId = ownProps.clientId, initialPosition ) {
selectBlock( clientId, initialPosition );
},
onChange: ( attributes ) => {
updateBlockAttributes( ownProps.clientId, attributes );
},
onReplace( blocks, indexToSelect ) {
replaceBlocks( [ ownProps.clientId ], blocks, indexToSelect );
},
};
} ),
] )( BlockListBlock );