-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathAztecView.js
254 lines (219 loc) · 7.3 KB
/
AztecView.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
/**
* External dependencies
*/
import {
findNodeHandle,
requireNativeComponent,
UIManager,
TouchableWithoutFeedback,
Platform,
} from 'react-native';
import TextInputState from 'react-native/Libraries/Components/TextInput/TextInputState';
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { ENTER, BACKSPACE } from '@wordpress/keycodes';
const AztecManager = UIManager.getViewManagerConfig( 'RCTAztecView' );
class AztecView extends Component {
constructor() {
super( ...arguments );
this._onContentSizeChange = this._onContentSizeChange.bind( this );
this._onEnter = this._onEnter.bind( this );
this._onBackspace = this._onBackspace.bind( this );
this._onKeyDown = this._onKeyDown.bind( this );
this._onChange = this._onChange.bind( this );
this._onHTMLContentWithCursor = this._onHTMLContentWithCursor.bind(
this
);
this._onFocus = this._onFocus.bind( this );
this._onBlur = this._onBlur.bind( this );
this._onSelectionChange = this._onSelectionChange.bind( this );
this._onPress = this._onPress.bind( this );
this._onAztecFocus = this._onAztecFocus.bind( this );
this.blur = this.blur.bind( this );
this.focus = this.focus.bind( this );
}
dispatch( command, params ) {
params = params || [];
UIManager.dispatchViewManagerCommand(
findNodeHandle( this ),
command,
params
);
}
requestHTMLWithCursor() {
this.dispatch( AztecManager.Commands.returnHTMLWithCursor );
}
_onContentSizeChange( event ) {
if ( ! this.props.onContentSizeChange ) {
return;
}
const size = event.nativeEvent.contentSize;
const { onContentSizeChange } = this.props;
onContentSizeChange( size );
}
_onEnter( event ) {
if ( ! this.isFocused() ) {
return;
}
if ( ! this.props.onKeyDown ) {
return;
}
const { onKeyDown } = this.props;
const newEvent = { ...event, keyCode: ENTER };
onKeyDown( newEvent );
}
_onBackspace( event ) {
if ( ! this.props.onKeyDown ) {
return;
}
const { onKeyDown } = this.props;
const newEvent = {
...event,
keyCode: BACKSPACE,
preventDefault: () => {},
};
onKeyDown( newEvent );
}
_onKeyDown( event ) {
if ( ! this.props.onKeyDown ) {
return;
}
const { onKeyDown } = this.props;
const newEvent = {
...event,
keyCode: event.nativeEvent.keyCode,
preventDefault: () => {},
};
onKeyDown( newEvent );
}
_onHTMLContentWithCursor( event ) {
if ( ! this.props.onHTMLContentWithCursor ) {
return;
}
const text = event.nativeEvent.text;
const selectionStart = event.nativeEvent.selectionStart;
const selectionEnd = event.nativeEvent.selectionEnd;
const { onHTMLContentWithCursor } = this.props;
onHTMLContentWithCursor( text, selectionStart, selectionEnd );
}
_onFocus( event ) {
if ( ! this.props.onFocus ) {
return;
}
const { onFocus } = this.props;
onFocus( event );
}
_onBlur( event ) {
this.selectionEndCaretY = null;
TextInputState.blurTextInput( findNodeHandle( this ) );
if ( ! this.props.onBlur ) {
return;
}
const { onBlur } = this.props;
onBlur( event );
}
_onChange( event ) {
// iOS uses the the onKeyDown prop directly from native only when one of the triggerKeyCodes is entered, but
// Android includes the information needed for onKeyDown in the event passed to onChange.
if ( Platform.OS === 'android' ) {
const triggersIncludeEventKeyCode =
this.props.triggerKeyCodes &&
this.props.triggerKeyCodes
.map( ( char ) => char.charCodeAt( 0 ) )
.includes( event.nativeEvent.keyCode );
if ( triggersIncludeEventKeyCode ) {
this._onKeyDown( event );
}
}
const { onChange } = this.props;
if ( onChange ) {
onChange( event );
}
}
_onSelectionChange( event ) {
if ( this.props.onSelectionChange ) {
const { selectionStart, selectionEnd, text } = event.nativeEvent;
const { onSelectionChange } = this.props;
onSelectionChange( selectionStart, selectionEnd, text, event );
}
if (
this.props.onCaretVerticalPositionChange &&
this.selectionEndCaretY !== event.nativeEvent.selectionEndCaretY
) {
const caretY = event.nativeEvent.selectionEndCaretY;
this.props.onCaretVerticalPositionChange(
event.target,
caretY,
this.selectionEndCaretY
);
this.selectionEndCaretY = caretY;
}
}
blur() {
TextInputState.blurTextInput( findNodeHandle( this ) );
}
focus() {
TextInputState.focusTextInput( findNodeHandle( this ) );
}
isFocused() {
const focusedField = TextInputState.currentlyFocusedField();
return focusedField && focusedField === findNodeHandle( this );
}
_onPress( event ) {
if ( ! this.isFocused() ) {
this.focus(); // Call to move the focus in RN way (TextInputState)
this._onFocus( event ); // Check if there are listeners set on the focus event
}
}
_onAztecFocus( event ) {
// IMPORTANT: the onFocus events from Aztec are thrown away on Android as these are handled by onPress() in the upper level.
// It's necessary to do this otherwise onFocus may be set by `{...otherProps}` and thus the onPress + onFocus
// combination generate an infinite loop as described in https://github.com/wordpress-mobile/gutenberg-mobile/issues/302
// For iOS, this is necessary to let the system know when Aztec was focused programatically.
if ( Platform.OS === 'ios' ) {
this._onPress( event );
}
}
render() {
// eslint-disable-next-line no-unused-vars
const { onActiveFormatsChange, ...otherProps } = this.props;
// `style` has to be destructured separately, without `otherProps`, because of:
// https://github.com/WordPress/gutenberg/issues/23611
const { style } = this.props;
if ( style.hasOwnProperty( 'lineHeight' ) ) {
delete style.lineHeight;
window.console.warn(
"Removing lineHeight style as it's not supported by native AztecView"
);
// IMPORTANT: Current Gutenberg implementation is supporting line-height without unit e.g. 'line-height':1.5
// and library which we are using to convert css to react-native requires unit to be included with dimension
// https://github.com/kristerkari/css-to-react-native-transform/blob/945866e84a505fdfb1a43b03ebe4bd32784a7f22/src/index.spec.js#L1234
// which means that we would need to patch the library if we want to support line-height from native AztecView in the future.
}
return (
<TouchableWithoutFeedback onPress={ this._onPress }>
<RCTAztecView
{ ...otherProps }
style={ style }
onContentSizeChange={ this._onContentSizeChange }
onHTMLContentWithCursor={ this._onHTMLContentWithCursor }
onChange={ this._onChange }
onSelectionChange={ this._onSelectionChange }
onEnter={ this.props.onKeyDown && this._onEnter }
onBackspace={ this.props.onKeyDown && this._onBackspace }
onKeyDown={ this.props.onKeyDown && this._onKeyDown }
deleteEnter={ this.props.deleteEnter }
// IMPORTANT: the onFocus events are thrown away as these are handled by onPress() in the upper level.
// It's necessary to do this otherwise onFocus may be set by `{...otherProps}` and thus the onPress + onFocus
// combination generate an infinite loop as described in https://github.com/wordpress-mobile/gutenberg-mobile/issues/302
onFocus={ this._onAztecFocus }
onBlur={ this._onBlur }
/>
</TouchableWithoutFeedback>
);
}
}
const RCTAztecView = requireNativeComponent( 'RCTAztecView', AztecView );
export default AztecView;