@lethevimlet/sshift 1.0.2 → 1.0.4

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.
@@ -39,6 +39,12 @@ class MobileTerminalHandler {
39
39
 
40
40
  // Terminal resize listener disposable
41
41
  this._resizeDisposable = null;
42
+
43
+ // Scroll tracking (RAF loop + viewport scroll listener)
44
+ this._scrollTracking = false;
45
+ this._scrollRAF = null;
46
+ this._lastScrollOffset = 0;
47
+ this._viewportScrollListener = null;
42
48
 
43
49
  // UI elements
44
50
  this.container = null;
@@ -48,6 +54,8 @@ class MobileTerminalHandler {
48
54
  this.endHandle = null;
49
55
  this.contextMenu = null;
50
56
  this.hiddenTextarea = null;
57
+ this._isComposing = false;
58
+ this._compositionText = '';
51
59
  this.contextMenuUserPositioned = false; // Track if user has manually positioned the menu
52
60
 
53
61
  // Configuration
@@ -150,7 +158,7 @@ class MobileTerminalHandler {
150
158
  offsetY: contentTop - containerRect.top,
151
159
  cols,
152
160
  rows,
153
- scrollOffset: this.terminal.buffer.ydisp || 0,
161
+ scrollOffset: this.terminal.buffer.active.viewportY,
154
162
  containerRect
155
163
  };
156
164
  }
@@ -173,7 +181,7 @@ class MobileTerminalHandler {
173
181
  offsetY: 0,
174
182
  cols,
175
183
  rows,
176
- scrollOffset: this.terminal.buffer.ydisp || 0,
184
+ scrollOffset: this.terminal.buffer.active.viewportY,
177
185
  containerRect
178
186
  };
179
187
  }
