From d3744b0e6ef7d06adbbb38f26cff593aafb1b4db Mon Sep 17 00:00:00 2001 From: Henry Heino <46334387+personalizedrefrigerator@users.noreply.github.com> Date: Sun, 22 Oct 2023 03:51:46 -0700 Subject: [PATCH] Mobile: Fixes #9066: Improve list toggle logic (#9103) --- .../markdownCommands.toggleList.test.ts | 29 +++++++++++++++++++ .../CodeMirror/markdown/markdownCommands.ts | 7 +++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/packages/editor/CodeMirror/markdown/markdownCommands.toggleList.test.ts b/packages/editor/CodeMirror/markdown/markdownCommands.toggleList.test.ts index ae26ed26c..c96bcfd59 100644 --- a/packages/editor/CodeMirror/markdown/markdownCommands.toggleList.test.ts +++ b/packages/editor/CodeMirror/markdown/markdownCommands.toggleList.test.ts @@ -63,6 +63,35 @@ describe('markdownCommands.toggleList', () => { ); }); + it('should not toggle a the full list when the cursor is on a blank line', async () => { + const checklistStartText = [ + '# Test', + '', + '- [ ] This', + '- [ ] is', + '', + ].join('\n'); + + const checklistEndText = [ + '- [ ] a', + '- [ ] test', + ].join('\n'); + + const editor = await createTestEditor( + `${checklistStartText}\n${checklistEndText}`, + + // Place the cursor on the blank line between the checklist + // regions + EditorSelection.cursor(unorderedListText.length + 1), + ['BulletList', 'ATXHeading1'], + ); + + // Should create a checkbox on the blank line + toggleList(ListType.CheckList)(editor); + expect(editor.state.doc.toString()).toBe( + `${checklistStartText}- [ ] \n${checklistEndText}`, + ); + }); // it('should correctly replace an unordered list with a checklist', async () => { // const editor = await createEditor( diff --git a/packages/editor/CodeMirror/markdown/markdownCommands.ts b/packages/editor/CodeMirror/markdown/markdownCommands.ts index c4c3217fd..89c09ca01 100644 --- a/packages/editor/CodeMirror/markdown/markdownCommands.ts +++ b/packages/editor/CodeMirror/markdown/markdownCommands.ts @@ -183,8 +183,11 @@ export const toggleList = (listType: ListType): Command => { const origFirstLineIndentation = firstLineIndentation; const origContainerType = containerType; - // Grow [sel] to the smallest containing list - if (sel.empty) { + // Grow `sel` to the smallest containing list, unless the + // cursor is on an empty line, in which case, the user + // probably wants to add a list item (and not select the entire + // list). + if (sel.empty && fromLine.text.trim() !== '') { sel = growSelectionToNode(state, sel, [orderedListTag, unorderedListTag]); computeSelectionProps(); }