From 769a5aeca84331b1f1f5ad5f3b93934ec33d4fbf Mon Sep 17 00:00:00 2001 From: dvd233 <111864431+dvd233@users.noreply.github.com> Date: Fri, 9 Oct 2026 11:16:59 +0800 Subject: [PATCH] fix(frontend): preserve the caret when outdenting Markdown lists --- frontend/src/utils/markdownEditing.test.ts | 47 +++++++++++++++++++++- frontend/src/utils/markdownEditing.ts | 7 +++- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/frontend/src/utils/markdownEditing.test.ts b/frontend/src/utils/markdownEditing.test.ts index 222631be..2197bb47 100644 --- a/frontend/src/utils/markdownEditing.test.ts +++ b/frontend/src/utils/markdownEditing.test.ts @@ -30,7 +30,52 @@ test('Tab indents a list line and Shift+Tab takes it back out', () => { assert.deepEqual(indented, { value: ' - item', start: 8, end: 8 }) const outdented = indentOnTab(caret(' - item', 8), true) - assert.equal(outdented?.value, '- item') + assert.deepEqual(outdented, { value: '- item', start: 6, end: 6 }) +}) + +for (const { name, value, at, expectedValue, expectedAt } of [ + { name: 'inside the item', value: ' - item', at: 6, expectedValue: '- item', expectedAt: 4 }, + { name: 'at the line start', value: ' - item', at: 0, expectedValue: '- item', expectedAt: 0 }, + { name: 'inside removed indentation', value: ' - item', at: 1, expectedValue: '- item', expectedAt: 0 }, + { name: 'after removed indentation', value: ' - item', at: 2, expectedValue: '- item', expectedAt: 0 }, + { name: 'with one space', value: ' - item', at: 7, expectedValue: '- item', expectedAt: 6 }, + { name: 'with a tab', value: '\t- item', at: 7, expectedValue: '- item', expectedAt: 6 }, + { name: 'with deeper indentation', value: ' - item', at: 10, expectedValue: ' - item', expectedAt: 8 }, + { name: 'without indentation', value: '- item', at: 6, expectedValue: '- item', expectedAt: 6 }, + { name: 'after another line', value: 'before\n - item\nafter', at: 8, expectedValue: 'before\n- item\nafter', expectedAt: 7 }, +]) { + test(`Shift+Tab keeps a collapsed caret ${name}`, () => { + assert.deepEqual(indentOnTab(caret(value, at), true), { + value: expectedValue, + start: expectedAt, + end: expectedAt, + }) + }) +} + +test('typing after outdenting inserts at the caret instead of replacing the item', () => { + const patch = indentOnTab(caret(' - item', 8), true) + assert.ok(patch) + const typed = patch.value.slice(0, patch.start) + ' next' + patch.value.slice(patch.end) + assert.equal(typed, '- item next') +}) + +test('Shift+Tab keeps explicitly selected lines selected', () => { + assert.deepEqual(indentOnTab({ value: ' - item', start: 4, end: 6 }, true), { + value: '- item', start: 0, end: 6, + }) + assert.deepEqual(indentOnTab({ value: ' - first\n\t- second', start: 2, end: 19 }, true), { + value: '- first\n- second', start: 0, end: 16, + }) +}) + +test('Tab continues to preserve a caret or selection within a list item', () => { + assert.deepEqual(indentOnTab(caret('- item', 4), false), { + value: ' - item', start: 6, end: 6, + }) + assert.deepEqual(indentOnTab({ value: '- item', start: 2, end: 4 }, false), { + value: ' - item', start: 4, end: 6, + }) }) test('Tab indents every line of a multi-line selection but skips blank ones', () => { diff --git a/frontend/src/utils/markdownEditing.ts b/frontend/src/utils/markdownEditing.ts index 19a342fa..ca78824a 100644 --- a/frontend/src/utils/markdownEditing.ts +++ b/frontend/src/utils/markdownEditing.ts @@ -94,7 +94,12 @@ export function indentOnTab(state: EditorState, outdent: boolean): EditorPatch { if (!multiLine && !inList) return null if (outdent) { - return mapSelectedLines(state, (line) => line.replace(/^(?: {1,2}|\t)/, '')) + const outdented = mapSelectedLines(state, (line) => line.replace(/^(?: {1,2}|\t)/, '')) + if (start !== end) return outdented + // A caret must stay collapsed so the next keystroke does not replace the item. + const removedIndent = value.length - outdented.value.length + const caret = Math.max(lineStart, start - removedIndent) + return { ...outdented, start: caret, end: caret } } if (multiLine) { return mapSelectedLines(state, (line) => (line.trim() ? INDENT + line : line))