Skip to content

Commit 69a4889

Browse files
committed
Add support for ordered list + tests
1 parent 20dd216 commit 69a4889

File tree

2 files changed

+67
-12
lines changed

2 files changed

+67
-12
lines changed

src/index.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -656,38 +656,49 @@ function listStyle(textarea: HTMLTextAreaElement, style: StyleArgs): SelectionRa
656656
// Select whole line
657657
expandSelectionToLine(textarea)
658658

659-
const prefix = '- '
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+
}
660667

661668
let selectedText = textarea.value.slice(textarea.selectionStart, textarea.selectionEnd)
662669

663670
const undoOrderedListResult = undoOrderedListStyle(selectedText)
664671
const undoUnorderedListResult = undoUnorderedListStyle(undoOrderedListResult.text)
665672

666673
if (undoOrderedListResult.processed || undoUnorderedListResult.processed) {
667-
if (noInitialSelection) {
668-
selectionStart = Math.max(selectionStart - prefix.length, 0)
669-
selectionEnd = selectionStart
670-
} else {
671-
selectionStart = Math.max(selectionStart - prefix.length, 0)
672-
selectionEnd = selectionEnd + prefix.length // * lines.length
673-
}
674+
// if (noInitialSelection) {
675+
// selectionStart = Math.max(selectionStart - prefix.length, 0)
676+
// selectionEnd = selectionStart
677+
// } else {
678+
// selectionStart = Math.max(selectionStart - prefix.length, 0)
679+
// selectionEnd = selectionEnd + prefix.length // * lines.length
680+
// }
674681
return {text: undoUnorderedListResult.text, selectionStart, selectionEnd}
675682
}
676683

677684
selectedText = undoUnorderedListResult.text
678685

679686
const lines = selectedText.split('\n').map((value, index) => {
680-
return `${prefix}${value}`
687+
return `${prefix(index)}${value}`
681688
})
682689

690+
const totalPrefixLength = lines.reduce((previousValue, currentValue, currentIndex) => {
691+
return previousValue + prefix(currentIndex).length
692+
}, 0)
693+
683694
const {newlinesToAppend, newlinesToPrepend} = newlinesToSurroundSelectedText(textarea)
684695

685696
if (noInitialSelection) {
686-
selectionStart = Math.max(selectionStart + prefix.length + newlinesToAppend.length, 0)
697+
selectionStart = Math.max(selectionStart + prefix(0).length + newlinesToAppend.length, 0)
687698
selectionEnd = selectionStart
688699
} else {
689-
selectionStart = Math.max(selectionStart + prefix.length + newlinesToAppend.length, 0)
690-
selectionEnd = selectionEnd + newlinesToAppend.length + prefix.length * lines.length
700+
selectionStart = Math.max(selectionStart + prefix(0).length + newlinesToAppend.length, 0)
701+
selectionEnd = selectionEnd + newlinesToAppend.length + totalPrefixLength
691702
}
692703

693704
const text = newlinesToAppend + lines.join('\n') + newlinesToPrepend

test/test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,50 @@ describe('markdown-toolbar-element', function () {
484484
})
485485
})
486486

487+
describe('ordered list', function () {
488+
it('turns line into list if cursor at end of line', function () {
489+
setVisualValue('One\nTwo|\nThree\n')
490+
clickToolbar('md-ordered-list')
491+
assert.equal('One\n\n1. Two|\n\nThree\n', visualValue())
492+
})
493+
494+
it('turns line into list if cursor at end of document', function () {
495+
setVisualValue('One\nTwo\nThree|')
496+
clickToolbar('md-ordered-list')
497+
assert.equal('One\nTwo\n\n1. Three|', visualValue())
498+
})
499+
500+
it('turns line into list if cursor at beginning of line', function () {
501+
setVisualValue('One\n|Two\nThree\n')
502+
clickToolbar('md-ordered-list')
503+
assert.equal('One\n\n1. |Two\n\nThree\n', visualValue())
504+
})
505+
506+
it('turns line into list if cursor at middle of line', function () {
507+
setVisualValue('One\nT|wo\nThree\n')
508+
clickToolbar('md-ordered-list')
509+
assert.equal('One\n\n1. T|wo\n\nThree\n', visualValue())
510+
})
511+
512+
it('turns line into list if partial line is selected', function () {
513+
setVisualValue('One\nT|w|o\nThree\n')
514+
clickToolbar('md-ordered-list')
515+
assert.equal('One\n\n1. T|w|o\n\nThree\n', visualValue())
516+
})
517+
518+
it('turns two lines into list if two lines are selected', function () {
519+
setVisualValue('|One\nTwo|\nThree\n')
520+
clickToolbar('md-ordered-list')
521+
assert.equal('1. |One\n2. Two|\n\nThree\n', visualValue())
522+
})
523+
524+
it('turns two lines into list if 2 lines are partially selected', function () {
525+
setVisualValue('O|ne\nTw|o\nThree\n')
526+
clickToolbar('md-ordered-list')
527+
assert.equal('1. O|ne\n2. Tw|o\n\nThree\n', visualValue())
528+
})
529+
})
530+
487531
describe('unordered list', function () {
488532
it('turns line into list if cursor at end of line', function () {
489533
setVisualValue('One\nTwo|\nThree\n')

0 commit comments

Comments
 (0)