Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Communication: Fix cursor jump issue during list text editing #10181

Merged
merged 2 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
Loading