diff --git a/services/web/frontend/js/features/source-editor/extensions/visual/visual-keymap.ts b/services/web/frontend/js/features/source-editor/extensions/visual/visual-keymap.ts index bb50131107..1731c38b52 100644 --- a/services/web/frontend/js/features/source-editor/extensions/visual/visual-keymap.ts +++ b/services/web/frontend/js/features/source-editor/extensions/visual/visual-keymap.ts @@ -63,11 +63,22 @@ export const visualKeymap = Prec.highest( const indent = indentString(state, columns) const insert = `\n${indent}\\item ` + const countWhitespaceAfterPosition = (pos: number) => { + const line = state.doc.lineAt(pos) + const followingText = state.sliceDoc(pos, line.to) + const matches = followingText.match(/^(\s+)/) + return matches ? matches[1].length : 0 + } + + // move the cursor past any whitespace on the new line + const pos = + from + insert.length + countWhitespaceAfterPosition(from) + handled = true return { changes: { from, insert }, - range: EditorSelection.cursor(from + insert.length), + range: EditorSelection.cursor(pos), } } diff --git a/services/web/test/frontend/features/source-editor/components/codemirror-editor-visual-list.spec.tsx b/services/web/test/frontend/features/source-editor/components/codemirror-editor-visual-list.spec.tsx index 0fe272c510..5ed248ec2f 100644 --- a/services/web/test/frontend/features/source-editor/components/codemirror-editor-visual-list.spec.tsx +++ b/services/web/test/frontend/features/source-editor/components/codemirror-editor-visual-list.spec.tsx @@ -231,4 +231,20 @@ describe(' lists in Rich Text mode', function () { cy.get('.cm-content').should('have.text', [' first', ' second'].join('')) }) + + it('positions the cursor after creating a new line with leading whitespace', function () { + const content = [ + '\\begin{itemize}', + '\\item foo bar', + '\\end{itemize}', + ].join('\n') + mountEditor(content) + + cy.get('.cm-line').eq(0).as('line') + cy.get('@line').click() + cy.get('@line').type('{leftArrow}'.repeat(4)) + cy.get('@line').type('{enter}baz') + + cy.get('.cm-content').should('have.text', [' foo', ' bazbar'].join('')) + }) })