@lethevimlet/sshift 1.0.3 → 1.0.5

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) {
@@ -1190,6 +1336,9 @@ class MobileTerminalHandler {
1190
1336
  this.selection.end = null;
1191
1337
  this.selection.text = '';
1192
1338
 
1339
+ // Stop scroll tracking since selection is cleared
1340
+ this._stopScrollTracking();
1341
+
1193
1342
  // Reset selecting mode to allow keyboard input
1194
1343
  this.touchState.isSelecting = false;
1195
1344
 
@@ -1380,7 +1529,10 @@ class MobileTerminalHandler {
1380
1529
  break;
1381
1530
  case 'v':
1382
1531
  e.preventDefault();
1383
- this._pasteFromClipboard();
1532
+ this._pasteInProgress = true;
1533
+ this._pasteFromClipboard().finally(() => {
1534
+ setTimeout(() => { this._pasteInProgress = false; }, 100);
1535
+ });
1384
1536
  break;
1385
1537
  case 'a':
1386
1538
  e.preventDefault();
package/src/webapp/sw.js CHANGED
@@ -1,7 +1,6 @@
1
- const CACHE_NAME = 'sshift-v2';
1
+ const CACHE_NAME = 'sshift-__VERSION__';
2
2
 
3
3
  const PRECACHE_URLS = [
4
- '/index.html',
5
4
  '/css/style.css',
6
5
  '/libs/xterm/xterm.css',
7
6
  '/libs/font-awesome/css/all.min.css',
@@ -44,6 +43,24 @@ self.addEventListener('fetch', (event) => {
44
43
 
45
44
  if (url.protocol === 'http:' || url.protocol === 'https:') {
46
45
  if (url.pathname.startsWith('/socket.io/')) return;
46
+ if (url.pathname.startsWith('/api/')) return;
47
+ }
48
+
49
+ const isNavigation = event.request.mode === 'navigate';
50
+
51
+ if (isNavigation) {
52
+ event.respondWith(
53
+ fetch(event.request)
54
+ .then((response) => {
55
+ if (response && response.status === 200) {
56
+ const clone = response.clone();
57
+ caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone));
58
+ }
59
+ return response;
60
+ })
61
+ .catch(() => caches.match(event.request))
62
+ );
63
+ return;
47
64
  }
48
65
 
49
66
  event.respondWith(
package/sshift CHANGED
@@ -32,12 +32,12 @@
32
32
 
33
33
  const path = require('path');
34
34
  const fs = require('fs');
35
- const { spawn } = require('child_process');
35
+ const os = require('os');
36
36
 
37
37
  // Get the directory where this script is located
38
38
  const scriptDir = __dirname;
39
39
  const serverPath = path.join(scriptDir, 'src', 'server', 'index.js');
40
- const pidFile = path.join(scriptDir, '.sshift.pid');
40
+ const pidFile = path.join(os.homedir(), '.local', 'share', 'sshift', '.sshift.pid');
41
41
 
42
42
  // CLI argument parsing
43
43
  const args = process.argv.slice(2);
@@ -131,6 +131,34 @@ if (!fs.existsSync(serverPath)) {
131
131
  process.exit(1);
132
132
  }
133
133
 
134
+ // Helper: Ensure PID file directory exists
135
+ function ensurePidDir() {
136
+ const dir = path.dirname(pidFile);
137
+ if (!fs.existsSync(dir)) {
138
+ fs.mkdirSync(dir, { recursive: true });
139
+ }
140
+ }
141
+
142
+ // Helper: Write current PID to file
143
+ function writePidFile() {
144
+ ensurePidDir();
145
+ fs.writeFileSync(pidFile, process.pid.toString(), { mode: 0o644 });
146
+ }
147
+
148
+ // Helper: Remove PID file
149
+ function cleanupPidFile() {
150
+ try {
151
+ if (fs.existsSync(pidFile)) {
152
+ const storedPid = parseInt(fs.readFileSync(pidFile, 'utf8').trim(), 10);
153
+ if (isNaN(storedPid) || storedPid === process.pid) {
154
+ fs.unlinkSync(pidFile);
155
+ }
156
+ }
157
+ } catch (e) {
158
+ // Ignore cleanup errors
159
+ }
160
+ }
161
+
134
162
  // Helper: Check if sshift is running
135
163
  function isRunning() {
136
164
  if (fs.existsSync(pidFile)) {
@@ -265,5 +293,19 @@ if (options.dev) {
265
293
  process.env.NODE_ENV = 'development';
266
294
  }
267
295
 
296
+ // Write PID file before starting
297
+ writePidFile();
298
+
299
+ // Clean up PID file on exit
300
+ process.on('exit', cleanupPidFile);
301
+ process.on('SIGTERM', () => {
302
+ cleanupPidFile();
303
+ process.exit(0);
304
+ });
305
+ process.on('SIGINT', () => {
306
+ cleanupPidFile();
307
+ process.exit(0);
308
+ });
309
+
268
310
  // Start the server
269
311
  require(serverPath);
@@ -33,7 +33,7 @@ if ($installDir -eq "") {
33
33
  }
34
34
  $BinDir = Split-Path $installDir -Parent
35
35
  $BinDir = Join-Path $BinDir "bin"
36
- $PidFile = "$env:TEMP\sshift.pid"
36
+ $PidFile = Join-Path $installDir ".sshift.pid"
37
37
  $ServiceName = "sshift"
38
38
  $ServerPort = $port
39
39
 
package/sshift-install.sh CHANGED
@@ -19,7 +19,7 @@ set -e
19
19
  NODE_VERSION="20"
20
20
  NPM_PACKAGE="@lethevimlet/sshift"
21
21
  INSTALL_DIR="$HOME/.local/share/sshift"
22
- PID_FILE="/tmp/sshift.pid"
22
+ PID_FILE="$HOME/.local/share/sshift/.sshift.pid"
23
23
  SERVICE_NAME="sshift"
24
24
  SERVER_PORT=""
25
25