File size: 1,852 Bytes
4e23b01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const PASTE_BURST_MIN_CHARS = 8;
const PASTE_BURST_CHAR_INTERVAL_MS = 8;
const PASTE_BURST_ACTIVE_IDLE_TIMEOUT_MS = 30;
const PASTE_ENTER_SUPPRESS_WINDOW_MS = 120;

/**
 * Detects non-bracketed paste bursts: a rapid stream of plain characters
 * followed by Enter, where a bare Enter would otherwise submit the draft.
 *
 * This is a heuristic fallback for terminals that do not surface bracketed
 * paste markers. It intentionally does not buffer characters; the editor still
 * inserts typed text normally, and this class only decides whether an imminent
 * Enter should insert a newline instead of submitting.
 */
export class PasteBurst {
  private lastPlainCharAt?: number;
  private consecutivePlainChars = 0;
  private activeUntil = 0;
  private enterSuppressUntil = 0;

  onPlainChar(now: number): void {
    if (
      this.lastPlainCharAt !== undefined &&
      now - this.lastPlainCharAt <= PASTE_BURST_CHAR_INTERVAL_MS
    ) {
      this.consecutivePlainChars++;
    } else {
      this.consecutivePlainChars = 1;
    }

    this.lastPlainCharAt = now;

    if (this.consecutivePlainChars >= PASTE_BURST_MIN_CHARS) {
      this.extendWindow(now);
    }
  }

  shouldInsertNewlineInsteadOfSubmit(now: number): boolean {
    if (now <= this.activeUntil || now <= this.enterSuppressUntil) {
      return true;
    }

    return (
      this.lastPlainCharAt !== undefined &&
      this.consecutivePlainChars >= PASTE_BURST_MIN_CHARS &&
      now - this.lastPlainCharAt <= PASTE_BURST_CHAR_INTERVAL_MS
    );
  }

  extendWindow(now: number): void {
    this.activeUntil = now + PASTE_BURST_ACTIVE_IDLE_TIMEOUT_MS;
    this.enterSuppressUntil = now + PASTE_ENTER_SUPPRESS_WINDOW_MS;
  }

  reset(): void {
    this.lastPlainCharAt = undefined;
    this.consecutivePlainChars = 0;
    this.activeUntil = 0;
    this.enterSuppressUntil = 0;
  }
}