mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-31 21:28:11 +09:00 
			
		
		
		
	fix textarea newline handle (#32966)
- Fix cursor position if input newline on middle of lines - ~Increment number if numbered list~  --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
		| @@ -4,13 +4,24 @@ test('EditorMarkdown', () => { | |||||||
|   const textarea = document.createElement('textarea'); |   const textarea = document.createElement('textarea'); | ||||||
|   initTextareaMarkdown(textarea); |   initTextareaMarkdown(textarea); | ||||||
|  |  | ||||||
|   const testInput = (value, expected) => { |   type ValueWithCursor = string | { | ||||||
|     textarea.value = value; |     value: string; | ||||||
|     textarea.setSelectionRange(value.length, value.length); |     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}); |     const e = new KeyboardEvent('keydown', {key: 'Enter', cancelable: true}); | ||||||
|     textarea.dispatchEvent(e); |     textarea.dispatchEvent(e); | ||||||
|     if (!e.defaultPrevented) textarea.value += '\n'; |     if (!e.defaultPrevented) textarea.value += '\n'; // simulate default behavior | ||||||
|     expect(textarea.value).toEqual(expected); |  | ||||||
|  |     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('-', '-\n'); | ||||||
| @@ -18,8 +29,11 @@ test('EditorMarkdown', () => { | |||||||
|  |  | ||||||
|   testInput('- ', ''); |   testInput('- ', ''); | ||||||
|   testInput('1. ', ''); |   testInput('1. ', ''); | ||||||
|  |   testInput({value: '1. \n2. ', pos: 3}, {value: '\n2. ', pos: 0}); | ||||||
|  |  | ||||||
|   testInput('- x', '- x\n- '); |   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('- [ ]', '- [ ]\n- '); | ||||||
|   testInput('- [ ] foo', '- [ ] foo\n- [ ] '); |   testInput('- [ ] foo', '- [ ] foo\n- [ ] '); | ||||||
|   testInput('* [x] foo', '* [x] foo\n* [ ] '); |   testInput('* [x] foo', '* [x] foo\n* [ ] '); | ||||||
|   | |||||||
| @@ -92,6 +92,7 @@ function handleNewline(textarea: HTMLTextAreaElement, e: Event) { | |||||||
|   if (!line) { |   if (!line) { | ||||||
|     // clear current line if we only have i.e. '1. ' and the user presses enter again to finish creating a list |     // 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.value = value.slice(0, lineStart) + value.slice(lineEnd); | ||||||
|  |     textarea.setSelectionRange(selStart - prefix.length, selStart - prefix.length); | ||||||
|   } else { |   } else { | ||||||
|     // start a new line with the same indention and prefix |     // start a new line with the same indention and prefix | ||||||
|     let newPrefix = prefix; |     let newPrefix = prefix; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user