fix(frontend): preserve nonempty lists when pressing Enter (#4224)

main
Jarvis 2026-10-13 16:40:24 +08:00 committed by GitHub
parent ec382d7911
commit a7a5f47625
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 1 deletions

View File

@ -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)

View File

@ -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 }