@@ -45,8 +45,10 @@ export default class RichTextEditor extends Component {
45
45
that . _onKeyboardWillHide = that . _onKeyboardWillHide . bind ( that ) ;
46
46
that . init = that . init . bind ( that ) ;
47
47
that . setRef = that . setRef . bind ( that ) ;
48
+ that . onViewLayout = that . onViewLayout . bind ( that ) ;
48
49
that . _keyOpen = false ;
49
- this . _focus = false ;
50
+ that . _focus = false ;
51
+ that . layout = { } ;
50
52
that . selectionChangeListeners = [ ] ;
51
53
const {
52
54
editorStyle : { backgroundColor, color, placeholderColor, cssText, contentCSSText} = { } ,
@@ -133,72 +135,74 @@ export default class RichTextEditor extends Component {
133
135
}*/
134
136
135
137
onMessage ( event ) {
138
+ const that = this ;
139
+ const {
140
+ onFocus, onBlur, onChange, onPaste,
141
+ onKeyUp, onKeyDown, onMessage,
142
+ onCursorPosition,
143
+ } = that . props ;
136
144
try {
137
- const props = this . props ;
138
145
const message = JSON . parse ( event . nativeEvent . data ) ;
139
146
const data = message . data ;
140
147
switch ( message . type ) {
141
148
case messages . CONTENT_HTML_RESPONSE :
142
- if ( this . contentResolve ) {
143
- this . contentResolve ( message . data ) ;
144
- this . contentResolve = undefined ;
145
- this . contentReject = undefined ;
146
- if ( this . pendingContentHtml ) {
147
- clearTimeout ( this . pendingContentHtml ) ;
148
- this . pendingContentHtml = undefined ;
149
+ if ( that . contentResolve ) {
150
+ that . contentResolve ( message . data ) ;
151
+ that . contentResolve = undefined ;
152
+ that . contentReject = undefined ;
153
+ if ( that . pendingContentHtml ) {
154
+ clearTimeout ( that . pendingContentHtml ) ;
155
+ that . pendingContentHtml = undefined ;
149
156
}
150
157
}
151
158
break ;
152
159
case messages . LOG :
153
160
console . log ( 'FROM EDIT:' , ...data ) ;
154
161
break ;
155
- case messages . SELECTION_CHANGE : {
162
+ case messages . SELECTION_CHANGE :
156
163
const items = message . data ;
157
- this . selectionChangeListeners . map ( ( listener ) => {
164
+ that . selectionChangeListeners . map ( ( listener ) => {
158
165
listener ( items ) ;
159
166
} ) ;
160
167
break ;
161
- }
162
- case messages . CONTENT_FOCUSED : {
163
- this . _focus = true ;
164
- this . focusListeners . map ( ( da ) => da ( ) ) ; // Subsequent versions will be deleted
165
- props . onFocus && props . onFocus ( ) ;
168
+ case messages . CONTENT_FOCUSED :
169
+ that . _focus = true ;
170
+ that . focusListeners . map ( ( da ) => da ( ) ) ; // Subsequent versions will be deleted
171
+ onFocus ?. ( ) ;
166
172
break ;
167
- }
168
- case messages . CONTENT_BLUR : {
169
- this . _focus = false ;
170
- props . onBlur && props . onBlur ( ) ;
173
+ case messages . CONTENT_BLUR :
174
+ that . _focus = false ;
175
+ onBlur ?. ( ) ;
171
176
break ;
172
- }
173
- case messages . CONTENT_CHANGE : {
174
- props . onChange && props . onChange ( data ) ;
177
+ case messages . CONTENT_CHANGE :
178
+ onChange ?. ( data ) ;
175
179
break ;
176
- }
177
- case messages . CONTENT_PASTED : {
178
- props . onPaste && props . onPaste ( data ) ;
180
+ case messages . CONTENT_PASTED :
181
+ onPaste ?. ( data ) ;
179
182
break ;
180
- }
181
- case messages . CONTENT_KEYUP : {
182
- props . onKeyUp && props . onKeyUp ( data ) ;
183
+ case messages . CONTENT_KEYUP :
184
+ onKeyUp ?. ( data ) ;
183
185
break ;
184
- }
185
- case messages . CONTENT_KEYDOWN : {
186
- props . onKeyDown && props . onKeyDown ( data ) ;
186
+ case messages . CONTENT_KEYDOWN :
187
+ onKeyDown ?. ( data ) ;
187
188
break ;
188
- }
189
189
case messages . OFFSET_HEIGHT :
190
- this . setWebHeight ( data ) ;
190
+ that . setWebHeight ( data ) ;
191
+ break ;
192
+ case messages . OFFSET_Y :
193
+ let offsetY = Number . parseInt ( Number . parseInt ( data ) + that . layout . y ) ;
194
+ offsetY > 0 && onCursorPosition ( offsetY ) ;
191
195
break ;
192
196
default :
193
- props . onMessage && props . onMessage ( message ) ;
197
+ onMessage ?. ( message ) ;
194
198
break ;
195
199
}
196
200
} catch ( e ) {
197
201
//alert('NON JSON MESSAGE');
198
202
}
199
203
}
200
204
201
- setWebHeight = ( height ) => {
205
+ setWebHeight ( height ) {
202
206
// console.log(height);
203
207
const { onHeightChange, useContainer} = this . props ;
204
208
if ( height !== this . state . height ) {
@@ -260,11 +264,16 @@ export default class RichTextEditor extends Component {
260
264
opacity = { opacity }
261
265
onLoad = { that . init }
262
266
/>
263
- { Platform . OS === 'android' && < TextInput ref = { ( ref ) => ( that . _input = ref ) } style = { styles . _input } /> }
267
+ { Platform . OS === 'android' && < TextInput ref = { ( ref ) => ( that . _input = ref ) } style = { styles . _input } /> }
264
268
</ >
265
269
) ;
266
270
}
267
271
272
+ onViewLayout ( { nativeEvent : { layout} } ) {
273
+ // const {x, y, width, height} = layout;
274
+ this . layout = layout ;
275
+ }
276
+
268
277
render ( ) {
269
278
let { height} = this . state ;
270
279
@@ -273,7 +282,7 @@ export default class RichTextEditor extends Component {
273
282
// If set to false, it will not use a View wrapper
274
283
const { useContainer, style, initialHeight = 0 } = this . props ;
275
284
return useContainer ? (
276
- < View style = { [ style , { height : height || initialHeight } ] } > { this . renderWebView ( ) } </ View >
285
+ < View style = { [ style , { height : height || initialHeight } ] } onLayout = { this . onViewLayout } > { this . renderWebView ( ) } </ View >
277
286
) : (
278
287
this . renderWebView ( )
279
288
) ;
@@ -367,11 +376,11 @@ export default class RichTextEditor extends Component {
367
376
this . sendAction ( actions . fontSize , 'result' , size ) ;
368
377
}
369
378
370
- setForeColor ( color ) {
379
+ setForeColor ( color ) {
371
380
this . sendAction ( actions . foreColor , 'result' , color ) ;
372
381
}
373
382
374
- setHiliteColor ( color ) {
383
+ setHiliteColor ( color ) {
375
384
this . sendAction ( actions . hiliteColor , 'result' , color ) ;
376
385
}
377
386
0 commit comments