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

feat: support maxLength prop in web #505

Merged
merged 13 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 12 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
10 changes: 5 additions & 5 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1497,7 +1497,7 @@ PODS:
- React-logger (= 0.75.2)
- React-perflogger (= 0.75.2)
- React-utils (= 0.75.2)
- RNLiveMarkdown (0.1.160):
- RNLiveMarkdown (0.1.162):
- DoubleConversion
- glog
- hermes-engine
Expand All @@ -1517,9 +1517,9 @@ PODS:
- ReactCodegen
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- RNLiveMarkdown/newarch (= 0.1.160)
- RNLiveMarkdown/newarch (= 0.1.162)
- Yoga
- RNLiveMarkdown/newarch (0.1.160):
- RNLiveMarkdown/newarch (0.1.162):
- DoubleConversion
- glog
- hermes-engine
Expand Down Expand Up @@ -1805,9 +1805,9 @@ SPEC CHECKSUMS:
React-utils: 81a715d9c0a2a49047e77a86f3a2247408540deb
ReactCodegen: 60973d382704c793c605b9be0fc7f31cb279442f
ReactCommon: 6ef348087d250257c44c0204461c03f036650e9b
RNLiveMarkdown: 201e1457f83d07376ae08c918c8ad14e3078d5a0
RNLiveMarkdown: 3071652e4e2b5a57cb647cfe5c76c392e58a98b3
SocketRocket: abac6f5de4d4d62d24e11868d7a2f427e0ef940d
Yoga: 2a45d7e59592db061217551fd3bbe2dd993817ae
Yoga: a1d7895431387402a674fd0d1c04ec85e87909b8

PODFILE CHECKSUM: 9b81b0f7bfba9e6fb4fa10efe8319f7860794e08

Expand Down
1 change: 1 addition & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default function App() {
onSelectionChange={e => setSelection(e.nativeEvent.selection)}
selection={selection}
id={TEST_CONST.INPUT_ID}
maxLength={30000}
/>
<Text style={styles.text}>{JSON.stringify(value)}</Text>
<Button
Expand Down
30 changes: 22 additions & 8 deletions src/MarkdownTextInput.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
id,
inputMode,
onTouchStart,
maxLength,
},
ref,
) => {
Expand Down Expand Up @@ -169,7 +170,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
const processedMarkdownStyle = useMemo(() => {
const newMarkdownStyle = processMarkdownStyle(markdownStyle);
if (divRef.current) {
parseText(divRef.current, divRef.current.value, newMarkdownStyle, null, false);
parseText(divRef.current, divRef.current.value, newMarkdownStyle, null, false, false);
}
return newMarkdownStyle;
}, [markdownStyle, parseText]);
Expand Down Expand Up @@ -305,14 +306,18 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(

updateTextColor(divRef.current, e.target.textContent ?? '');
const previousText = divRef.current.value;
const parsedText = normalizeValue(
let parsedText = normalizeValue(
inputType === 'pasteText' ? pasteContent.current || '' : parseInnerHTMLToText(e.target as MarkdownTextInputElement, inputType, contentSelection.current.start),
);

if (pasteContent.current) {
pasteContent.current = null;
}

if (maxLength !== undefined && parsedText.length > maxLength) {
parsedText = previousText;
}

const prevSelection = contentSelection.current ?? {start: 0, end: 0};
const newCursorPosition =
inputType === 'deleteContentForward' && contentSelection.current.start === contentSelection.current.end
Expand All @@ -331,7 +336,6 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
}
return;
}

let newInputUpdate: ParseTextResult;
switch (inputType) {
case 'historyUndo':
Expand Down Expand Up @@ -393,7 +397,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(

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

const insertText = useCallback(
Expand All @@ -403,17 +407,25 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
}

const previousText = divRef.current.value;
const newText = `${divRef.current.value.substring(0, contentSelection.current.start)}${text}${divRef.current.value.substring(contentSelection.current.end)}`;
let insertedText = text;
let availableLength = text.length;
const prefix = divRef.current.value.substring(0, contentSelection.current.start);
const suffix = divRef.current.value.substring(contentSelection.current.end);
if (maxLength !== undefined) {
availableLength = maxLength - prefix.length - suffix.length;
insertedText = text.slice(0, Math.max(availableLength, 0));
}
const newText = `${prefix}${insertedText}${suffix}`;
if (previousText === newText) {
document.execCommand('delete');
}

pasteContent.current = newText;
pasteContent.current = availableLength > 0 ? newText : previousText;
(e.nativeEvent as MarkdownNativeEvent).inputType = 'pasteText';

handleOnChangeText(e);
},
[handleOnChangeText],
[handleOnChangeText, maxLength],
);

const handleKeyPress = useCallback(
Expand Down Expand Up @@ -637,11 +649,13 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
return;
}
const normalizedValue = normalizeValue(value);

divRef.current.value = normalizedValue;
parseText(divRef.current, normalizedValue, processedMarkdownStyle, null, true, false, true);

updateTextColor(divRef.current, value);
},
[multiline, processedMarkdownStyle, value],
[multiline, processedMarkdownStyle, value, maxLength],
);

useClientEffect(
Expand Down
Loading