@expensify/react-native-live-markdown 0.1.6 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -167,6 +167,17 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
167
167
  // Empty placeholder would collapse the div, so we need to use zero-width space to prevent it
168
168
  const heightSafePlaceholder = useMemo(() => getPlaceholderValue(placeholder), [placeholder]);
169
169
 
170
+ const updateSelection = useCallback(() => {
171
+ if (!divRef.current) {
172
+ return;
173
+ }
174
+ const selection = CursorUtils.getCurrentCursorPosition(divRef.current);
175
+ const markdownHTMLInput = divRef.current as HTMLInputElement;
176
+ markdownHTMLInput.selectionStart = selection.start;
177
+ markdownHTMLInput.selectionEnd = selection.end;
178
+ contentSelection.current = selection;
179
+ }, []);
180
+
170
181
  const parseText = useCallback(
171
182
  (target: HTMLDivElement, text: string | null, customMarkdownStyles: MarkdownStyle, cursorPosition: number | null = null, shouldAddToHistory = true) => {
172
183
  if (text === null) {
@@ -177,12 +188,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
177
188
  history.current.debouncedAdd(parsedText.text, parsedText.cursorPosition);
178
189
  }
179
190
 
180
- if (parsedText.cursorPosition !== null) {
181
- contentSelection.current = {
182
- start: parsedText.cursorPosition,
183
- end: parsedText.cursorPosition,
184
- };
185
- }
191
+ updateSelection();
186
192
 
187
193
  return parsedText;
188
194
  },
@@ -354,10 +360,9 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
354
360
  (event) => {
355
361
  const e = event as unknown as NativeSyntheticEvent<TextInputSelectionChangeEventData>;
356
362
  setEventProps(e);
357
- const selection = CursorUtils.getCurrentCursorPosition(e.target as unknown as HTMLElement);
358
- contentSelection.current = selection;
359
- if (onSelectionChange) {
360
- e.nativeEvent.selection = selection;
363
+ updateSelection();
364
+ if (onSelectionChange && contentSelection.current) {
365
+ e.nativeEvent.selection = contentSelection.current;
361
366
  onSelectionChange(e);
362
367
  }
363
368
  },
@@ -414,6 +419,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
414
419
 
415
420
  const handleClick = useCallback(
416
421
  (e: MouseEvent<HTMLDivElement, globalThis.MouseEvent>) => {
422
+ updateSelection();
417
423
  if (!onClick || !divRef.current) {
418
424
  return;
419
425
  }
@@ -509,6 +515,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
509
515
  autoCapitalize={autoCapitalize}
510
516
  className={className}
511
517
  onKeyDown={handleKeyPress}
518
+ onKeyUp={updateSelection}
512
519
  onInput={handleOnChangeText}
513
520
  onSelect={handleSelectionChange}
514
521
  onClick={handleClick}