@lethevimlet/sshift 1.7.5 → 1.7.7

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.
@@ -82,6 +82,11 @@ class MobileTerminalHandler {
82
82
  // Set to true before intentionally blurring the textarea (e.g., collapseKeyboard)
83
83
  // so the blur handler knows not to auto-refocus
84
84
  this._intentionalBlur = false;
85
+ // True while the keyboard is collapsed (textarea blurred + readonly).
86
+ // Lets _collapseKeyboard early-out: it runs on EVERY touchstart, and
87
+ // repeatedly blurring/re-adding readonly needlessly tears down the
88
+ // IME session state.
89
+ this._keyboardCollapsed = true;
85
90
 
86
91
  // Bound methods (for event listener management)
87
92
  this._boundHandleTouchStart = this._handleTouchStart.bind(this);
@@ -268,18 +273,18 @@ class MobileTerminalHandler {
268
273
  handle.addEventListener('touchmove', (e) => {
269
274
  if (!this.touchState.isDragging) return;
270
275
  e.preventDefault();
271
-
276
+
272
277
  const touch = e.touches[0];
273
278
  const coords = this._screenToTerminalCoords(touch.clientX, touch.clientY);
274
-
279
+
275
280
  if (coords) {
276
281
  if (type === 'start') {
277
282
  this.selection.start = coords;
278
283
  } else {
279
284
  this.selection.end = coords;
280
285
  }
281
-
282
- this._updateSelection();
286
+
287
+ this._scheduleSelectionUpdate();
283
288
  }
284
289
  }, { passive: false });
285
290
 
@@ -553,27 +558,57 @@ class MobileTerminalHandler {
553
558
  });
554
559
 
555
560
  this.hiddenTextarea.addEventListener('compositionend', () => {
556
- this._isComposing = false;
557
- // Sync whatever the composition left in the textarea. Because the
558
- // diff runs against the persistent _sentValue baseline, this is
559
- // idempotent: text that was already sent by an input event firing
560
- // before compositionstart (a Gboard timing quirk) is not re-sent,
561
- // and a duplicate input event firing right after compositionend
562
- // diffs to nothing.
563
- if (!this.touchState.isDragging) {
564
- this._syncTextareaToTerminal();
565
- }
561
+ this._onCompositionEnd();
566
562
  });
567
563
  }
568
564
 
565
+ /**
566
+ * compositionend handler, extracted so the deferred-reset behaviour is
567
+ * unit-testable without a DOM.
568
+ * @private
569
+ */
570
+ _onCompositionEnd() {
571
+ this._isComposing = false;
572
+ // 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.
576
+ if (this._pendingTrackingReset) {
577
+ this._pendingTrackingReset = false;
578
+ this._resetInputTracking();
579
+ return;
580
+ }
581
+ // Sync whatever the composition left in the textarea. Because the
582
+ // diff runs against the persistent _sentValue baseline, this is
583
+ // idempotent: text that was already sent by an input event firing
584
+ // before compositionstart (a Gboard timing quirk) is not re-sent,
585
+ // and a duplicate input event firing right after compositionend
586
+ // diffs to nothing.
587
+ if (!this.touchState.isDragging) {
588
+ this._syncTextareaToTerminal();
589
+ }
590
+ }
591
+
569
592
  /**
570
593
  * Reset the textarea input tracking: clear the textarea and the
571
594
  * synced-content baseline together so future diffs start from a clean
572
595
  * state. Must be used everywhere the textarea is cleared — clearing one
573
596
  * without the other makes the next diff re-send or drop text.
597
+ *
598
+ * Composition guard: clearing the textarea while a Gboard/IME
599
+ * composition is in flight is INVISIBLE to the keyboard (programmatic
600
+ * value assignment fires no input/beforeinput events the IME can see).
601
+ * Gboard keeps its own cached model of the field content, notices the
602
+ * mismatch on the next suggestion tap, and re-inserts text computed
603
+ * from the stale model — the "autocomplete types repetitive text"
604
+ * bug. When composing, defer the reset to compositionend instead.
574
605
  * @private
575
606
  */
