diff --git a/src/MarkdownTextInput.web.tsx b/src/MarkdownTextInput.web.tsx index b6902801..23a3c462 100644 --- a/src/MarkdownTextInput.web.tsx +++ b/src/MarkdownTextInput.web.tsx @@ -236,6 +236,35 @@ const MarkdownTextInput = React.forwardRef { + if (!contentSelection.current) { + return { + text: '', + cursorPosition: 0, + }; + } + let markdown; + switch (formatType) { + case 'formatBold': + markdown = '*'; + break; + case 'formatItalic': + markdown = '_'; + break; + default: + markdown = ''; + } + + const beforeSelectedText = parsedText.slice(0, contentSelection.current.start); + const selectedText = parsedText.slice(contentSelection.current.start, contentSelection.current.end); + const afterSelectedText = parsedText.slice(contentSelection.current.end); + const text = `${beforeSelectedText}${markdown}${selectedText}${markdown}${afterSelectedText}`; + return parseText(parser, target, text, processedMarkdownStyle, cursorPosition + 2, true); + }, + [parser, parseText, processedMarkdownStyle], + ); + // Placeholder text color logic const updateTextColor = useCallback( (node: HTMLDivElement, text: string) => { @@ -361,6 +390,10 @@ const MarkdownTextInput = React.forwardRef