-
Notifications
You must be signed in to change notification settings - Fork 71
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
Handle context menu format bold and italic #556
Changes from 8 commits
292f1d8
8f5716b
2dd64f6
8bc3c47
bce6f0a
ce50265
ad6ca17
6aeb62d
3d6a4ee
4cf9a1a
30d5e5d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -30,6 +30,7 @@ const useClientEffect = typeof window === 'undefined' ? useEffect : useLayoutEff | |||||
interface MarkdownTextInputProps extends TextInputProps, InlineImagesInputProps { | ||||||
markdownStyle?: MarkdownStyle; | ||||||
parser: (text: string) => MarkdownRange[]; | ||||||
formatSelection?: (selectedText: string, formatCommand: string) => string; | ||||||
onClick?: (e: MouseEvent<HTMLDivElement>) => void; | ||||||
dir?: string; | ||||||
disabled?: boolean; | ||||||
|
@@ -85,6 +86,7 @@ const MarkdownTextInput = React.forwardRef<MarkdownTextInput, MarkdownTextInputP | |||||
multiline = false, | ||||||
markdownStyle, | ||||||
parser, | ||||||
formatSelection, | ||||||
onBlur, | ||||||
onChange, | ||||||
onChangeText, | ||||||
|
@@ -236,6 +238,29 @@ const MarkdownTextInput = React.forwardRef<MarkdownTextInput, MarkdownTextInputP | |||||
[parser, parseText, processedMarkdownStyle], | ||||||
); | ||||||
|
||||||
const handleFormatSelection = useCallback( | ||||||
(target: MarkdownTextInputElement, parsedText: string, cursorPosition: number, formatCommand: string): ParseTextResult => { | ||||||
if (!contentSelection.current || contentSelection.current.end - contentSelection.current.start < 1) { | ||||||
throw new Error('[react-native-live-markdown] invalid selection'); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's rephrase the error message to be more specific.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||||||
} | ||||||
|
||||||
const selectedText = parsedText.slice(contentSelection.current.start, contentSelection.current.end); | ||||||
const formattedText = formatSelection?.(selectedText, formatCommand) ?? selectedText; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need the |
||||||
|
||||||
if (selectedText === formattedText) { | ||||||
return parseText(parser, target, parsedText, processedMarkdownStyle, cursorPosition); | ||||||
} | ||||||
tomekzaw marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
const prefix = parsedText.slice(0, contentSelection.current.start); | ||||||
const suffix = parsedText.slice(contentSelection.current.end); | ||||||
const diffLength = formattedText.length - selectedText.length; | ||||||
const text = `${prefix}${formattedText}${suffix}`; | ||||||
|
||||||
return parseText(parser, target, text, processedMarkdownStyle, cursorPosition + diffLength, true); | ||||||
}, | ||||||
[parser, parseText, formatSelection, processedMarkdownStyle], | ||||||
); | ||||||
|
||||||
// Placeholder text color logic | ||||||
const updateTextColor = useCallback( | ||||||
(node: HTMLDivElement, text: string) => { | ||||||
|
@@ -361,6 +386,11 @@ const MarkdownTextInput = React.forwardRef<MarkdownTextInput, MarkdownTextInputP | |||||
case 'historyRedo': | ||||||
newInputUpdate = redo(divRef.current); | ||||||
break; | ||||||
case 'formatBold': | ||||||
case 'formatItalic': | ||||||
case 'formatUnderline': | ||||||
newInputUpdate = handleFormatSelection(divRef.current, parsedText, newCursorPosition, inputType); | ||||||
break; | ||||||
default: | ||||||
newInputUpdate = parseText(parser, divRef.current, parsedText, processedMarkdownStyle, newCursorPosition, true, !inputType, inputType === 'pasteText'); | ||||||
} | ||||||
|
@@ -414,7 +444,21 @@ const MarkdownTextInput = React.forwardRef<MarkdownTextInput, MarkdownTextInputP | |||||
|
||||||
handleContentSizeChange(); | ||||||
}, | ||||||
[parser, updateTextColor, updateSelection, onChange, onChangeText, handleContentSizeChange, undo, redo, parseText, processedMarkdownStyle, setEventProps, maxLength], | ||||||
[ | ||||||
parser, | ||||||
updateTextColor, | ||||||
updateSelection, | ||||||
onChange, | ||||||
onChangeText, | ||||||
handleContentSizeChange, | ||||||
undo, | ||||||
redo, | ||||||
handleFormatSelection, | ||||||
parseText, | ||||||
processedMarkdownStyle, | ||||||
setEventProps, | ||||||
maxLength, | ||||||
], | ||||||
); | ||||||
|
||||||
const insertText = useCallback( | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a nit but this is a perfect place to use
switch/case
block.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done