From c732cd4ed6630e1f9358df8a08574aa2be3286bf Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Wed, 4 Dec 2024 23:55:02 +0800 Subject: [PATCH] feat: handle context menu format bold and italic --- src/MarkdownTextInput.web.tsx | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) 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