Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add onContentSizeChange prop #290

Merged
merged 3 commits into from
Apr 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion src/MarkdownTextInput.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
TextInputProps,
TextInputKeyPressEventData,
TextInputFocusEventData,
TextInputContentSizeChangeEventData,
} from 'react-native';
import React, {useEffect, useRef, useCallback, useMemo, useLayoutEffect} from 'react';
import type {CSSProperties, MutableRefObject, ReactEventHandler, FocusEventHandler, MouseEvent, KeyboardEvent, SyntheticEvent} from 'react';
Expand Down Expand Up @@ -68,6 +69,11 @@ type Selection = {
end: number;
};

type Dimensions = {
width: number;
height: number;
};

let focusTimeout: NodeJS.Timeout | null = null;

// Removes one '\n' from the end of the string that were added by contentEditable div
Expand Down Expand Up @@ -154,6 +160,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
style = {},
value,
autoFocus = false,
onContentSizeChange,
},
ref,
) => {
Expand All @@ -164,6 +171,8 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
const contentSelection = useRef<Selection | null>(null);
const className = `react-native-live-markdown-input-${multiline ? 'multiline' : 'singleline'}`;
const history = useRef<InputHistory>();
const dimensions = React.useRef<Dimensions | null>(null);

if (!history.current) {
history.current = new InputHistory(100);
}
Expand Down Expand Up @@ -296,6 +305,26 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
[handleSelectionChange, updateRefSelectionVariables],
);

const handleContentSizeChange = useCallback(() => {
if (!divRef.current || !multiline || !onContentSizeChange) {
return;
}

const hostNode = (divRef.current.firstChild as HTMLElement) ?? divRef.current;
const newWidth = hostNode.offsetWidth;
const newHeight = hostNode.offsetHeight;

if (newHeight !== dimensions.current?.height || newWidth !== dimensions.current.width) {
dimensions.current = {height: newHeight, width: newWidth};

onContentSizeChange({
nativeEvent: {
contentSize: dimensions.current,
},
} as NativeSyntheticEvent<TextInputContentSizeChangeEventData>);
}
}, [multiline, onContentSizeChange]);

const handleOnChangeText = useCallback(
(e: SyntheticEvent<HTMLDivElement>) => {
if (!divRef.current || !(e.target instanceof HTMLElement)) {
Expand Down Expand Up @@ -336,8 +365,10 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
const normalizedText = normalizeValue(text);
onChangeText(normalizedText);
}

handleContentSizeChange();
},
[updateSelection, updateTextColor, onChange, onChangeText, undo, redo, parseText, processedMarkdownStyle, setEventProps],
[updateTextColor, handleContentSizeChange, onChange, onChangeText, undo, redo, parseText, processedMarkdownStyle, updateSelection, setEventProps],
);

const handleKeyPress = useCallback(
Expand Down Expand Up @@ -552,6 +583,8 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
}
const currentValue = value ?? '';
history.current.add(currentValue, currentValue.length);

handleContentSizeChange();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

Expand Down
Loading