@@ -645,6 +645,22 @@ function makePrefix(index: number, unorderedList: boolean): string {
645
645
}
646
646
}
647
647
648
+ function clearExistingListStyle ( style : StyleArgs , selectedText : string ) : [ UndoResult , UndoResult , string ] {
649
+ let undoResultOpositeList : UndoResult
650
+ let undoResult : UndoResult
651
+ let pristineText
652
+ if ( style . orderedList ) {
653
+ undoResult = undoOrderedListStyle ( selectedText )
654
+ undoResultOpositeList = undoUnorderedListStyle ( undoResult . text )
655
+ pristineText = undoResultOpositeList . text
656
+ } else {
657
+ undoResult = undoUnorderedListStyle ( selectedText )
658
+ undoResultOpositeList = undoOrderedListStyle ( undoResult . text )
659
+ pristineText = undoResultOpositeList . text
660
+ }
661
+ return [ undoResult , undoResultOpositeList , pristineText ]
662
+ }
663
+
648
664
function listStyle ( textarea : HTMLTextAreaElement , style : StyleArgs ) : SelectionRange {
649
665
const noInitialSelection = textarea . selectionStart === textarea . selectionEnd
650
666
let selectionStart = textarea . selectionStart
@@ -653,32 +669,21 @@ function listStyle(textarea: HTMLTextAreaElement, style: StyleArgs): SelectionRa
653
669
// Select whole line
654
670
expandSelectionToLine ( textarea )
655
671
656
- let selectedText = textarea . value . slice ( textarea . selectionStart , textarea . selectionEnd )
672
+ const selectedText = textarea . value . slice ( textarea . selectionStart , textarea . selectionEnd )
657
673
658
674
// If the user intent was to do an undo, we will stop after this.
659
675
// Otherwise, we will still undo to other list type to prevent list stacking
660
- let undoResultOpositeList : UndoResult
661
- let undoResult : UndoResult
662
-
663
- if ( style . orderedList ) {
664
- undoResult = undoOrderedListStyle ( selectedText )
665
- undoResultOpositeList = undoUnorderedListStyle ( undoResult . text )
666
- selectedText = undoResultOpositeList . text
667
- } else {
668
- undoResult = undoUnorderedListStyle ( selectedText )
669
- undoResultOpositeList = undoOrderedListStyle ( undoResult . text )
670
- selectedText = undoResultOpositeList . text
671
- }
676
+ const [ undoResult , undoResultOpositeList , pristineText ] = clearExistingListStyle ( style , selectedText )
672
677
673
- const lines = selectedText . split ( '\n' ) . map ( ( value , index ) => {
678
+ const prefixedLines = pristineText . split ( '\n' ) . map ( ( value , index ) => {
674
679
return `${ makePrefix ( index , style . unorderedList ) } ${ value } `
675
680
} )
676
681
677
- const totalPrefixLength = lines . reduce ( ( previousValue , currentValue , currentIndex ) => {
682
+ const totalPrefixLength = prefixedLines . reduce ( ( previousValue , _currentValue , currentIndex ) => {
678
683
return previousValue + makePrefix ( currentIndex , style . unorderedList ) . length
679
684
} , 0 )
680
685
681
- const totalPrefixLengthOpositeList = lines . reduce ( ( previousValue , currentValue , currentIndex ) => {
686
+ const totalPrefixLengthOpositeList = prefixedLines . reduce ( ( previousValue , _currentValue , currentIndex ) => {
682
687
return previousValue + makePrefix ( currentIndex , ! style . unorderedList ) . length
683
688
} , 0 )
684
689
@@ -690,10 +695,11 @@ function listStyle(textarea: HTMLTextAreaElement, style: StyleArgs): SelectionRa
690
695
selectionStart = textarea . selectionStart
691
696
selectionEnd = textarea . selectionEnd - totalPrefixLength
692
697
}
693
- return { text : undoResult . text , selectionStart, selectionEnd}
698
+ return { text : pristineText , selectionStart, selectionEnd}
694
699
}
695
700
696
701
const { newlinesToAppend, newlinesToPrepend} = newlinesToSurroundSelectedText ( textarea )
702
+ const text = newlinesToAppend + prefixedLines . join ( '\n' ) + newlinesToPrepend
697
703
698
704
if ( noInitialSelection ) {
699
705
selectionStart = Math . max ( selectionStart + makePrefix ( 0 , style . unorderedList ) . length + newlinesToAppend . length , 0 )
@@ -708,7 +714,6 @@ function listStyle(textarea: HTMLTextAreaElement, style: StyleArgs): SelectionRa
708
714
}
709
715
}
710
716
711
- const text = newlinesToAppend + lines . join ( '\n' ) + newlinesToPrepend
712
717
return { text, selectionStart, selectionEnd}
713
718
}
714
719
0 commit comments