Skip to content

fix textarea newline handle (#32966) #32977

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

Merged
merged 1 commit into from
Dec 25, 2024
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
24 changes: 19 additions & 5 deletions web_src/js/features/comp/EditorMarkdown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,36 @@ test('EditorMarkdown', () => {
const textarea = document.createElement('textarea');
initTextareaMarkdown(textarea);

const testInput = (value, expected) => {
textarea.value = value;
textarea.setSelectionRange(value.length, value.length);
type ValueWithCursor = string | {
value: string;
pos: number;
}
const testInput = (input: ValueWithCursor, result: ValueWithCursor) => {
const intputValue = typeof input === 'string' ? input : input.value;
const inputPos = typeof input === 'string' ? intputValue.length : input.pos;
textarea.value = intputValue;
textarea.setSelectionRange(inputPos, inputPos);

const e = new KeyboardEvent('keydown', {key: 'Enter', cancelable: true});
textarea.dispatchEvent(e);
if (!e.defaultPrevented) textarea.value += '\n';
expect(textarea.value).toEqual(expected);
if (!e.defaultPrevented) textarea.value += '\n'; // simulate default behavior

const expectedValue = typeof result === 'string' ? result : result.value;
const expectedPos = typeof result === 'string' ? expectedValue.length : result.pos;
expect(textarea.value).toEqual(expectedValue);
expect(textarea.selectionStart).toEqual(expectedPos);
};

testInput('-', '-\n');
testInput('1.', '1.\n');

testInput('- ', '');
testInput('1. ', '');
testInput({value: '1. \n2. ', pos: 3}, {value: '\n2. ', pos: 0});

testInput('- x', '- x\n- ');
testInput('1. foo', '1. foo\n1. ');
testInput({value: '1. a\n2. b\n3. c', pos: 4}, {value: '1. a\n1. \n2. b\n3. c', pos: 8});
testInput('- [ ]', '- [ ]\n- ');
testInput('- [ ] foo', '- [ ] foo\n- [ ] ');
testInput('* [x] foo', '* [x] foo\n* [ ] ');
Expand Down
1 change: 1 addition & 0 deletions web_src/js/features/comp/EditorMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ function handleNewline(textarea: HTMLTextAreaElement, e: Event) {
if (!line) {
// clear current line if we only have i.e. '1. ' and the user presses enter again to finish creating a list
textarea.value = value.slice(0, lineStart) + value.slice(lineEnd);
textarea.setSelectionRange(selStart - prefix.length, selStart - prefix.length);
} else {
// start a new line with the same indention and prefix
let newPrefix = prefix;
Expand Down
Loading