aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGiteabot <teabot@gitea.io>2024-12-25 13:10:14 +0800
committerGitHub <noreply@github.com>2024-12-25 13:10:14 +0800
commit0e0ebf68d78127bba19ce647bb1d99fcdc119715 (patch)
tree68671f5ca047081408f1c02e07b7ea376530064c
parent90bd08ceef6264eb4cb57201708044c9161bdbf3 (diff)
downloadgitea-0e0ebf68d78127bba19ce647bb1d99fcdc119715.tar.gz
gitea-0e0ebf68d78127bba19ce647bb1d99fcdc119715.zip
fix textarea newline handle (#32966) (#32977)
Backport #32966 by metiftikci Co-authored-by: metiftikci <metiftikci@hotmail.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
-rw-r--r--web_src/js/features/comp/EditorMarkdown.test.ts24
-rw-r--r--web_src/js/features/comp/EditorMarkdown.ts1
2 files changed, 20 insertions, 5 deletions
diff --git a/web_src/js/features/comp/EditorMarkdown.test.ts b/web_src/js/features/comp/EditorMarkdown.test.ts
index acd496bed6..7b4b44e83c 100644
--- a/web_src/js/features/comp/EditorMarkdown.test.ts
+++ b/web_src/js/features/comp/EditorMarkdown.test.ts
@@ -4,13 +4,24 @@ 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');
@@ -18,8 +29,11 @@ test('EditorMarkdown', () => {
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* [ ] ');
diff --git a/web_src/js/features/comp/EditorMarkdown.ts b/web_src/js/features/comp/EditorMarkdown.ts
index 2af003ccb0..5e2ef121f5 100644
--- a/web_src/js/features/comp/EditorMarkdown.ts
+++ b/web_src/js/features/comp/EditorMarkdown.ts
@@ -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;