@@ -433,8 +441,7 @@ class MobileTerminalHandler {
433
441
  this.hiddenTextarea.setAttribute('autocorrect', 'off');
434
442
  this.hiddenTextarea.setAttribute('autocapitalize', 'off');
435
443
  this.hiddenTextarea.setAttribute('spellcheck', 'false');
436
- this.hiddenTextarea.setAttribute('contenteditable', 'true');
437
- this.hiddenTextarea.setAttribute('readonly', 'true'); // Prevent keyboard on focus by default
444
+ this.hiddenTextarea.setAttribute('readonly', 'true');
438
445
  this.hiddenTextarea.style.position = 'absolute';
439
446
  this.hiddenTextarea.style.left = '-9999px';
440
447
  this.hiddenTextarea.style.top = '0';
@@ -447,21 +454,24 @@ class MobileTerminalHandler {
447
454
  // Handle paste events
448
455
  this.hiddenTextarea.addEventListener('paste', (e) => {
449
456
  e.preventDefault();
457
+ if (this._pasteInProgress) return;
450
458
  const text = e.clipboardData.getData('text');
451
459
  this._pasteText(text);
452
460
  });
453
461
 
454
462
  // Handle input events (for virtual keyboard)
455
463
  this.hiddenTextarea.addEventListener('input', (e) => {
456
- // Don't process input if in selection mode
457
464
  if (this.touchState.isSelecting) {
458
465
  e.preventDefault();
459
466
  this.hiddenTextarea.value = '';
460
467
  return;
461
468
  }
462
-
469
+
470
+ if (this._isComposing) {
471
+ return;
472
+ }
473
+
463
474
  if (e.data && !this.touchState.isDragging) {
464
- // Send input to terminal
465
475
  this._sendToTerminal(e.data);
466
476
  this.hiddenTextarea.value = '';
467
477
  }
@@ -469,12 +479,15 @@ class MobileTerminalHandler {
469
479
 
470
480
  // Handle keydown for special keys
471
481
  this.hiddenTextarea.addEventListener('keydown', (e) => {
472
- // Don't process keys if in selection mode
473
482
  if (this.touchState.isSelecting) {
474
483
  e.preventDefault();
475
484
  return;
476
485
  }
477
-
486
+
487
+ if (this._isComposing) {
488
+ return;
489
+ }
490
+
478
491
  if (e.key === 'Enter') {
479
492
  e.preventDefault();
480
493
  this._sendToTerminal('\r');
@@ -496,10 +509,29 @@ class MobileTerminalHandler {
496
509
  e.preventDefault();
497
510
  this._intentionalBlur = true;
498
511
  this.hiddenTextarea.blur();
499
- // Also collapse any active keyboard
500
512
  this._collapseKeyboard();
501
513
  }
502
514
  });
515
+
516
+ // Composition events for IME/autocomplete handling
517
+ this.hiddenTextarea.addEventListener('compositionstart', () => {
518
+ this._isComposing = true;
519
+ this._compositionText = '';
520
+ });
521
+
522
+ this.hiddenTextarea.addEventListener('compositionupdate', (e) => {
523
+ this._compositionText = e.data || '';
524
+ });
525
+
526
+ this.hiddenTextarea.addEventListener('compositionend', (e) => {
527
+ this._isComposing = false;
528
+ const composed = this.hiddenTextarea.value;
529
+ if (composed && !this.touchState.isDragging) {
530
+ this._sendToTerminal(composed);
531
+ }
532
+ this._compositionText = '';
533
+ this.hiddenTextarea.value = '';
534
+ });
503
535
  }
504
536
 
505
537
  /**
@@ -538,6 +570,28 @@ class MobileTerminalHandler {
538
570
  });
539
571
  });
540
572
  }
573
+
574
+ // Listen for native scroll on .xterm-viewport element
575
+ const viewportEl = this.terminal.element
576
+ ? this.terminal.element.querySelector('.xterm-viewport')
577
+ : null;
578
+ if (viewportEl) {
579
+ this._viewportScrollListener = () => {
580
+ if (this.selection.active) {
581
+ requestAnimationFrame(() => {
582
+ if (this.selection.active) {
583
+ const currentScrollOffset = this.terminal.buffer.active.viewportY;
584
+ if (currentScrollOffset !== this._lastScrollOffset) {
585
+ this._lastScrollOffset = currentScrollOffset;
586
+ this._updateSelectionOverlay(this.selection.start, this.selection.end);
587
+ this._updateHandles(this.selection.start, this.selection.end);
588
+ }
589
+ }
590
+ });
591
+ }
592
+ };
593
+ viewportEl.addEventListener('scroll', this._viewportScrollListener, { passive: true });
594
+ }
541
595
  }
542
596
 
543
597
  /**
@@ -567,6 +621,20 @@ class MobileTerminalHandler {
567
621
  this._resizeDisposable = null;
568
622
  }
569
623
 
624
+ // Stop scroll tracking
625
+ this._stopScrollTracking();
626
+
627
+ // Remove viewport scroll listener
628
+ if (this._viewportScrollListener) {
629
+ const viewportEl = this.terminal && this.terminal.element
630
+ ? this.terminal.element.querySelector('.xterm-viewport')
631
+ : null;
632
+ if (viewportEl) {
633
+ viewportEl.removeEventListener('scroll', this._viewportScrollListener);
634
+ }
635
+ this._viewportScrollListener = null;
636
+ }
637
+
570
638
  // Remove UI elements
571
639
  if (this.selectionOverlay && this.selectionOverlay.parentNode) {
572
640
  this.selectionOverlay.parentNode.removeChild(this.selectionOverlay);
@@ -853,12 +921,16 @@ class MobileTerminalHandler {
853
921
  */
854
922
  _updateSelection() {
855
923
  if (!this.selection.active || !this.selection.start || !this.selection.end) {
924
+ this._stopScrollTracking();
856
925
  this.selectionOverlay.style.display = 'none';
857
926
  this.startHandle.style.display = 'none';
858
927
  this.endHandle.style.display = 'none';
859
928
  return;
860
929
  }
861
930
 
931
+ // Start scroll tracking when selection is active
932
+ this._startScrollTracking();
933
+
862
934
  // Force keyboard to collapse when selection is active
863
935
  this._collapseKeyboard();
864
936
 
@@ -940,9 +1012,18 @@ class MobileTerminalHandler {
940
1012
  const textBeginX = (start.y < end.y || (start.y === end.y && start.x <= end.x)) ? start.x : end.x;
941
1013
  const textEndX = (start.y < end.y || (start.y === end.y && start.x <= end.x)) ? end.x : start.x;
942
1014
 
1015
+ // Visible viewport range in buffer coordinates
1016
+ const visibleTop = metrics.scrollOffset;
1017
+ const visibleBottom = metrics.scrollOffset + metrics.rows - 1;
1018
+
943
1019
  this.selectionOverlay.innerHTML = '';
944
1020
 
945
1021
  for (let y = startY; y <= endY; y++) {
1022
+ // Skip lines entirely above the visible viewport
1023
+ if (y < visibleTop) continue;
1024
+ // No more visible lines below
1025
+ if (y > visibleBottom) break;
1026
+
946
1027
  const lineStartX = (y === startY) ? textBeginX : 0;
947
1028
  const lineEndX = (y === endY) ? textEndX : metrics.cols - 1;
948
1029
  const extendsToRight = (lineEndX === metrics.cols - 1);
@@ -977,6 +1058,19 @@ class MobileTerminalHandler {
977
1058
  * @private
978
1059
  */
979
1060
  _updateHandles(start, end) {
1061
+ const metrics = this._getTerminalMetrics();
1062
+ if (!metrics) {
1063
+ this.startHandle.style.display = 'none';
1064
+ this.endHandle.style.display = 'none';
1065
+ return;
1066
+ }
1067
+
1068
+ const { offsetY, contentHeight, cellHeight } = metrics;
1069
+
1070
+ // Clamp limits for handle positions within visible content area
1071
+ const clampMinY = offsetY;
1072
+ const clampMaxY = offsetY + contentHeight - cellHeight;
1073
+
980
1074
  const startPos = start.y * this.terminal.cols + start.x;
981
1075
  const endPos = end.y * this.terminal.cols + end.x;
982
1076
  const isInverted = startPos > endPos;
@@ -989,7 +1083,10 @@ class MobileTerminalHandler {
989
1083
  } else {
990
1084
  this.startHandle.style.left = `${startCoords.x}px`;
991
1085
  }
992
- this.startHandle.style.top = `${startCoords.y + startCoords.height}px`;
1086
+ const handleTop = startCoords.y + startCoords.height;
1087
+ this.startHandle.style.top = `${Math.max(clampMinY, Math.min(clampMaxY, handleTop))}px`;
1088
+ } else {
1089
+ this.startHandle.style.display = 'none';
993
1090
  }
994
1091
 
995
1092
  const endCoords = this._terminalToScreenCoords(end.x, end.y);
@@ -1000,7 +1097,10 @@ class MobileTerminalHandler {
1000
1097
  } else {
1001
1098
  this.endHandle.style.left = `${endCoords.x + endCoords.width}px`;
1002
1099
  }
1003
- this.endHandle.style.top = `${endCoords.y + endCoords.height}px`;
1100
+ const handleTop = endCoords.y + endCoords.height;
1101
+ this.endHandle.style.top = `${Math.max(clampMinY, Math.min(clampMaxY, handleTop))}px`;
1102
+ } else {
1103
+ this.endHandle.style.display = 'none';
1004
1104
  }
1005
1105
 
1006
1106
  if (isInverted) {
@@ -1016,6 +1116,52 @@ class MobileTerminalHandler {
1016
1116
  }
1017
1117
  }
1018
1118
 
1119
+ /**
1120
+ * Start tracking terminal scroll position via RAF loop.
1121
+ * Updates overlay/handles when ydisp changes during active selection.
1122
+ * @private
1123
+ */
1124
+ _startScrollTracking() {
1125
+ if (this._scrollTracking) return;
1126
+ this._scrollTracking = true;
1127
+ this._lastScrollOffset = this.terminal.buffer.active.viewportY;
1128
+ this._scrollTrackLoop();
1129
+ }
1130
+
1131
+ /**
1132
+ * Stop the RAF scroll tracking loop
1133
+ * @private
1134
+ */
1135
+ _stopScrollTracking() {
1136
+ this._scrollTracking = false;
1137
+ if (this._scrollRAF) {
1138
+ cancelAnimationFrame(this._scrollRAF);
1139
+ this._scrollRAF = null;
1140
+ }
1141
+ }
1142
+
1143
+ /**
1144
+ * RAF loop that checks for scroll offset changes and updates selection visuals
1145
+ * @private
1146
+ */
1147
+ _scrollTrackLoop() {
1148
+ if (!this._scrollTracking || !this.selection.active) {
1149
+ this._scrollRAF = null;
1150
+ return;
1151
+ }
1152
+
1153
+ const currentScrollOffset = this.terminal.buffer.active.viewportY;
1154
+ if (currentScrollOffset !== this._lastScrollOffset) {
1155
+ this._lastScrollOffset = currentScrollOffset;
1156
+ if (this.selection.start && this.selection.end) {
1157
+ this._updateSelectionOverlay(this.selection.start, this.selection.end);
1158
+ this._updateHandles(this.selection.start, this.selection.end);
1159
+ }
1160
+ }
1161
+
1162
+ this._scrollRAF = requestAnimationFrame(() => this._scrollTrackLoop());
1163
+ }
1164
+
1019
1165
  /**
1020
1166
  * Show context menu at position
1021
1167
  * @private
@@ -1050,7 +1196,7 @@ class MobileTerminalHandler {
1050
1196
 
1051
1197
  // Position above the start handle
1052
1198
  let posX = startHandleRect.left + (startHandleRect.width / 2) - (menuWidth / 2);
1053
- let posY = startHandleRect.top - menuHeight - 10; // 10px gap above handle
1199
+ let posY = startHandleRect.top - menuHeight - 55; // 55px gap above handle to keep clear of finger
1054
1200
 
1055
1201
  // Adjust position to stay within viewport
1056
1202
  if (posX + menuWidth > window.innerWidth) {
@@ -1143,16 +1289,11 @@ class MobileTerminalHandler {
1143
1289
  _pasteText(text) {
1144
1290
  if (!text) return;
1145
1291
 
1146
- // Send paste to server
1147
1292
  const session = this.app.sessions.get(this.sessionId);
1148
- if (session && session.socket && session.connected) {
1149
- session.socket.emit('terminal:input', {
1150
- sessionId: this.sessionId,
1151
- data: text
1152
- });
1293
+ if (session && session.connected) {
1294
+ this.app.sendChunkedInput(this.sessionId, text);
1295
+ this.app.showToast('Pasted', 'success');
1153
1296
  }
1154
-
1155
- this.app.showToast('Pasted', 'success');
1156
1297
  }
1157
1298
 
1158
1299
  /**
@@ -1195,6 +1336,9 @@ class MobileTerminalHandler {
1195
1336
  this.selection.end = null;
1196
1337
  this.selection.text = '';
1197
1338
 
1339
+ // Stop scroll tracking since selection is cleared
1340
+ this._stopScrollTracking();
1341
+
1198
1342
  // Reset selecting mode to allow keyboard input
1199
1343
  this.touchState.isSelecting = false;
1200
1344
 
@@ -1271,11 +1415,8 @@ class MobileTerminalHandler {
1271
1415
  */
1272
1416
  _sendToTerminal(text) {
1273
1417
  const session = this.app.sessions.get(this.sessionId);
1274
- if (session && session.socket && session.connected && session.isController) {
1275
- session.socket.emit('terminal:input', {
1276
- sessionId: this.sessionId,
1277
- data: text
1278
- });
1418
+ if (session && session.connected && session.isController) {
1419
+ this.app.socket.emit('ssh-data', { sessionId: this.sessionId, data: text });
1279
1420
  }
1280
1421
  }
1281
1422
 
@@ -1388,7 +1529,10 @@ class MobileTerminalHandler {
1388
1529
  break;
1389
1530
  case 'v':
1390
1531
  e.preventDefault();
1391
- this._pasteFromClipboard();
1532
+ this._pasteInProgress = true;
1533
+ this._pasteFromClipboard().finally(() => {
1534
+ setTimeout(() => { this._pasteInProgress = false; }, 100);
1535
+ });
1392
1536
  break;
1393
1537
  case 'a':
1394
1538
  e.preventDefault();
@@ -26,7 +26,8 @@ let config = {
26
26
 
27
27
  function createSessionState(sessionId) {
28
28
  return {
29
- buffer: '',
29
+ chunks: [],
30
+ bufferSize: 0,
30
31
  timerActive: false,
31
32
  paused: false,
32
33
  totalBytesReceived: 0,
@@ -49,26 +50,42 @@ function flushChunks(sessionId) {
49
50
  return;
50
51
  }
51
52
 
52
- if (session.buffer.length === 0) {
53
+ if (session.chunks.length === 0) {
53
54
  session.timerActive = false;
54
55
  return;
55
56
  }
56
57
 
57
58
  // Take up to chunkSize bytes from the buffer
58
- const chunk = session.buffer.substring(0, config.chunkSize);
59
- session.buffer = session.buffer.substring(config.chunkSize);
59
+ let sentSize = 0;
60
+ const sendChunks = [];
61
+ while (session.chunks.length > 0 && sentSize < config.chunkSize) {
62
+ const chunk = session.chunks.shift();
63
+ if (sentSize + chunk.length <= config.chunkSize) {
64
+ sendChunks.push(chunk);
65
+ sentSize += chunk.length;
66
+ } else {
67
+ const remaining = config.chunkSize - sentSize;
68
+ sendChunks.push(chunk.substring(0, remaining));
69
+ session.chunks.unshift(chunk.substring(remaining));
70
+ session.bufferSize -= remaining;
71
+ sentSize = config.chunkSize;
72
+ break;
73
+ }
74
+ }
75
+ session.bufferSize -= sentSize;
60
76
 
61
- session.totalBytesSent += chunk.length;
77
+ const data = sendChunks.join('');
78
+ session.totalBytesSent += data.length;
62
79
  session.chunksSent++;
63
80
 
64
81
  self.postMessage({
65
82
  type: 'data',
66
83
  sessionId: sessionId,
67
- data: chunk
84
+ data: data
68
85
  });
69
86
 
70
87
  // If there's more data to send, schedule next chunk
71
- if (session.buffer.length > 0) {
88
+ if (session.chunks.length > 0) {
72
89
  setTimeout(() => flushChunks(sessionId), config.flushInterval);
73
90
  } else {
74
91
  session.timerActive = false;
@@ -98,25 +115,30 @@ self.onmessage = function(e) {
98
115
  if (!sessionId) break;
99
116
 
100
117
  const session = getOrCreateSession(sessionId);
101
- session.totalBytesReceived += (msg.data || '').length;
118
+ const dataLen = (msg.data || '').length;
119
+ session.totalBytesReceived += dataLen;
102
120
 
103
121
  // Drop data if buffer is overwhelmed (prevents memory explosion)
104
- if (session.buffer.length + (msg.data || '').length > MAX_BUFFER_SIZE) {
122
+ if (session.bufferSize + dataLen > MAX_BUFFER_SIZE) {
105
123
  // Emergency: flush entire buffer immediately in one go
106
124
  // Better than dropping data silently
107
- if (session.buffer.length > 0) {
125
+ if (session.chunks.length > 0) {
108
126
  self.postMessage({
109
127
  type: 'data',
110
128
  sessionId: sessionId,
111
- data: session.buffer
129
+ data: session.chunks.join('')
112
130
  });
113
- session.totalBytesSent += session.buffer.length;
131
+ session.totalBytesSent += session.bufferSize;
114
132
  session.chunksSent++;
115
- session.buffer = '';
133
+ session.chunks = [];
134
+ session.bufferSize = 0;
116
135
  }
117
136
  }
118
137
 
119
- session.buffer += msg.data || '';
138
+ if (msg.data) {
139
+ session.chunks.push(msg.data);
140
+ session.bufferSize += dataLen;
141
+ }
120
142
  scheduleFlush(sessionId);
121
143
  break;
122
144
  }
@@ -127,16 +149,17 @@ self.onmessage = function(e) {
127
149
  if (!sessionId) break;
128
150
 
129
151
  const session = sessions.get(sessionId);
130
- if (!session || session.buffer.length === 0) break;
152
+ if (!session || session.chunks.length === 0) break;
131
153
 
132
154
  self.postMessage({
133
155
  type: 'data',
134
156
  sessionId: sessionId,
135
- data: session.buffer
157
+ data: session.chunks.join('')
136
158
  });
137
- session.totalBytesSent += session.buffer.length;
159
+ session.totalBytesSent += session.bufferSize;
138
160
  session.chunksSent++;
139
- session.buffer = '';
161
+ session.chunks = [];
162
+ session.bufferSize = 0;
140
163
  session.timerActive = false;
141
164
  break;
142
165
  }
@@ -155,7 +178,7 @@ self.onmessage = function(e) {
155
178
  const session = sessions.get(sessionId);
156
179
  if (session) {
157
180
  session.paused = false;
158
- if (session.buffer.length > 0) {
181
+ if (session.chunks.length > 0) {
159
182
  scheduleFlush(sessionId);
160
183
  }
161
184
  }
@@ -166,11 +189,11 @@ self.onmessage = function(e) {
166
189
  const sessionId = msg.sessionId;
167
190
  if (!sessionId) break;
168
191
  const session = sessions.get(sessionId);
169
- if (session && session.buffer.length > 0) {
192
+ if (session && session.chunks.length > 0) {
170
193
  self.postMessage({
171
194
  type: 'data',
172
195
  sessionId: sessionId,
173
- data: session.buffer
196
+ data: session.chunks.join('')
174
197
  });
175
198
  }
176
199
  sessions.delete(sessionId);
@@ -179,11 +202,11 @@ self.onmessage = function(e) {
179
202
 
180
203
  case 'destroy-all': {
181
204
  for (const [sessionId, session] of sessions) {
182
- if (session.buffer.length > 0) {
205
+ if (session.chunks.length > 0) {
183
206
  self.postMessage({
184
207
  type: 'data',
185
208
  sessionId: sessionId,
186
- data: session.buffer
209
+ data: session.chunks.join('')
187
210
  });
188
211
  }
189
212
  }
@@ -198,7 +221,7 @@ self.onmessage = function(e) {
198
221
  type: 'stats',
199
222
  sessionId: sessionId,
200
223
  stats: session ? {
201
- bufferLength: session.buffer.length,
224
+ bufferLength: session.bufferSize,
202
225
  totalBytesReceived: session.totalBytesReceived,
203
226
  totalBytesSent: session.totalBytesSent,
204
227
  chunksSent: session.chunksSent,