From a7a5f47625f31698897a62c152b1e186c95b43c7 Mon Sep 17 00:00:00 2001 From: Jarvis Date: Tue, 13 Oct 2026 16:40:24 +0800 Subject: [PATCH] fix(frontend): preserve nonempty lists when pressing Enter (#4224) --- frontend/src/utils/markdownEditing.test.ts | 29 ++++++++++++++++++++++ frontend/src/utils/markdownEditing.ts | 3 ++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/frontend/src/utils/markdownEditing.test.ts b/frontend/src/utils/markdownEditing.test.ts index d6e8ca23..0edc7a1f 100644 --- a/frontend/src/utils/markdownEditing.test.ts +++ b/frontend/src/utils/markdownEditing.test.ts @@ -20,6 +20,35 @@ test('Enter on an empty item ends the list instead of stacking markers', () => { assert.deepEqual(patch, { value: '- first\n', start: 8, end: 8 }) }) +for (const { value, at, expected } of [ + { value: '- hello', at: 2, expected: '- \n- hello' }, + { value: '* hello', at: 2, expected: '* \n* hello' }, + { value: '1. hello', at: 3, expected: '1. \n2. hello' }, + { value: '- [x] hello', at: 6, expected: '- [x] \n- [ ] hello' }, + { value: ' - hello', at: 4, expected: ' - \n - hello' }, + { value: 'before\n- hello\nafter', at: 9, expected: 'before\n- \n- hello\nafter' }, +]) { + test(`Enter before existing list content preserves the marker: ${JSON.stringify(value)}`, () => { + const patch = continueListOnEnter(caret(value, at)) + assert.ok(patch) + assert.equal(patch.value, expected) + assert.equal(patch.start, expected.indexOf('hello')) + assert.equal(patch.end, patch.start) + }) +} + +test('Enter in the middle of list content splits the item', () => { + assert.deepEqual(continueListOnEnter(caret('- hello world', 7)), { + value: '- hello\n- world', start: 10, end: 10, + }) +}) + +test('an empty item with trailing whitespace still exits the list', () => { + assert.deepEqual(continueListOnEnter(caret('- first\n- ', 10)), { + value: '- first\n ', start: 8, end: 8, + }) +}) + test('Enter outside a list and over a selection is left to the browser', () => { assert.equal(continueListOnEnter(caret('plain text', 10)), null) assert.equal(continueListOnEnter({ value: '- first', start: 2, end: 7 }), null) diff --git a/frontend/src/utils/markdownEditing.ts b/frontend/src/utils/markdownEditing.ts index e959f12e..5a2ab661 100644 --- a/frontend/src/utils/markdownEditing.ts +++ b/frontend/src/utils/markdownEditing.ts @@ -47,7 +47,8 @@ export function continueListOnEnter({ value, start, end }: EditorState): EditorP if (!match) return null const [, indent, marker, checkbox, content] = match - if (!content.trim()) { + const remainingContent = value.slice(start, lineEndAt(value, start)) + if (!content.trim() && !remainingContent.trim()) { // Empty item: drop the marker and leave the caret on a clean line. const nextValue = value.slice(0, lineStart) + value.slice(start) return { value: nextValue, start: lineStart, end: lineStart }