Skip to content

Commit

Permalink
implement maxParsableTextLength for web
Browse files Browse the repository at this point in the history
  • Loading branch information
dominictb committed Sep 15, 2024
1 parent 5638b54 commit 505a6dd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/MarkdownTextInput.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const useClientEffect = typeof window === 'undefined' ? useEffect : useLayoutEff

interface MarkdownTextInputProps extends TextInputProps {
markdownStyle?: MarkdownStyle;
maxParsableTextLength?: number;
onClick?: (e: MouseEvent<HTMLDivElement>) => void;
dir?: string;
disabled?: boolean;
Expand Down Expand Up @@ -79,6 +80,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
numberOfLines,
multiline = false,
markdownStyle,
maxParsableTextLength = null,
onBlur,
onChange,
onChangeText,
Expand Down Expand Up @@ -149,7 +151,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
if (text === null) {
return {text: divRef.current.value, cursorPosition: null};
}
const parsedText = updateInputStructure(target, text, cursorPosition, multiline, customMarkdownStyles, false, shouldForceDOMUpdate);
const parsedText = updateInputStructure(target, text, cursorPosition, maxParsableTextLength, multiline, customMarkdownStyles, false, shouldForceDOMUpdate);
divRef.current.value = parsedText.text;

if (history.current && shouldAddToHistory) {
Expand All @@ -158,7 +160,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(

return parsedText;
},
[multiline],
[maxParsableTextLength, multiline],
);

const processedMarkdownStyle = useMemo(() => {
Expand Down
3 changes: 2 additions & 1 deletion src/web/utils/parserUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ function updateInputStructure(
target: MarkdownTextInputElement,
text: string,
cursorPositionIndex: number | null,
maxParsableTextLength: number | null,
isMultiline = true,
markdownStyle: PartialMarkdownStyle = {},
alwaysMoveCursorToTheEnd = false,
Expand All @@ -296,7 +297,7 @@ function updateInputStructure(
const selection = getCurrentCursorPosition(target);
cursorPosition = selection ? selection.start : null;
}
const markdownRanges = global.parseExpensiMarkToRanges(text);
const markdownRanges = maxParsableTextLength !== null && text.length <= maxParsableTextLength ? global.parseExpensiMarkToRanges(text) : [];
if (!text || targetElement.innerHTML === '<br>' || (targetElement && targetElement.innerHTML === '\n')) {
targetElement.innerHTML = '';
targetElement.innerText = '';
Expand Down

0 comments on commit 505a6dd

Please sign in to comment.