diff --git a/src/main/webapp/app/shared/monaco-editor/model/actions/list.action.ts b/src/main/webapp/app/shared/monaco-editor/model/actions/list.action.ts index 5f404a25535e..4bef7abe2135 100644 --- a/src/main/webapp/app/shared/monaco-editor/model/actions/list.action.ts +++ b/src/main/webapp/app/shared/monaco-editor/model/actions/list.action.ts @@ -79,15 +79,12 @@ export abstract class ListAction extends TextEditorAction { const lines = selectedText.split('\n'); // Check if the cursor is at the end of the line to add or remove the prefix - let position = editor.getPosition(); + const position = editor.getPosition(); if (position) { const currentLineText = editor.getLineText(position.getLineNumber()); if (!selectedText && position.getColumn() <= currentLineText.length) { - const endPosition = new TextEditorPosition(position.getLineNumber(), currentLineText.length + 1); - editor.setPosition(endPosition); - editor.focus(); - position = endPosition; + return; } if (position.getColumn() === currentLineText.length + 1) { diff --git a/src/test/javascript/spec/component/shared/metis/postings-markdown-editor/postings-markdown-editor.component.spec.ts b/src/test/javascript/spec/component/shared/metis/postings-markdown-editor/postings-markdown-editor.component.spec.ts index eeae40d0f971..276d3e82fd7d 100644 --- a/src/test/javascript/spec/component/shared/metis/postings-markdown-editor/postings-markdown-editor.component.spec.ts +++ b/src/test/javascript/spec/component/shared/metis/postings-markdown-editor/postings-markdown-editor.component.spec.ts @@ -582,4 +582,26 @@ describe('PostingsMarkdownEditor', () => { (component as any).handleKeyDown(mockModel, mockPosition.lineNumber); expect(handleActionClickSpy).not.toHaveBeenCalled(); }); + + it('should keep the cursor position intact when editing text in a list item', () => { + const bulletedListAction = component.defaultActions.find((action: any) => action instanceof BulletedListAction) as BulletedListAction; + mockEditor.getPosition.mockReturnValue({ + getLineNumber: () => 1, + getColumn: () => 5, + } as TextEditorPosition); + mockEditor.getLineText.mockReturnValue('- First line'); + mockEditor.getTextAtRange.mockReturnValue(''); + + const replaceTextSpy = jest.spyOn(mockEditor, 'replaceTextAtRange'); + bulletedListAction.run(mockEditor); + + expect(replaceTextSpy).not.toHaveBeenCalled(); + + const cursorPosition = mockEditor.getPosition(); + expect(cursorPosition).toEqual({ + getLineNumber: expect.any(Function), + getColumn: expect.any(Function), + }); + expect(cursorPosition?.getColumn()).toBe(5); + }); });