@@ -236,6 +236,35 @@ const MarkdownTextInput = React.forwardRef<MarkdownTextInput, MarkdownTextInputP
236
236
[ parser , parseText , processedMarkdownStyle ] ,
237
237
) ;
238
238
239
+ const format = useCallback (
240
+ ( target : MarkdownTextInputElement , parsedText : string , cursorPosition : number , formatType : string ) : ParseTextResult => {
241
+ if ( ! contentSelection . current ) {
242
+ return {
243
+ text : '' ,
244
+ cursorPosition : 0 ,
245
+ } ;
246
+ }
247
+ let markdown ;
248
+ switch ( formatType ) {
249
+ case 'formatBold' :
250
+ markdown = '*' ;
251
+ break ;
252
+ case 'formatItalic' :
253
+ markdown = '_' ;
254
+ break ;
255
+ default :
256
+ markdown = '' ;
257
+ }
258
+
259
+ const beforeSelectedText = parsedText . slice ( 0 , contentSelection . current . start ) ;
260
+ const selectedText = parsedText . slice ( contentSelection . current . start , contentSelection . current . end ) ;
261
+ const afterSelectedText = parsedText . slice ( contentSelection . current . end ) ;
262
+ const text = `${ beforeSelectedText } ${ markdown } ${ selectedText } ${ markdown } ${ afterSelectedText } ` ;
263
+ return parseText ( parser , target , text , processedMarkdownStyle , cursorPosition + 2 , true ) ;
264
+ } ,
265
+ [ parser , parseText , processedMarkdownStyle ] ,
266
+ ) ;
267
+
239
268
// Placeholder text color logic
240
269
const updateTextColor = useCallback (
241
270
( node : HTMLDivElement , text : string ) => {
@@ -361,6 +390,10 @@ const MarkdownTextInput = React.forwardRef<MarkdownTextInput, MarkdownTextInputP
361
390
case 'historyRedo' :
362
391
newInputUpdate = redo ( divRef . current ) ;
363
392
break ;
393
+ case 'formatBold' :
394
+ case 'formatItalic' :
395
+ newInputUpdate = format ( divRef . current , parsedText , newCursorPosition , inputType ) ;
396
+ break ;
364
397
default :
365
398
newInputUpdate = parseText ( parser , divRef . current , parsedText , processedMarkdownStyle , newCursorPosition , true , ! inputType , inputType === 'pasteText' ) ;
366
399
}
@@ -414,7 +447,7 @@ const MarkdownTextInput = React.forwardRef<MarkdownTextInput, MarkdownTextInputP
414
447
415
448
handleContentSizeChange ( ) ;
416
449
} ,
417
- [ parser , updateTextColor , updateSelection , onChange , onChangeText , handleContentSizeChange , undo , redo , parseText , processedMarkdownStyle , setEventProps , maxLength ] ,
450
+ [ parser , updateTextColor , updateSelection , onChange , onChangeText , handleContentSizeChange , undo , redo , format , parseText , processedMarkdownStyle , setEventProps , maxLength ] ,
418
451
) ;
419
452
420
453
const insertText = useCallback (
0 commit comments