576
607
  _resetInputTracking() {
608
+ if (this._isComposing) {
609
+ this._pendingTrackingReset = true;
610
+ return;
611
+ }
577
612
  if (this.hiddenTextarea) {
578
613
  this.hiddenTextarea.value = '';
579
614
  }
@@ -736,6 +771,12 @@ class MobileTerminalHandler {
736
771
  // Stop scroll tracking
737
772
  this._stopScrollTracking();
738
773
 
774
+ // Cancel any pending coalesced selection update
775
+ if (this._selectionUpdateRAF) {
776
+ cancelAnimationFrame(this._selectionUpdateRAF);
777
+ this._selectionUpdateRAF = null;
778
+ }
779
+
739
780
  // Remove viewport scroll listener
740
781
  if (this._viewportScrollListener) {
741
782
  const viewportEl = this.terminal && this.terminal.element
@@ -837,14 +878,14 @@ class MobileTerminalHandler {
837
878
  e.preventDefault();
838
879
  const touch = e.touches[0];
839
880
  const coords = this._screenToTerminalCoords(touch.clientX, touch.clientY);
840
-
881
+
841
882
  if (coords) {
842
883
  if (this.touchState.dragHandle === 'start') {
843
884
  this.selection.start = coords;
844
885
  } else {
845
886
  this.selection.end = coords;
846
887
  }
847
- this._updateSelection();
888
+ this._scheduleSelectionUpdate();
848
889
  }
849
890
  }
850
891
  }
@@ -1027,6 +1068,25 @@ class MobileTerminalHandler {
1027
1068
  };
1028
1069
  }
1029
1070
 
1071
+ /**
1072
+ * Coalesce selection overlay rebuilds to one per frame.
1073
+ * _updateSelection() scans the buffer for the selection text and
1074
+ * rebuilds the overlay DOM — far too heavy to run per touchmove event
1075
+ * during a selection-handle drag (events outpace frames on 90/120Hz
1076
+ * phones). The drag handlers update selection.start/end immediately
1077
+ * and schedule the visual refresh once per frame instead.
1078
+ * @private
1079
+ */
1080
+ _scheduleSelectionUpdate() {
1081
+ if (this._selectionUpdateRAF) return;
1082
+ this._selectionUpdateRAF = requestAnimationFrame(() => {
1083
+ this._selectionUpdateRAF = null;
1084
+ if (this.selection.active) {
1085
+ this._updateSelection();
1086
+ }
1087
+ });
1088
+ }
1089
+
1030
1090
  /**
1031
1091
  * Update selection overlay and handles
1032
1092
  * @private
@@ -1486,25 +1546,51 @@ class MobileTerminalHandler {
1486
1546
  if (this.touchState.isSelecting || this.selection.active) {
1487
1547
  return;
1488
1548
  }
1489
-
1549
+
1490
1550
  if (this.hiddenTextarea) {
1551
+ // Already focused (e.g. re-focus after a mobile-keys-bar tap or a
1552
+ // screen-sync while the keyboard stayed open): leave everything
1553
+ // untouched. Re-running the readonly dance or focusing again would
1554
+ // at best be a no-op and at worst tear down the IME session.
1555
+ if (document.activeElement === this.hiddenTextarea) {
1556
+ return;
1557
+ }
1558
+
1491
1559
  // Remove readonly to allow keyboard
1492
1560
  this.hiddenTextarea.removeAttribute('readonly');
1493
- // Reset textarea to empty when refocusing so the diff tracking
1494
- // starts from a clean state. Gboard autocorrect operates on the
1495
- // textarea content, so stale text from a previous focus would
1496
- // produce incorrect diffs.
1497
- this._resetInputTracking();
1561
+ // Do NOT clear the textarea on refocus. The diff baseline
1562
+ // (_sentValue) tracks exactly what was sent to the terminal, so
1563
+ // stale-looking content is NOT stale: textarea, baseline and the
1564
+ // remote prompt line all agree, and Gboard's own model of the
1565
+ // field (from its last focus query) agrees too.
1566
+ //
1567
+ // Wiping the value here (the old behaviour) is a programmatic
1568
+ // assignment that is INVISIBLE to Gboard: the keyboard keeps its
1569
+ // cached model ("hel"), the field is now empty, and the next
1570
+ // suggestion tap re-inserts text computed from the stale model.
1571
+ // The diff then treats that fragment as brand-new text and sends
1572
+ // it on top of what the terminal already echoed — the classic
1573
+ // "hel" + autocomplete → "helhel" duplication. Keeping the
1574
+ // textarea intact keeps the IME, the field and the baseline in
1575
+ // sync; autocomplete diffs then send only the true delta.
1576
+ //
1577
+ // Genuine context switches (session teardown, Enter/newline,
1578
+ // Tab key, selection start) still reset explicitly via
1579
+ // _resetInputTracking().
1498
1580
  this.hiddenTextarea.focus();
1581
+ this._keyboardCollapsed = false;
1499
1582
  }
1500
1583
  }
1501
-
1584
+
1502
1585
  /**
1503
1586
  * Collapse keyboard by blurring hidden textarea
1504
1587
  * @private
1505
1588
  */
1506
1589
  _collapseKeyboard() {
1507
1590
  if (this.hiddenTextarea) {
1591
+ // Already collapsed (this runs on every touchstart) — nothing to do.
1592
+ if (this._keyboardCollapsed) return;
1593
+ this._keyboardCollapsed = true;
1508
1594
  this._intentionalBlur = true;
1509
1595
  this.hiddenTextarea.blur();
1510
1596
  // Restore readonly to prevent keyboard on any accidental focus
@@ -1515,7 +1601,7 @@ class MobileTerminalHandler {
1515
1601
  }
1516
1602
  }
1517
1603
  }
1518
-
1604
+
1519
1605
  /**
1520
1606
  * Enable keyboard input by removing readonly from hidden textarea
1521
1607
  * @private
@@ -1523,6 +1609,7 @@ class MobileTerminalHandler {
1523
1609
  _enableKeyboardInput() {
1524
1610
  if (this.hiddenTextarea) {
1525
1611
  this.hiddenTextarea.removeAttribute('readonly');
1612
+ this._keyboardCollapsed = false;
1526
1613
  }
1527
1614
  }
1528
1615