Skip to content

Commit 242cb0d

Browse files
paxosBestra
andcommitted
Update index.ts
Improve code readability Co-Authored-By: Chris Westra <[email protected]>
1 parent 7827c39 commit 242cb0d

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

src/index.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,22 @@ function makePrefix(index: number, unorderedList: boolean): string {
645645
}
646646
}
647647

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+
648664
function listStyle(textarea: HTMLTextAreaElement, style: StyleArgs): SelectionRange {
649665
const noInitialSelection = textarea.selectionStart === textarea.selectionEnd
650666
let selectionStart = textarea.selectionStart
@@ -653,32 +669,21 @@ function listStyle(textarea: HTMLTextAreaElement, style: StyleArgs): SelectionRa
653669
// Select whole line
654670
expandSelectionToLine(textarea)
655671

656-
let selectedText = textarea.value.slice(textarea.selectionStart, textarea.selectionEnd)
672+
const selectedText = textarea.value.slice(textarea.selectionStart, textarea.selectionEnd)
657673

658674
// If the user intent was to do an undo, we will stop after this.
659675
// 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)
672677

673-
const lines = selectedText.split('\n').map((value, index) => {
678+
const prefixedLines = pristineText.split('\n').map((value, index) => {
674679
return `${makePrefix(index, style.unorderedList)}${value}`
675680
})
676681

677-
const totalPrefixLength = lines.reduce((previousValue, currentValue, currentIndex) => {
682+
const totalPrefixLength = prefixedLines.reduce((previousValue, _currentValue, currentIndex) => {
678683
return previousValue + makePrefix(currentIndex, style.unorderedList).length
679684
}, 0)
680685

681-
const totalPrefixLengthOpositeList = lines.reduce((previousValue, currentValue, currentIndex) => {
686+
const totalPrefixLengthOpositeList = prefixedLines.reduce((previousValue, _currentValue, currentIndex) => {
682687
return previousValue + makePrefix(currentIndex, !style.unorderedList).length
683688
}, 0)
684689

@@ -690,10 +695,11 @@ function listStyle(textarea: HTMLTextAreaElement, style: StyleArgs): SelectionRa
690695
selectionStart = textarea.selectionStart
691696
selectionEnd = textarea.selectionEnd - totalPrefixLength
692697
}
693-
return {text: undoResult.text, selectionStart, selectionEnd}
698+
return {text: pristineText, selectionStart, selectionEnd}
694699
}
695700

696701
const {newlinesToAppend, newlinesToPrepend} = newlinesToSurroundSelectedText(textarea)
702+
const text = newlinesToAppend + prefixedLines.join('\n') + newlinesToPrepend
697703

698704
if (noInitialSelection) {
699705
selectionStart = Math.max(selectionStart + makePrefix(0, style.unorderedList).length + newlinesToAppend.length, 0)
@@ -708,7 +714,6 @@ function listStyle(textarea: HTMLTextAreaElement, style: StyleArgs): SelectionRa
708714
}
709715
}
710716

711-
const text = newlinesToAppend + lines.join('\n') + newlinesToPrepend
712717
return {text, selectionStart, selectionEnd}
713718
}
714719

0 commit comments

Comments
 (0)