@@ -65,11 +65,6 @@ interface MarkdownNativeEvent extends Event {
6565 inputType : string ;
6666}
6767
68- type Selection = {
69- start : number ;
70- end : number ;
71- } ;
72-
7368type Dimensions = {
7469 width : number ;
7570 height : number ;
@@ -179,7 +174,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
179174 const pasteRef = useRef < boolean > ( false ) ;
180175 const divRef = useRef < HTMLDivElement | null > ( null ) ;
181176 const currentlyFocusedField = useRef < HTMLDivElement | null > ( null ) ;
182- const contentSelection = useRef < Selection | null > ( null ) ;
177+ const contentSelection = useRef < ParseUtils . Selection | null > ( null ) ;
183178 const className = `react-native-live-markdown-input-${ multiline ? 'multiline' : 'singleline' } ` ;
184179 const history = useRef < InputHistory > ( ) ;
185180 const dimensions = React . useRef < Dimensions | null > ( null ) ;
@@ -303,15 +298,15 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
303298 [ onSelectionChange , setEventProps ] ,
304299 ) ;
305300
306- const updateRefSelectionVariables = useCallback ( ( newSelection : Selection ) => {
301+ const updateRefSelectionVariables = useCallback ( ( newSelection : ParseUtils . Selection ) => {
307302 const { start, end} = newSelection ;
308303 const markdownHTMLInput = divRef . current as HTMLInputElement ;
309304 markdownHTMLInput . selectionStart = start ;
310305 markdownHTMLInput . selectionEnd = end ;
311306 } , [ ] ) ;
312307
313308 const updateSelection = useCallback (
314- ( e : SyntheticEvent < HTMLDivElement > | null = null , predefinedSelection : Selection | null = null ) => {
309+ ( e : SyntheticEvent < HTMLDivElement > | null = null , predefinedSelection : ParseUtils . Selection | null = null ) => {
315310 if ( ! divRef . current ) {
316311 return ;
317312 }
@@ -400,26 +395,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
400395 } > ;
401396 setEventProps ( event ) ;
402397
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 ) ;
423399
424400 event . nativeEvent . count = count ;
425401 event . nativeEvent . before = before ;
@@ -660,7 +636,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
660636 return ;
661637 }
662638
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 } ;
664640 contentSelection . current = newSelection ;
665641 updateRefSelectionVariables ( newSelection ) ;
666642 CursorUtils . setCursorPosition ( divRef . current , newSelection . start , newSelection . end ) ;
0 commit comments