Project

General

Profile

Defect #44336 ยป list_autofill_undo_scroll.patch

Jonathan Cormier, 2026-08-10 22:54

View differences:

app/javascript/controllers/list_autofill_controller.js
33 33
    switch (result.action) {
34 34
      case 'remove':
35 35
        event.preventDefault()
36
        this.input.setRangeText('', lineStartPos, selectionStart, 'start')
36
        // Use execCommand so the edit participates in the browser's undo
37
        // stack (setRangeText and value assignments are not undoable).
38
        this.input.setSelectionRange(lineStartPos, selectionStart)
39
        if (!document.execCommand('delete')) {
40
          this.input.setRangeText('', lineStartPos, selectionStart, 'start')
41
        }
37 42
        break
38
      case 'insert':
39
        event.preventDefault()
40
        const insertText = "\n" + result.text
41
        const newValue = value.slice(0, selectionStart) + insertText + value.slice(selectionStart)
42
        const newCursor = selectionStart + insertText.length
43
        this.input.value = newValue
44
        this.input.setSelectionRange(newCursor, newCursor)
43
      case 'insert': {
44
        // Let the native line break through (it creates an undo entry and
45
        // scrolls the caret into view), then append the list marker at the
46
        // caret. Replacing input.value here instead would discard the undo
47
        // history on every Enter and leave the caret off-screen in an
48
        // overflowing textarea.
49
        const text = result.text
50
        this.input.addEventListener('input', (e) => {
51
          if (e.inputType !== 'insertLineBreak') return
52
          if (!document.execCommand('insertText', false, text)) {
53
            const el = e.currentTarget
54
            const pos = el.selectionStart
55
            el.setRangeText(text, pos, pos, 'end')
56
          }
57
        }, { once: true })
45 58
        break
59
      }
46 60
      default:
47 61
        return
48 62
    }
    (1-1/1)