@lethevimlet/sshift 1.7.9 → 1.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -59,6 +59,11 @@ class MobileTerminalHandler {
59
59
  // terminal. All input diffing runs against this persistent baseline —
60
60
  // see _syncTextareaToTerminal().
61
61
  this._sentValue = '';
62
+ // Ring buffer of the last keyboard/IME events on the hidden textarea
63
+ // (Debug Info → "Copy Input Trace"). Gboard behaviour cannot be
64
+ // reproduced on a desktop; this is how device reports become
65
+ // actionable.
66
+ this._inputTrace = [];
62
67
  this.contextMenuUserPositioned = false; // Track if user has manually positioned the menu
63
68
 
64
69
  // Configuration
@@ -490,11 +495,11 @@ class MobileTerminalHandler {
490
495
  // empty snapshot, desyncing Gboard's own state from the textarea and
491
496
  // making it re-insert the full text on the next suggestion tap.
492
497
  this.hiddenTextarea.addEventListener('input', (e) => {
493
- if (this.touchState.isSelecting) {
494
- e.preventDefault();
495
- this._resetInputTracking();
496
- return;
497
- }
498
+ this._trace('input', {
499
+ inputType: e.inputType,
500
+ data: e.data,
501
+ composing: !!(e.isComposing || this._isComposing)
502
+ });
498
503
 
499
504
  // While a composition is in flight we don't send anything; the
500
505
  // textarea is synced once on compositionend. (Sending live
@@ -504,7 +509,7 @@ class MobileTerminalHandler {
504
509
  return;
505
510
  }
506
511
 
507
- if (this.touchState.isDragging) {
512
+ if (this.touchState.isDragging || this.selection.active) {
508
513
  return;
509
514
  }
510
515
 
@@ -513,7 +518,10 @@ class MobileTerminalHandler {
513
518
 
514
519
  // Handle keydown for special keys
515
520
  this.hiddenTextarea.addEventListener('keydown', (e) => {
516
- if (this.touchState.isSelecting) {
521
+ if (e.key === 'Enter' || e.key === 'Backspace' || e.key === 'Tab') {
522
+ this._trace('keydown', { key: e.key, composing: this._isComposing });
523
+ }
524
+ if (this.selection.active) {
517
525
  e.preventDefault();
518
526
  return;
519
527
  }
@@ -524,9 +532,17 @@ class MobileTerminalHandler {
524
532
  }
525
533
 
526
534
  if (e.key === 'Enter') {
527
- e.preventDefault();
528
- this._sendToTerminal('\r');
529
- this._resetInputTracking();
535
+ // Let the browser insert the newline into the textarea. The input
536
+ // diff (_syncTextareaToTerminal) translates it to \r and KEEPS the
537
+ // submitted text in the field. Clearing the field here (the old
538
+ // behaviour) was a programmatic edit that Gboard never sees: its
539
+ // own model of the field still held the previous sentence, and the
540
+ // next suggestion tap re-committed "old sentence + new word" —
541
+ // which the diff then faithfully re-typed into the terminal
542
+ // (the "autocomplete repeats previous sentences" bug). The field
543
+ // is only cleared when the keyboard is collapsed (textarea blurred),
544
+ // at which point the IME session is torn down anyway.
545
+ return;
530
546
  } else if (e.key === 'Backspace') {
531
547
  // Let the browser handle backspace in the textarea; the input diff
532
548
  // will detect the deletion and send \x7f to the terminal.
@@ -538,12 +554,13 @@ class MobileTerminalHandler {
538
554
  } else if (e.key === 'Tab') {
539
555
  e.preventDefault();
540
556
  this._sendToTerminal('\t');
541
- this._resetInputTracking();
557
+ // No tracking reset: see the Enter comment above.
542
558
  }
543
559
  });
544
560
 
545
561
  // Prevent focus when in selection mode or when selection is active
546
562
  this.hiddenTextarea.addEventListener('focus', (e) => {
563
+ this._trace('focus', {});
547
564
  if (this.touchState.isSelecting || this.selection.active) {
548
565
  e.preventDefault();
549
566
  this._intentionalBlur = true;
@@ -552,16 +569,74 @@ class MobileTerminalHandler {
552
569
  }
553
570
  });
554
571
 
572
+ // The blur tears down the IME session: a reset that had to wait for
573
+ // the field to lose focus (see _resetInputTracking) is applied now.
574
+ this.hiddenTextarea.addEventListener('blur', () => {
575
+ this._trace('blur', {});
576
+ this._isComposing = false;
577
+ if (this._pendingTrackingReset) {
578
+ this._resetInputTracking();
579
+ }
580
+ });
581
+
555
582
  // Composition events for IME/autocomplete handling
556
- this.hiddenTextarea.addEventListener('compositionstart', () => {
583
+ this.hiddenTextarea.addEventListener('compositionstart', (e) => {
584
+ this._trace('compositionstart', { data: e && e.data });
557
585
  this._isComposing = true;
558
586
  });
559
587
 
560
- this.hiddenTextarea.addEventListener('compositionend', () => {
588
+ this.hiddenTextarea.addEventListener('compositionend', (e) => {
589
+ this._trace('compositionend', { data: e && e.data });
561
590
  this._onCompositionEnd();
562
591
  });
563
592
  }
564
593
 
594
+ /**
595
+ * Record one keyboard/IME event (ring buffer, see Debug Info → Copy
596
+ * Input Trace). Values are trimmed so the trace stays small.
597
+ * @private
598
+ */
599
+ _trace(kind, extra) {
600
+ if (!this._inputTrace) this._inputTrace = [];
601
+ const ta = this.hiddenTextarea;
602
+ const value = ta && typeof ta.value === 'string' ? ta.value : '';
603
+ const entry = {
604
+ t: Date.now(),
605
+ kind,
606
+ len: value.length,
607
+ tail: value.length > 48 ? '…' + value.slice(-48) : value,
608
+ sel: ta && typeof ta.selectionStart === 'number' ? [ta.selectionStart, ta.selectionEnd] : undefined,
609
+ sentLen: this._sentValue ? this._sentValue.length : 0
610
+ };
611
+ if (typeof document !== 'undefined' && ta) {
612
+ entry.focused = document.activeElement === ta;
613
+ }
614
+ Object.keys(extra || {}).forEach(k => {
615
+ let v = extra[k];
616
+ if (typeof v === 'string' && v.length > 64) v = v.slice(0, 64) + '…';
617
+ if (v !== undefined) entry[k] = v;
618
+ });
619
+ this._inputTrace.push(entry);
620
+ if (this._inputTrace.length > 400) this._inputTrace.splice(0, this._inputTrace.length - 400);
621
+ }
622
+
623
+ /**
624
+ * @returns {Array} copy of the keyboard/IME event trace
625
+ */
626
+ getInputTrace() {
627
+ return (this._inputTrace || []).slice();
628
+ }
629
+
630
+ /**
631
+ * @returns {boolean} true while the hidden textarea owns focus (a live
632
+ * IME session may be tracking its content)
633
+ * @private
634
+ */
635
+ _isTextareaFocused() {
636
+ return typeof document !== 'undefined' && !!this.hiddenTextarea &&
637
+ document.activeElement === this.hiddenTextarea;
638
+ }
639
+
565
640
  /**
566
641
  * compositionend handler, extracted so the deferred-reset behaviour is
567
642
  * unit-testable without a DOM.
@@ -570,13 +645,13 @@ class MobileTerminalHandler {
570
645
  _onCompositionEnd() {
571
646
  this._isComposing = false;
572
647
  // A reset was requested while the composition was in flight
573
- // (e.g. Enter/Tab committed through the input path, or a selection
574
- // started mid-word): apply it now, discarding the composition
575
- // result, instead of having cleared the field under the IME.
648
+ // (e.g. a selection started mid-word): apply it now, discarding the
649
+ // composition result, instead of having cleared the field under the
650
+ // IME. If the field is still focused the reset stays deferred (until
651
+ // blur) and the composition result is synced like any other input.
576
652
  if (this._pendingTrackingReset) {
577
- this._pendingTrackingReset = false;
578
653
  this._resetInputTracking();
579
- return;
654
+ if (!this._pendingTrackingReset) return;
580
655
  }
581
656
  // Sync whatever the composition left in the textarea. Because the
582
657
  // diff runs against the persistent _sentValue baseline, this is
@@ -602,17 +677,26 @@ class MobileTerminalHandler {
602
677
  * mismatch on the next suggestion tap, and re-inserts text computed
603
678
  * from the stale model — the "autocomplete types repetitive text"
604
679
  * bug. When composing, defer the reset to compositionend instead.
680
+ *
681
+ * Focus guard (v1.8.1): the same applies to ANY clear while the
682
+ * textarea is focused — the IME session is alive and keeps its own
683
+ * model. So a reset is only ever applied when the field is not focused
684
+ * (after the blur in _collapseKeyboard, or from the blur listener);
685
+ * otherwise it is remembered and applied on the next blur.
605
686
  * @private
606
687
  */
607
688
  _resetInputTracking() {
608
- if (this._isComposing) {
689
+ if (this._isComposing || this._isTextareaFocused()) {
609
690
  this._pendingTrackingReset = true;
691
+ this._trace('reset-deferred', { composing: this._isComposing });
610
692
  return;
611
693
  }
694
+ this._pendingTrackingReset = false;
612
695
  if (this.hiddenTextarea) {
613
696
  this.hiddenTextarea.value = '';
614
697
  }
615
698
  this._sentValue = '';
699
+ this._trace('reset', {});
616
700
  }
617
701
 
618
702
  /**
@@ -652,9 +736,17 @@ class MobileTerminalHandler {
652
736
  }
653
737
  }
654
738
 
655
- // Erase back to the divergence point. Count code points (not UTF-16
656
- // units) so astral characters (emoji etc.) get one DEL each.
657
- const removed = prev.substring(prefixLen);
739
+ // The field accumulates every submitted line (separated by \n — see
740
+ // the Enter keydown comment). Lines above the current one were already
741
+ // submitted and cannot be edited any more, so a divergence before the
742
+ // current line's start must never turn into DELs (in a multi-line TUI
743
+ // editor a DEL at column 0 joins lines).
744
+ const lineStart = prev.lastIndexOf('\n') + 1;
745
+
746
+ // Erase back to the divergence point (clamped to the current line).
747
+ // Count code points (not UTF-16 units) so astral characters (emoji
748
+ // etc.) get one DEL each.
749
+ const removed = prev.substring(Math.max(prefixLen, lineStart));
658
750
  const deleteCount = removed ? Array.from(removed).length : 0;
659
751
 
660
752
  // Retype the remainder. Translate newlines (some keyboards commit
@@ -665,6 +757,7 @@ class MobileTerminalHandler {
665
757
  insert = insert.replace(/\r\n/g, '\r').replace(/\n/g, '\r');
666
758
  }
667
759
 
760
+ this._trace('sync', { prefixLen, del: deleteCount, insert });
668
761
  if (deleteCount > 0) {
669
762
  this._sendToTerminal('\x7f'.repeat(deleteCount));
670
763
  }
@@ -672,13 +765,9 @@ class MobileTerminalHandler {
672
765
  this._sendToTerminal(insert);
673
766
  }
674
767
 
675
- if (hadNewline) {
676
- // The line was submitted — start from a clean slate (mirrors the
677
- // Enter keydown path).
678
- this._resetInputTracking();
679
- } else {
680
- this._sentValue = curr;
681
- }
768
+ // Keep the submitted text (including the newline) as the baseline —
769
+ // never clear the field while the IME may still be tracking it.
770
+ this._sentValue = curr;
682
771
  }
683
772
 
684
773
  /**
@@ -820,9 +909,17 @@ class MobileTerminalHandler {
820
909
  // during long press detection. This will be reset in touchEnd if it's
821
910
  // a quick tap for text input.
822
911
  this.touchState.isSelecting = true;
823
-
824
- // Always prevent keyboard from appearing during touch interactions
825
- this._collapseKeyboard();
912
+
913
+ // Do NOT collapse the keyboard here. Until v1.8.0 every touchstart
914
+ // blurred AND cleared the hidden textarea and a quick tap re-focused
915
+ // it ~100 ms later. That is a programmatic clear under a live Gboard
916
+ // session (Chrome coalesces the blur/focus into one IME state update
917
+ // when they land close together, so Gboard never restarts its field
918
+ // model): the next suggestion tap then re-inserted text from the
919
+ // stale model — the "autocomplete repeats words" family. A tap on the
920
+ // terminal while typing is extremely common (dismissing a suggestion
921
+ // popup, repositioning). Long press, double tap, selection and drags
922
+ // collapse the keyboard themselves when they are detected.
826
923
 
827
924
  const touch = e.touches[0];
828
925
  const now = Date.now();
@@ -1577,6 +1674,7 @@ class MobileTerminalHandler {
1577
1674
  // autocomplete" regression). Force the caret to the end so typing
1578
1675
  // appends, matching both the diff and the remote prompt cursor.
1579
1676
  const len = this.hiddenTextarea.value.length;
1677
+ this._trace('focus-request', { from: document.activeElement ? document.activeElement.tagName : null });
1580
1678
  this.hiddenTextarea.focus();
1581
1679
  try { this.hiddenTextarea.setSelectionRange(len, len); } catch (_) {}
1582
1680
  this._keyboardCollapsed = false;
@@ -1593,9 +1691,17 @@ class MobileTerminalHandler {
1593
1691
  if (this._keyboardCollapsed) return;
1594
1692
  this._keyboardCollapsed = true;
1595
1693
  this._intentionalBlur = true;
1694
+ this._trace('collapse', {});
1596
1695
  this.hiddenTextarea.blur();
1597
1696
  // Restore readonly to prevent keyboard on any accidental focus
1598
1697
  this.hiddenTextarea.setAttribute('readonly', 'true');
1698
+ // The blur tore down the IME session, so this is the one place a
1699
+ // programmatic clear cannot desync Gboard's field model. Drop the
1700
+ // accumulated (already submitted) text so the field doesn't grow
1701
+ // for the whole session. Blur also ends any composition.
1702
+ this._isComposing = false;
1703
+ this._pendingTrackingReset = false;
1704
+ this._resetInputTracking();
1599
1705
  // Also blur any active element that might have focus
1600
1706
  if (document.activeElement && document.activeElement !== document.body) {
1601
1707
  document.activeElement.blur();