@expensify/react-native-live-markdown 0.1.52 → 0.1.54

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.
@@ -8,6 +8,7 @@ import type {
8
8
  TextInputProps,
9
9
  TextInputKeyPressEventData,
10
10
  TextInputFocusEventData,
11
+ TextInputContentSizeChangeEventData,
11
12
  } from 'react-native';
12
13
  import React, {useEffect, useRef, useCallback, useMemo, useLayoutEffect} from 'react';
13
14
  import type {CSSProperties, MutableRefObject, ReactEventHandler, FocusEventHandler, MouseEvent, KeyboardEvent, SyntheticEvent} from 'react';
@@ -68,6 +69,11 @@ type Selection = {
68
69
  end: number;
69
70
  };
70
71
 
72
+ type Dimensions = {
73
+ width: number;
74
+ height: number;
75
+ };
76
+
71
77
  let focusTimeout: NodeJS.Timeout | null = null;
72
78
 
73
79
  // Removes one '\n' from the end of the string that were added by contentEditable div
@@ -154,6 +160,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
154
160
  style = {},
155
161
  value,
156
162
  autoFocus = false,
163
+ onContentSizeChange,
157
164
  },
158
165
  ref,
159
166
  ) => {
@@ -164,6 +171,8 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
164
171
  const contentSelection = useRef<Selection | null>(null);
165
172
  const className = `react-native-live-markdown-input-${multiline ? 'multiline' : 'singleline'}`;
166
173
  const history = useRef<InputHistory>();
174
+ const dimensions = React.useRef<Dimensions | null>(null);
175
+
167
176
  if (!history.current) {
168
177
  history.current = new InputHistory(100);
169
178
  }
@@ -296,6 +305,26 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
296
305
  [handleSelectionChange, updateRefSelectionVariables],
297
306
  );
298
307
 
308
+ const handleContentSizeChange = useCallback(() => {
309
+ if (!divRef.current || !multiline || !onContentSizeChange) {
310
+ return;
311
+ }
312
+
313
+ const hostNode = (divRef.current.firstChild as HTMLElement) ?? divRef.current;
314
+ const newWidth = hostNode.offsetWidth;
315
+ const newHeight = hostNode.offsetHeight;
316
+
317
+ if (newHeight !== dimensions.current?.height || newWidth !== dimensions.current.width) {
318
+ dimensions.current = {height: newHeight, width: newWidth};
319
+
320
+ onContentSizeChange({
321
+ nativeEvent: {
322
+ contentSize: dimensions.current,
323
+ },
324
+ } as NativeSyntheticEvent<TextInputContentSizeChangeEventData>);
325
+ }
326
+ }, [multiline, onContentSizeChange]);
327
+
299
328
  const handleOnChangeText = useCallback(
300
329
  (e: SyntheticEvent<HTMLDivElement>) => {
301
330
  if (!divRef.current || !(e.target instanceof HTMLElement)) {
@@ -336,8 +365,10 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
336
365
  const normalizedText = normalizeValue(text);
337
366
  onChangeText(normalizedText);
338
367
  }
368
+
369
+ handleContentSizeChange();
339
370
  },
340
- [updateSelection, updateTextColor, onChange, onChangeText, undo, redo, parseText, processedMarkdownStyle, setEventProps],
371
+ [updateTextColor, handleContentSizeChange, onChange, onChangeText, undo, redo, parseText, processedMarkdownStyle, updateSelection, setEventProps],
341
372
  );
342
373
 
343
374
  const handleKeyPress = useCallback(
@@ -552,6 +583,8 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
552
583
  }
553
584
  const currentValue = value ?? '';
554
585
  history.current.add(currentValue, currentValue.length);
586
+
587
+ handleContentSizeChange();
555
588
  // eslint-disable-next-line react-hooks/exhaustive-deps
556
589
  }, []);
557
590