From 2ec797303cd28e0b3951528e71e655c3178e82dc Mon Sep 17 00:00:00 2001 From: Patrick Dinger <121539+paxos@users.noreply.github.com> Date: Mon, 22 Nov 2021 11:46:40 -1000 Subject: [PATCH 1/3] Undo opposite lists --- src/index.ts | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 87162e7..abd5f7f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -417,6 +417,30 @@ function insertText(textarea: HTMLTextAreaElement, {text, selectionStart, select } } +function undoOrderedListStyle(textarea: HTMLTextAreaElement): string[] { + const text = textarea.value.slice(textarea.selectionStart, textarea.selectionEnd) + const lines = text.split('\n') + const orderedListRegex = /^\d+\.\s+/ + const result = lines + const shouldUndoOrderedList = lines.every(line => orderedListRegex.test(line)) + if (shouldUndoOrderedList) { + return lines.map(line => line.replace(orderedListRegex, '')) + } + return result +} + +function undoUnorderedListStyle(textarea: HTMLTextAreaElement): string[] { + const text = textarea.value.slice(textarea.selectionStart, textarea.selectionEnd) + const lines = text.split('\n') + const unorderedListPrefix = '- ' + const shouldUndoUnorderedList = lines.every(line => line.startsWith(unorderedListPrefix)) + const result = lines + if (shouldUndoUnorderedList) { + return lines.map(line => line.slice(unorderedListPrefix.length, line.length)) + } + return result +} + function styleSelectedText(textarea: HTMLTextAreaElement, styleArgs: StyleArgs) { const text = textarea.value.slice(textarea.selectionStart, textarea.selectionEnd) @@ -568,7 +592,7 @@ function multilineStyle(textarea: HTMLTextAreaElement, arg: StyleArgs) { let text = textarea.value.slice(textarea.selectionStart, textarea.selectionEnd) let selectionStart = textarea.selectionStart let selectionEnd = textarea.selectionEnd - const lines = text.split('\n') + const lines = undoOrderedListStyle(textarea) const undoStyle = lines.every(line => line.startsWith(prefix) && line.endsWith(suffix)) if (undoStyle) { @@ -602,6 +626,7 @@ function orderedList(textarea: HTMLTextAreaElement): SelectionRange { endOfLine = wordSelectionEnd(textarea.value, textarea.selectionStart, true) textToUnstyle = textarea.value.slice(startOfLine, endOfLine) } + lines = undoUnorderedListStyle(textarea) const linesToUnstyle = textToUnstyle.split('\n') const undoStyling = linesToUnstyle.every(line => orderedListRegex.test(line)) From 6e141a690fd4268645803a513769d12496d8c48f Mon Sep 17 00:00:00 2001 From: Patrick Dinger <121539+paxos@users.noreply.github.com> Date: Mon, 22 Nov 2021 12:08:05 -1000 Subject: [PATCH 2/3] Small refactoring --- src/index.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index abd5f7f..b15b41b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -618,7 +618,6 @@ function orderedList(textarea: HTMLTextAreaElement): SelectionRange { let selectionStart let text = textarea.value.slice(textarea.selectionStart, textarea.selectionEnd) let textToUnstyle = text - let lines = text.split('\n') let startOfLine, endOfLine if (noInitialSelection) { const linesBefore = textarea.value.slice(0, textarea.selectionStart).split(/\n/) @@ -626,7 +625,7 @@ function orderedList(textarea: HTMLTextAreaElement): SelectionRange { endOfLine = wordSelectionEnd(textarea.value, textarea.selectionStart, true) textToUnstyle = textarea.value.slice(startOfLine, endOfLine) } - lines = undoUnorderedListStyle(textarea) + let lines = undoUnorderedListStyle(textarea) const linesToUnstyle = textToUnstyle.split('\n') const undoStyling = linesToUnstyle.every(line => orderedListRegex.test(line)) From e815f7aee8d1ce4c451699345ef9384b07758f32 Mon Sep 17 00:00:00 2001 From: Patrick Dinger <121539+paxos@users.noreply.github.com> Date: Wed, 24 Nov 2021 12:39:21 -1000 Subject: [PATCH 3/3] Add more tests --- test/test.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/test.js b/test/test.js index ab8915e..b2d74e3 100644 --- a/test/test.js +++ b/test/test.js @@ -485,6 +485,20 @@ describe('markdown-toolbar-element', function () { }) describe('lists', function () { + it('does not stack list styles when selecting multiple lines', function () { + setVisualValue('One\n|Two\nThree|\n') + clickToolbar('md-ordered-list') + clickToolbar('md-unordered-list') + assert.equal('One\n\n|- Two\n- Three|\n', visualValue()) + }) + + it('does not stack list styles when selecting one line', function () { + setVisualValue('One\n|Two|\nThree|\n') + clickToolbar('md-ordered-list') + clickToolbar('md-unordered-list') + assert.equal('One\n\n|- Two\n- Three|\n', visualValue()) + }) + it('turns line into list when you click the unordered list icon with selection', function () { setVisualValue('One\n|Two|\nThree\n') clickToolbar('md-unordered-list')
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: