Skip to content

Commit

Permalink
Communication: Fix cursor jump issue during list text editing (#10181)
Browse files Browse the repository at this point in the history
  • Loading branch information
asliayk authored Jan 21, 2025
1 parent 9cd2d74 commit 4c90b41
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 4c90b41

Please sign in to comment.