@@ -65,11 +65,6 @@ interface MarkdownNativeEvent extends Event {
65
65
inputType : string ;
66
66
}
67
67
68
- type Selection = {
69
- start : number ;
70
- end : number ;
71
- } ;
72
-
73
68
type Dimensions = {
74
69
width : number ;
75
70
height : number ;
@@ -179,7 +174,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
179
174
const pasteRef = useRef < boolean > ( false ) ;
180
175
const divRef = useRef < HTMLDivElement | null > ( null ) ;
181
176
const currentlyFocusedField = useRef < HTMLDivElement | null > ( null ) ;
182
- const contentSelection = useRef < Selection | null > ( null ) ;
177
+ const contentSelection = useRef < ParseUtils . Selection | null > ( null ) ;
183
178
const className = `react-native-live-markdown-input-${ multiline ? 'multiline' : 'singleline' } ` ;
184
179
const history = useRef < InputHistory > ( ) ;
185
180
const dimensions = React . useRef < Dimensions | null > ( null ) ;
@@ -303,15 +298,15 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
303
298
[ onSelectionChange , setEventProps ] ,
304
299
) ;
305
300
306
- const updateRefSelectionVariables = useCallback ( ( newSelection : Selection ) => {
301
+ const updateRefSelectionVariables = useCallback ( ( newSelection : ParseUtils . Selection ) => {
307
302
const { start, end} = newSelection ;
308
303
const markdownHTMLInput = divRef . current as HTMLInputElement ;
309
304
markdownHTMLInput . selectionStart = start ;
310
305
markdownHTMLInput . selectionEnd = end ;
311
306
} , [ ] ) ;
312
307
313
308
const updateSelection = useCallback (
314
- ( e : SyntheticEvent < HTMLDivElement > | null = null , predefinedSelection : Selection | null = null ) => {
309
+ ( e : SyntheticEvent < HTMLDivElement > | null = null , predefinedSelection : ParseUtils . Selection | null = null ) => {
315
310
if ( ! divRef . current ) {
316
311
return ;
317
312
}
@@ -400,26 +395,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
400
395
} > ;
401
396
setEventProps ( event ) ;
402
397
403
- // The new text is between the prev start selection and the new end selection, can be empty
404
- const addedText = normalizedText . slice ( prevSelection . start , cursorPosition ?? 0 ) ;
405
- // The length of the text that replaced the before text
406
- const count = addedText . length ;
407
- // The start index of the replacement operation
408
- let start = prevSelection . start ;
409
-
410
- const prevSelectionRange = prevSelection . end - prevSelection . start ;
411
- // The length the deleted text had before
412
- let before = prevSelectionRange ;
413
- if ( prevSelectionRange === 0 && ( inputType === 'deleteContentBackward' || inputType === 'deleteContentForward' ) ) {
414
- // its possible the user pressed a delete key without a selection range, so we need to adjust the before value to have the length of the deleted text
415
- before = prevTextLength - normalizedText . length ;
416
- }
417
-
418
- if ( inputType === 'deleteContentBackward' ) {
419
- // When the user does a backspace delete he expects the content before the cursor to be removed.
420
- // For this the start value needs to be adjusted (its as if the selection was before the text that we want to delete)
421
- start -= before ;
422
- }
398
+ const { start, before, count} = ParseUtils . calculateInputMetrics ( inputType , prevSelection , prevTextLength , normalizedText , cursorPosition ) ;
423
399
424
400
event . nativeEvent . count = count ;
425
401
event . nativeEvent . before = before ;
@@ -660,7 +636,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
660
636
return ;
661
637
}
662
638
663
- const newSelection : Selection = { start : selection . start , end : selection . end ?? selection . start } ;
639
+ const newSelection : ParseUtils . Selection = { start : selection . start , end : selection . end ?? selection . start } ;
664
640
contentSelection . current = newSelection ;
665
641
updateRefSelectionVariables ( newSelection ) ;
666
642
CursorUtils . setCursorPosition ( divRef . current , newSelection . start , newSelection . end ) ;
0 commit comments