@@ -648,6 +648,14 @@ function undoUnorderedListStyle(text: string): UndoResult {
648
648
}
649
649
}
650
650
651
+ const prefix = ( index : number , unorderedList : boolean ) : string => {
652
+ if ( unorderedList ) {
653
+ return '- '
654
+ } else {
655
+ return `${ index + 1 } . `
656
+ }
657
+ }
658
+
651
659
function listStyle ( textarea : HTMLTextAreaElement , style : StyleArgs ) : SelectionRange {
652
660
const noInitialSelection = textarea . selectionStart === textarea . selectionEnd
653
661
let selectionStart = textarea . selectionStart
@@ -656,55 +664,59 @@ function listStyle(textarea: HTMLTextAreaElement, style: StyleArgs): SelectionRa
656
664
// Select whole line
657
665
expandSelectionToLine ( textarea )
658
666
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
-
668
667
let selectedText = textarea . value . slice ( textarea . selectionStart , textarea . selectionEnd )
669
668
670
669
// If the user intent was to do an undo, we will stop after this.
671
670
// Otherwise, we will still undo to other list type to prevent list stacking
671
+ let undoResultOpositeList : UndoResult
672
672
let undoResult : UndoResult
673
+
673
674
if ( style . orderedList ) {
674
675
undoResult = undoOrderedListStyle ( selectedText )
675
- selectedText = undoUnorderedListStyle ( undoResult . text ) . text
676
+ undoResultOpositeList = undoUnorderedListStyle ( undoResult . text )
677
+ selectedText = undoResultOpositeList . text
676
678
} else {
677
679
undoResult = undoUnorderedListStyle ( selectedText )
678
- selectedText = undoOrderedListStyle ( undoResult . text ) . text
680
+ undoResultOpositeList = undoOrderedListStyle ( undoResult . text )
681
+ selectedText = undoResultOpositeList . text
679
682
}
680
683
681
684
const lines = selectedText . split ( '\n' ) . map ( ( value , index ) => {
682
- return `${ prefix ( index ) } ${ value } `
685
+ return `${ prefix ( index , style . unorderedList ) } ${ value } `
683
686
} )
684
687
685
688
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
687
694
} , 0 )
688
695
689
696
if ( undoResult . processed ) {
690
697
if ( noInitialSelection ) {
691
- selectionStart = Math . max ( selectionStart - prefix ( 0 ) . length , 0 )
698
+ selectionStart = Math . max ( selectionStart - prefix ( 0 , style . unorderedList ) . length , 0 )
692
699
selectionEnd = selectionStart
693
700
} 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
696
703
}
697
704
return { text : undoResult . text , selectionStart, selectionEnd}
698
705
}
699
706
700
707
const { newlinesToAppend, newlinesToPrepend} = newlinesToSurroundSelectedText ( textarea )
701
708
702
709
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 )
704
711
selectionEnd = selectionStart
705
712
} 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
+ }
708
720
}
709
721
710
722
const text = newlinesToAppend + lines . join ( '\n' ) + newlinesToPrepend
0 commit comments