@@ -648,6 +648,14 @@ function undoUnorderedListStyle(text: string): UndoResult {
648648 }
649649}
650650
651+ const prefix = ( index : number , unorderedList : boolean ) : string => {
652+ if ( unorderedList ) {
653+ return '- '
654+ } else {
655+ return `${ index + 1 } . `
656+ }
657+ }
658+
651659function listStyle ( textarea : HTMLTextAreaElement , style : StyleArgs ) : SelectionRange {
652660 const noInitialSelection = textarea . selectionStart === textarea . selectionEnd
653661 let selectionStart = textarea . selectionStart
@@ -656,55 +664,59 @@ function listStyle(textarea: HTMLTextAreaElement, style: StyleArgs): SelectionRa
656664 // Select whole line
657665 expandSelectionToLine ( textarea )
658666
659- const prefix = ( index : number ) : string => {
660- if ( style . unorderedList ) {
661- return '- '
662- } else if ( style . orderedList ) {
663- return `${ index + 1 } . `
664- }
665- return ''
666- }
667-
668667 let selectedText = textarea . value . slice ( textarea . selectionStart , textarea . selectionEnd )
669668
670669 // If the user intent was to do an undo, we will stop after this.
671670 // Otherwise, we will still undo to other list type to prevent list stacking
671+ let undoResultOpositeList : UndoResult
672672 let undoResult : UndoResult
673+
673674 if ( style . orderedList ) {
674675 undoResult = undoOrderedListStyle ( selectedText )
675- selectedText = undoUnorderedListStyle ( undoResult . text ) . text
676+ undoResultOpositeList = undoUnorderedListStyle ( undoResult . text )
677+ selectedText = undoResultOpositeList . text
676678 } else {
677679 undoResult = undoUnorderedListStyle ( selectedText )
678- selectedText = undoOrderedListStyle ( undoResult . text ) . text
680+ undoResultOpositeList = undoOrderedListStyle ( undoResult . text )
681+ selectedText = undoResultOpositeList . text
679682 }
680683
681684 const lines = selectedText . split ( '\n' ) . map ( ( value , index ) => {
682- return `${ prefix ( index ) } ${ value } `
685+ return `${ prefix ( index , style . unorderedList ) } ${ value } `
683686 } )
684687
685688 const totalPrefixLength = lines . reduce ( ( previousValue , currentValue , currentIndex ) => {
686- return previousValue + prefix ( currentIndex ) . length
689+ return previousValue + prefix ( currentIndex , style . unorderedList ) . length
690+ } , 0 )
691+
692+ const totalPrefixLengthOpositeList = lines . reduce ( ( previousValue , currentValue , currentIndex ) => {
693+ return previousValue + prefix ( currentIndex , ! style . unorderedList ) . length
687694 } , 0 )
688695
689696 if ( undoResult . processed ) {
690697 if ( noInitialSelection ) {
691- selectionStart = Math . max ( selectionStart - prefix ( 0 ) . length , 0 )
698+ selectionStart = Math . max ( selectionStart - prefix ( 0 , style . unorderedList ) . length , 0 )
692699 selectionEnd = selectionStart
693700 } else {
694- selectionStart = Math . max ( selectionStart - prefix ( 0 ) . length , 0 )
695- selectionEnd = selectionEnd - totalPrefixLength
701+ selectionStart = textarea . selectionStart
702+ selectionEnd = textarea . selectionEnd - prefix ( 0 , style . unorderedList ) . length
696703 }
697704 return { text : undoResult . text , selectionStart, selectionEnd}
698705 }
699706
700707 const { newlinesToAppend, newlinesToPrepend} = newlinesToSurroundSelectedText ( textarea )
701708
702709 if ( noInitialSelection ) {
703- selectionStart = Math . max ( selectionStart + prefix ( 0 ) . length + newlinesToAppend . length , 0 )
710+ selectionStart = Math . max ( selectionStart + prefix ( 0 , style . unorderedList ) . length + newlinesToAppend . length , 0 )
704711 selectionEnd = selectionStart
705712 } else {
706- selectionStart = Math . max ( textarea . selectionStart + newlinesToAppend . length , 0 )
707- selectionEnd = textarea . selectionEnd + newlinesToAppend . length + totalPrefixLength
713+ if ( undoResultOpositeList . processed ) {
714+ selectionStart = Math . max ( textarea . selectionStart + newlinesToAppend . length , 0 )
715+ selectionEnd = textarea . selectionEnd + newlinesToAppend . length + totalPrefixLength - totalPrefixLengthOpositeList
716+ } else {
717+ selectionStart = Math . max ( textarea . selectionStart + newlinesToAppend . length , 0 )
718+ selectionEnd = textarea . selectionEnd + newlinesToAppend . length + totalPrefixLength
719+ }
708720 }
709721
710722 const text = newlinesToAppend + lines . join ( '\n' ) + newlinesToPrepend
0 commit comments