Skip to content

Commit 20dd216

Browse files
committed
Adjust tests
1 parent 9545015 commit 20dd216

File tree

2 files changed

+46
-23
lines changed

2 files changed

+46
-23
lines changed

src/index.ts

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -614,44 +614,68 @@ function multilineStyle(textarea: HTMLTextAreaElement, arg: StyleArgs) {
614614
return {text, selectionStart, selectionEnd}
615615
}
616616

617-
function undoOrderedListStyle(textarea: HTMLTextAreaElement): string[] {
618-
const text = textarea.value.slice(textarea.selectionStart, textarea.selectionEnd)
617+
interface UndoResult {
618+
text: string
619+
processed: boolean
620+
}
621+
function undoOrderedListStyle(text: string): UndoResult {
619622
const lines = text.split('\n')
620623
const orderedListRegex = /^\d+\.\s+/
621-
const result = lines
622624
const shouldUndoOrderedList = lines.every(line => orderedListRegex.test(line))
625+
let result = lines
623626
if (shouldUndoOrderedList) {
624-
return lines.map(line => line.replace(orderedListRegex, ''))
627+
result = lines.map(line => line.replace(orderedListRegex, ''))
628+
}
629+
630+
return {
631+
text: result.join('\n'),
632+
processed: shouldUndoOrderedList
625633
}
626-
return result
627634
}
628635

629-
function undoUnorderedListStyle(textarea: HTMLTextAreaElement): string[] {
630-
const text = textarea.value.slice(textarea.selectionStart, textarea.selectionEnd)
636+
function undoUnorderedListStyle(text: string): UndoResult {
631637
const lines = text.split('\n')
632638
const unorderedListPrefix = '- '
633639
const shouldUndoUnorderedList = lines.every(line => line.startsWith(unorderedListPrefix))
634-
const result = lines
640+
let result = lines
635641
if (shouldUndoUnorderedList) {
636-
return lines.map(line => line.slice(unorderedListPrefix.length, line.length))
642+
result = lines.map(line => line.slice(unorderedListPrefix.length, line.length))
643+
}
644+
645+
return {
646+
text: result.join('\n'),
647+
processed: shouldUndoUnorderedList
637648
}
638-
return result
639649
}
640650

641651
function listStyle(textarea: HTMLTextAreaElement, style: StyleArgs): SelectionRange {
642652
const noInitialSelection = textarea.selectionStart === textarea.selectionEnd
643653
let selectionStart = textarea.selectionStart
644654
let selectionEnd = textarea.selectionEnd
645655

646-
undoOrderedListStyle(textarea)
647-
undoUnorderedListStyle(textarea)
656+
// Select whole line
657+
expandSelectionToLine(textarea)
648658

649659
const prefix = '- '
650660

651-
// Select whole line
652-
expandSelectionToLine(textarea)
661+
let selectedText = textarea.value.slice(textarea.selectionStart, textarea.selectionEnd)
662+
663+
const undoOrderedListResult = undoOrderedListStyle(selectedText)
664+
const undoUnorderedListResult = undoUnorderedListStyle(undoOrderedListResult.text)
665+
666+
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+
return {text: undoUnorderedListResult.text, selectionStart, selectionEnd}
675+
}
676+
677+
selectedText = undoUnorderedListResult.text
653678

654-
const selectedText = textarea.value.slice(textarea.selectionStart, textarea.selectionEnd)
655679
const lines = selectedText.split('\n').map((value, index) => {
656680
return `${prefix}${value}`
657681
})
@@ -667,7 +691,6 @@ function listStyle(textarea: HTMLTextAreaElement, style: StyleArgs): SelectionRa
667691
}
668692

669693
const text = newlinesToAppend + lines.join('\n') + newlinesToPrepend
670-
671694
return {text, selectionStart, selectionEnd}
672695
}
673696

test/test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -530,43 +530,43 @@ describe('markdown-toolbar-element', function () {
530530
it('undo list if cursor at end of line', function () {
531531
setVisualValue('One\n\n- Two|\n\nThree\n')
532532
clickToolbar('md-unordered-list')
533-
assert.equal('One\nTwo|\nThree\n', visualValue())
533+
assert.equal('One\n\nTwo|\n\nThree\n', visualValue())
534534
})
535535

536536
it('undo list if cursor at end of document', function () {
537537
setVisualValue('One\nTwo\n\n- Three|')
538538
clickToolbar('md-unordered-list')
539-
assert.equal('One\nTwo\nThree|', visualValue())
539+
assert.equal('One\nTwo\n\nThree|', visualValue())
540540
})
541541

542542
it('undo list if cursor at beginning of line', function () {
543543
setVisualValue('One\n\n- |Two\n\nThree\n')
544544
clickToolbar('md-unordered-list')
545-
assert.equal('One\n|Two\nThree\n', visualValue())
545+
assert.equal('One\n\n|Two\n\nThree\n', visualValue())
546546
})
547547

548548
it('undo list if cursor at middle of line', function () {
549549
setVisualValue('One\n\n- T|wo\n\nThree\n')
550550
clickToolbar('md-unordered-list')
551-
assert.equal('One\nT|wo\nThree\n', visualValue())
551+
assert.equal('One\n\nT|wo\n\nThree\n', visualValue())
552552
})
553553

554554
it('undo list if partial line is selected', function () {
555555
setVisualValue('One\n\n- T|w|o\n\nThree\n')
556556
clickToolbar('md-unordered-list')
557-
assert.equal('One\nT|w|o\nThree\n', visualValue())
557+
assert.equal('One\n\nT|w|o\n\nThree\n', visualValue())
558558
})
559559

560560
it('undo two lines list if two lines are selected', function () {
561561
setVisualValue('|- One\n- Two|\n\nThree\n')
562562
clickToolbar('md-unordered-list')
563-
assert.equal('|One\nTwo|\nThree\n', visualValue())
563+
assert.equal('|One\nTwo|\n\nThree\n', visualValue())
564564
})
565565

566566
it('undo two lines list if 2 lines are partially selected', function () {
567567
setVisualValue('- O|ne\n- Tw|o\n\nThree\n')
568568
clickToolbar('md-unordered-list')
569-
assert.equal('O|ne\nTw|o\nThree\n', visualValue())
569+
assert.equal('O|ne\nTw|o\n\nThree\n', visualValue())
570570
})
571571
})
572572

0 commit comments

Comments
 (0)