@lethevimlet/sshift 0.2.0 → 0.3.0
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.
- package/README.md +58 -1061
- package/example.config.json +1 -0
- package/package.json +3 -1
- package/src/server/index.js +182 -87
- package/src/server/utils/config.js +83 -17
- package/src/server/utils/env-loader.js +26 -5
- package/src/webapp/css/style.css +665 -42
- package/src/webapp/index.html +4 -1
- package/src/webapp/js/app.js +555 -78
- package/src/webapp/js/mobile-terminal.js +1370 -0
|
@@ -0,0 +1,1370 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MobileTerminalHandler - Provides mobile touch support for xterm.js terminals
|
|
3
|
+
*
|
|
4
|
+
* Features:
|
|
5
|
+
* - Touch-based text selection with custom handles
|
|
6
|
+
* - Long-press for word selection and context menu
|
|
7
|
+
* - Double-tap for line selection
|
|
8
|
+
* - Mobile-optimized context menu (Copy, Paste, Clear)
|
|
9
|
+
* - Virtual keyboard viewport management
|
|
10
|
+
* - Coordinate mapping from screen touches to terminal row/column
|
|
11
|
+
*/
|
|
12
|
+
class MobileTerminalHandler {
|
|
13
|
+
constructor(terminal, session, app) {
|
|
14
|
+
this.terminal = terminal;
|
|
15
|
+
this.session = session;
|
|
16
|
+
this.app = app;
|
|
17
|
+
this.sessionId = session.id;
|
|
18
|
+
|
|
19
|
+
// Selection state
|
|
20
|
+
this.selection = {
|
|
21
|
+
start: null, // { x, y } in terminal coordinates
|
|
22
|
+
end: null, // { x, y } in terminal coordinates
|
|
23
|
+
active: false,
|
|
24
|
+
text: ''
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// Touch state
|
|
28
|
+
this.touchState = {
|
|
29
|
+
startTime: 0,
|
|
30
|
+
startX: 0,
|
|
31
|
+
startY: 0,
|
|
32
|
+
lastTapTime: 0,
|
|
33
|
+
isLongPress: false,
|
|
34
|
+
longPressTimer: null,
|
|
35
|
+
isDragging: false,
|
|
36
|
+
dragHandle: null, // 'start' or 'end'
|
|
37
|
+
isSelecting: false // True when in selection mode (prevents keyboard)
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// Terminal resize listener disposable
|
|
41
|
+
this._resizeDisposable = null;
|
|
42
|
+
|
|
43
|
+
// UI elements
|
|
44
|
+
this.container = null;
|
|
45
|
+
this.terminalElement = null;
|
|
46
|
+
this.selectionOverlay = null;
|
|
47
|
+
this.startHandle = null;
|
|
48
|
+
this.endHandle = null;
|
|
49
|
+
this.contextMenu = null;
|
|
50
|
+
this.hiddenTextarea = null;
|
|
51
|
+
this.contextMenuUserPositioned = false; // Track if user has manually positioned the menu
|
|
52
|
+
|
|
53
|
+
// Configuration
|
|
54
|
+
this.config = {
|
|
55
|
+
longPressDelay: 500, // ms
|
|
56
|
+
doubleTapDelay: 300, // ms
|
|
57
|
+
handleSize: 20, // px
|
|
58
|
+
contextMenuWidth: 50, // px (reduced for compact mobile UI)
|
|
59
|
+
minTouchTarget: 44, // px (Apple HIG recommendation)
|
|
60
|
+
scrollEdgeSize: 50, // px - trigger scroll when near edge
|
|
61
|
+
scrollSpeed: 2 // lines per frame
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// Viewport management
|
|
65
|
+
this.viewport = {
|
|
66
|
+
originalHeight: null,
|
|
67
|
+
keyboardHeight: 0,
|
|
68
|
+
isKeyboardVisible: false
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
// Bound methods (for event listener management)
|
|
72
|
+
this._boundHandleTouchStart = this._handleTouchStart.bind(this);
|
|
73
|
+
this._boundHandleTouchMove = this._handleTouchMove.bind(this);
|
|
74
|
+
this._boundHandleTouchEnd = this._handleTouchEnd.bind(this);
|
|
75
|
+
this._boundHandleResize = this._handleResize.bind(this);
|
|
76
|
+
this._boundHandleVisualViewport = this._handleVisualViewport.bind(this);
|
|
77
|
+
this._boundHandleContextMenu = this._handleContextMenu.bind(this);
|
|
78
|
+
this._boundHandleKeyDown = this._handleKeyDown.bind(this);
|
|
79
|
+
|
|
80
|
+
console.log('[MobileTerminal] Handler created for session:', this.sessionId);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Get accurate terminal rendering metrics accounting for CSS padding and font size
|
|
85
|
+
* Uses the actual canvas/xterm-screen element for precise coordinate mapping
|
|
86
|
+
* @private
|
|
87
|
+
* @returns {Object|null} Terminal metrics or null if unavailable
|
|
88
|
+
*/
|
|
89
|
+
_getTerminalMetrics() {
|
|
90
|
+
if (!this.terminal || !this.terminal.element) return null;
|
|
91
|
+
|
|
92
|
+
const containerRect = this.container.getBoundingClientRect();
|
|
93
|
+
if (containerRect.width === 0 || containerRect.height === 0) return null;
|
|
94
|
+
|
|
95
|
+
const cols = this.terminal.cols;
|
|
96
|
+
const rows = this.terminal.rows;
|
|
97
|
+
if (cols === 0 || rows === 0) return null;
|
|
98
|
+
|
|
99
|
+
const screenEl = this.terminal.element.querySelector('.xterm-screen');
|
|
100
|
+
if (!screenEl) {
|
|
101
|
+
return this._getFallbackMetrics(containerRect, cols, rows);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const canvasEl = screenEl.querySelector('canvas');
|
|
105
|
+
|
|
106
|
+
let contentLeft, contentTop, contentWidth, contentHeight;
|
|
107
|
+
|
|
108
|
+
if (canvasEl) {
|
|
109
|
+
const canvasRect = canvasEl.getBoundingClientRect();
|
|
110
|
+
if (canvasRect.width === 0 || canvasRect.height === 0) {
|
|
111
|
+
return this._getFallbackMetrics(containerRect, cols, rows);
|
|
112
|
+
}
|
|
113
|
+
contentLeft = canvasRect.left;
|
|
114
|
+
contentTop = canvasRect.top;
|
|
115
|
+
contentWidth = canvasRect.width;
|
|
116
|
+
contentHeight = canvasRect.height;
|
|
117
|
+
} else {
|
|
118
|
+
const screenRect = screenEl.getBoundingClientRect();
|
|
119
|
+
const style = window.getComputedStyle(screenEl);
|
|
120
|
+
const paddingLeft = parseFloat(style.paddingLeft) || 0;
|
|
121
|
+
const paddingRight = parseFloat(style.paddingRight) || 0;
|
|
122
|
+
const paddingTop = parseFloat(style.paddingTop) || 0;
|
|
123
|
+
const paddingBottom = parseFloat(style.paddingBottom) || 0;
|
|
124
|
+
|
|
125
|
+
contentLeft = screenRect.left + paddingLeft;
|
|
126
|
+
contentTop = screenRect.top + paddingTop;
|
|
127
|
+
contentWidth = screenRect.width - paddingLeft - paddingRight;
|
|
128
|
+
contentHeight = screenRect.height - paddingTop - paddingBottom;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (contentWidth <= 0 || contentHeight <= 0) {
|
|
132
|
+
return this._getFallbackMetrics(containerRect, cols, rows);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const cellWidth = contentWidth / cols;
|
|
136
|
+
const cellHeight = contentHeight / rows;
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
cellWidth,
|
|
140
|
+
cellHeight,
|
|
141
|
+
contentLeft,
|
|
142
|
+
contentTop,
|
|
143
|
+
offsetX: contentLeft - containerRect.left,
|
|
144
|
+
offsetY: contentTop - containerRect.top,
|
|
145
|
+
cols,
|
|
146
|
+
rows,
|
|
147
|
+
scrollOffset: this.terminal.buffer.ydisp || 0,
|
|
148
|
+
containerRect
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Fallback metrics using container rect (legacy behavior)
|
|
154
|
+
* @private
|
|
155
|
+
*/
|
|
156
|
+
_getFallbackMetrics(containerRect, cols, rows) {
|
|
157
|
+
const cellWidth = containerRect.width / cols;
|
|
158
|
+
const cellHeight = containerRect.height / rows;
|
|
159
|
+
return {
|
|
160
|
+
cellWidth,
|
|
161
|
+
cellHeight,
|
|
162
|
+
contentLeft: containerRect.left,
|
|
163
|
+
contentTop: containerRect.top,
|
|
164
|
+
offsetX: 0,
|
|
165
|
+
offsetY: 0,
|
|
166
|
+
cols,
|
|
167
|
+
rows,
|
|
168
|
+
scrollOffset: this.terminal.buffer.ydisp || 0,
|
|
169
|
+
containerRect
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Initialize the mobile handler - attach to terminal element
|
|
175
|
+
* @param {HTMLElement} terminalElement - The terminal wrapper element
|
|
176
|
+
*/
|
|
177
|
+
init(terminalElement) {
|
|
178
|
+
this.terminalElement = terminalElement;
|
|
179
|
+
this.container = terminalElement.querySelector('.terminal-container') || terminalElement;
|
|
180
|
+
|
|
181
|
+
if (!this.container) {
|
|
182
|
+
console.error('[MobileTerminal] No terminal container found');
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Create UI elements
|
|
187
|
+
this._createSelectionOverlay();
|
|
188
|
+
this._createSelectionHandles();
|
|
189
|
+
this._createContextMenu();
|
|
190
|
+
this._createHiddenTextarea();
|
|
191
|
+
|
|
192
|
+
// Attach event listeners
|
|
193
|
+
this._attachEventListeners();
|
|
194
|
+
|
|
195
|
+
console.log('[MobileTerminal] Initialized for session:', this.sessionId);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Create the selection overlay element
|
|
200
|
+
* @private
|
|
201
|
+
*/
|
|
202
|
+
_createSelectionOverlay() {
|
|
203
|
+
this.selectionOverlay = document.createElement('div');
|
|
204
|
+
this.selectionOverlay.className = 'mobile-selection-overlay';
|
|
205
|
+
this.selectionOverlay.style.display = 'none';
|
|
206
|
+
this.container.appendChild(this.selectionOverlay);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Create selection handles (start and end)
|
|
211
|
+
* @private
|
|
212
|
+
*/
|
|
213
|
+
_createSelectionHandles() {
|
|
214
|
+
// Start handle
|
|
215
|
+
this.startHandle = document.createElement('div');
|
|
216
|
+
this.startHandle.className = 'mobile-selection-handle mobile-selection-handle-start';
|
|
217
|
+
this.startHandle.innerHTML = '<div class="handle-visual"></div>';
|
|
218
|
+
this.startHandle.style.display = 'none';
|
|
219
|
+
this.container.appendChild(this.startHandle);
|
|
220
|
+
|
|
221
|
+
// End handle
|
|
222
|
+
this.endHandle = document.createElement('div');
|
|
223
|
+
this.endHandle.className = 'mobile-selection-handle mobile-selection-handle-end';
|
|
224
|
+
this.endHandle.innerHTML = '<div class="handle-visual"></div>';
|
|
225
|
+
this.endHandle.style.display = 'none';
|
|
226
|
+
this.container.appendChild(this.endHandle);
|
|
227
|
+
|
|
228
|
+
// Add touch listeners to handles
|
|
229
|
+
this._setupHandleTouchListeners(this.startHandle, 'start');
|
|
230
|
+
this._setupHandleTouchListeners(this.endHandle, 'end');
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Setup touch listeners for selection handles
|
|
235
|
+
* @private
|
|
236
|
+
*/
|
|
237
|
+
_setupHandleTouchListeners(handle, type) {
|
|
238
|
+
handle.addEventListener('touchstart', (e) => {
|
|
239
|
+
e.preventDefault();
|
|
240
|
+
e.stopPropagation();
|
|
241
|
+
|
|
242
|
+
// Set selecting mode to prevent keyboard
|
|
243
|
+
this.touchState.isSelecting = true;
|
|
244
|
+
this.touchState.isDragging = true;
|
|
245
|
+
this.touchState.dragHandle = type;
|
|
246
|
+
handle.classList.add('active');
|
|
247
|
+
}, { passive: false });
|
|
248
|
+
|
|
249
|
+
handle.addEventListener('touchmove', (e) => {
|
|
250
|
+
if (!this.touchState.isDragging) return;
|
|
251
|
+
e.preventDefault();
|
|
252
|
+
|
|
253
|
+
const touch = e.touches[0];
|
|
254
|
+
const coords = this._screenToTerminalCoords(touch.clientX, touch.clientY);
|
|
255
|
+
|
|
256
|
+
if (coords) {
|
|
257
|
+
// Update the appropriate handle position
|
|
258
|
+
if (type === 'start') {
|
|
259
|
+
// When dragging start handle, update start position
|
|
260
|
+
this.selection.start = coords;
|
|
261
|
+
} else {
|
|
262
|
+
// When dragging end handle, update end position
|
|
263
|
+
this.selection.end = coords;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// Ensure start is always before end in buffer order
|
|
267
|
+
// This prevents selection from inverting
|
|
268
|
+
this._normalizeSelection();
|
|
269
|
+
this._updateSelection();
|
|
270
|
+
}
|
|
271
|
+
}, { passive: false });
|
|
272
|
+
|
|
273
|
+
handle.addEventListener('touchend', (e) => {
|
|
274
|
+
e.preventDefault();
|
|
275
|
+
e.stopPropagation();
|
|
276
|
+
this.touchState.isDragging = false;
|
|
277
|
+
this.touchState.dragHandle = null;
|
|
278
|
+
handle.classList.remove('active');
|
|
279
|
+
|
|
280
|
+
// Update context menu position after handle drag
|
|
281
|
+
if (this.selection.active) {
|
|
282
|
+
this._positionContextMenu();
|
|
283
|
+
}
|
|
284
|
+
}, { passive: false });
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Normalize selection so start is always before end
|
|
289
|
+
* @private
|
|
290
|
+
*/
|
|
291
|
+
_normalizeSelection() {
|
|
292
|
+
if (!this.selection.start || !this.selection.end) return;
|
|
293
|
+
|
|
294
|
+
const start = this.selection.start;
|
|
295
|
+
const end = this.selection.end;
|
|
296
|
+
|
|
297
|
+
// Compare positions (row first, then column)
|
|
298
|
+
const startPos = start.y * this.terminal.cols + start.x;
|
|
299
|
+
const endPos = end.y * this.terminal.cols + end.x;
|
|
300
|
+
|
|
301
|
+
// If end is before start, swap them
|
|
302
|
+
if (endPos < startPos) {
|
|
303
|
+
const temp = this.selection.start;
|
|
304
|
+
this.selection.start = this.selection.end;
|
|
305
|
+
this.selection.end = temp;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Create the mobile context menu
|
|
311
|
+
* @private
|
|
312
|
+
*/
|
|
313
|
+
_createContextMenu() {
|
|
314
|
+
this.contextMenu = document.createElement('div');
|
|
315
|
+
this.contextMenu.className = 'mobile-context-menu';
|
|
316
|
+
this.contextMenu.innerHTML = `
|
|
317
|
+
<div class="mobile-menu-drag-handle">
|
|
318
|
+
<i class="fas fa-grip-lines"></i>
|
|
319
|
+
</div>
|
|
320
|
+
<div class="mobile-menu-row">
|
|
321
|
+
<button class="mobile-menu-btn" data-action="copy">
|
|
322
|
+
<i class="fas fa-copy"></i>
|
|
323
|
+
<span>Copy</span>
|
|
324
|
+
</button>
|
|
325
|
+
<button class="mobile-menu-btn" data-action="paste">
|
|
326
|
+
<i class="fas fa-paste"></i>
|
|
327
|
+
<span>Paste</span>
|
|
328
|
+
</button>
|
|
329
|
+
<button class="mobile-menu-btn" data-action="clear">
|
|
330
|
+
<i class="fas fa-times"></i>
|
|
331
|
+
<span>Clear</span>
|
|
332
|
+
</button>
|
|
333
|
+
</div>
|
|
334
|
+
`;
|
|
335
|
+
this.contextMenu.style.display = 'none';
|
|
336
|
+
document.body.appendChild(this.contextMenu);
|
|
337
|
+
|
|
338
|
+
// Setup drag handle
|
|
339
|
+
this._setupContextMenuDrag();
|
|
340
|
+
|
|
341
|
+
// Add click handlers
|
|
342
|
+
this.contextMenu.querySelectorAll('.mobile-menu-btn').forEach(btn => {
|
|
343
|
+
btn.addEventListener('click', (e) => {
|
|
344
|
+
e.preventDefault();
|
|
345
|
+
e.stopPropagation();
|
|
346
|
+
const action = btn.dataset.action;
|
|
347
|
+
this._handleMenuAction(action);
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
// Prevent context menu from closing on item touch
|
|
351
|
+
btn.addEventListener('touchstart', (e) => {
|
|
352
|
+
e.stopPropagation();
|
|
353
|
+
}, { passive: true });
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
// Close menu on outside click
|
|
357
|
+
document.addEventListener('click', (e) => {
|
|
358
|
+
if (!this.contextMenu.contains(e.target)) {
|
|
359
|
+
this.hideContextMenu();
|
|
360
|
+
}
|
|
361
|
+
});
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Setup drag functionality for context menu
|
|
366
|
+
* @private
|
|
367
|
+
*/
|
|
368
|
+
_setupContextMenuDrag() {
|
|
369
|
+
const dragHandle = this.contextMenu.querySelector('.mobile-menu-drag-handle');
|
|
370
|
+
if (!dragHandle) return;
|
|
371
|
+
|
|
372
|
+
let isDragging = false;
|
|
373
|
+
let startX, startY;
|
|
374
|
+
let menuX, menuY;
|
|
375
|
+
|
|
376
|
+
dragHandle.addEventListener('touchstart', (e) => {
|
|
377
|
+
e.preventDefault();
|
|
378
|
+
e.stopPropagation();
|
|
379
|
+
|
|
380
|
+
isDragging = true;
|
|
381
|
+
const touch = e.touches[0];
|
|
382
|
+
startX = touch.clientX;
|
|
383
|
+
startY = touch.clientY;
|
|
384
|
+
|
|
385
|
+
// Get current menu position
|
|
386
|
+
const rect = this.contextMenu.getBoundingClientRect();
|
|
387
|
+
menuX = rect.left;
|
|
388
|
+
menuY = rect.top;
|
|
389
|
+
|
|
390
|
+
dragHandle.style.cursor = 'grabbing';
|
|
391
|
+
}, { passive: false });
|
|
392
|
+
|
|
393
|
+
document.addEventListener('touchmove', (e) => {
|
|
394
|
+
if (!isDragging) return;
|
|
395
|
+
|
|
396
|
+
const touch = e.touches[0];
|
|
397
|
+
const deltaX = touch.clientX - startX;
|
|
398
|
+
const deltaY = touch.clientY - startY;
|
|
399
|
+
|
|
400
|
+
// Update menu position
|
|
401
|
+
const newX = menuX + deltaX;
|
|
402
|
+
const newY = menuY + deltaY;
|
|
403
|
+
|
|
404
|
+
// Keep menu within viewport bounds
|
|
405
|
+
const maxX = window.innerWidth - this.contextMenu.offsetWidth;
|
|
406
|
+
const maxY = window.innerHeight - this.contextMenu.offsetHeight;
|
|
407
|
+
|
|
408
|
+
this.contextMenu.style.left = `${Math.max(0, Math.min(maxX, newX))}px`;
|
|
409
|
+
this.contextMenu.style.top = `${Math.max(0, Math.min(maxY, newY))}px`;
|
|
410
|
+
|
|
411
|
+
// Mark that user has manually positioned the menu
|
|
412
|
+
this.contextMenuUserPositioned = true;
|
|
413
|
+
}, { passive: true });
|
|
414
|
+
|
|
415
|
+
document.addEventListener('touchend', () => {
|
|
416
|
+
if (isDragging) {
|
|
417
|
+
isDragging = false;
|
|
418
|
+
dragHandle.style.cursor = 'grab';
|
|
419
|
+
}
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* Create hidden textarea for clipboard operations and keyboard input
|
|
425
|
+
* @private
|
|
426
|
+
*/
|
|
427
|
+
_createHiddenTextarea() {
|
|
428
|
+
this.hiddenTextarea = document.createElement('textarea');
|
|
429
|
+
this.hiddenTextarea.className = 'mobile-hidden-textarea';
|
|
430
|
+
this.hiddenTextarea.setAttribute('autocomplete', 'off');
|
|
431
|
+
this.hiddenTextarea.setAttribute('autocorrect', 'off');
|
|
432
|
+
this.hiddenTextarea.setAttribute('autocapitalize', 'off');
|
|
433
|
+
this.hiddenTextarea.setAttribute('spellcheck', 'false');
|
|
434
|
+
this.hiddenTextarea.setAttribute('contenteditable', 'true');
|
|
435
|
+
this.hiddenTextarea.setAttribute('readonly', 'true'); // Prevent keyboard on focus by default
|
|
436
|
+
this.hiddenTextarea.style.position = 'absolute';
|
|
437
|
+
this.hiddenTextarea.style.left = '-9999px';
|
|
438
|
+
this.hiddenTextarea.style.top = '0';
|
|
439
|
+
this.hiddenTextarea.style.width = '1px';
|
|
440
|
+
this.hiddenTextarea.style.height = '1px';
|
|
441
|
+
this.hiddenTextarea.style.opacity = '0';
|
|
442
|
+
this.hiddenTextarea.style.fontSize = '16px'; // Prevent zoom on iOS
|
|
443
|
+
document.body.appendChild(this.hiddenTextarea);
|
|
444
|
+
|
|
445
|
+
// Handle paste events
|
|
446
|
+
this.hiddenTextarea.addEventListener('paste', (e) => {
|
|
447
|
+
e.preventDefault();
|
|
448
|
+
const text = e.clipboardData.getData('text');
|
|
449
|
+
this._pasteText(text);
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
// Handle input events (for virtual keyboard)
|
|
453
|
+
this.hiddenTextarea.addEventListener('input', (e) => {
|
|
454
|
+
// Don't process input if in selection mode
|
|
455
|
+
if (this.touchState.isSelecting) {
|
|
456
|
+
e.preventDefault();
|
|
457
|
+
this.hiddenTextarea.value = '';
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
if (e.data && !this.touchState.isDragging) {
|
|
462
|
+
// Send input to terminal
|
|
463
|
+
this._sendToTerminal(e.data);
|
|
464
|
+
this.hiddenTextarea.value = '';
|
|
465
|
+
}
|
|
466
|
+
});
|
|
467
|
+
|
|
468
|
+
// Handle keydown for special keys
|
|
469
|
+
this.hiddenTextarea.addEventListener('keydown', (e) => {
|
|
470
|
+
// Don't process keys if in selection mode
|
|
471
|
+
if (this.touchState.isSelecting) {
|
|
472
|
+
e.preventDefault();
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
if (e.key === 'Enter') {
|
|
477
|
+
e.preventDefault();
|
|
478
|
+
this._sendToTerminal('\r');
|
|
479
|
+
this.hiddenTextarea.value = '';
|
|
480
|
+
} else if (e.key === 'Backspace') {
|
|
481
|
+
e.preventDefault();
|
|
482
|
+
this._sendToTerminal('\x7f');
|
|
483
|
+
this.hiddenTextarea.value = '';
|
|
484
|
+
} else if (e.key === 'Tab') {
|
|
485
|
+
e.preventDefault();
|
|
486
|
+
this._sendToTerminal('\t');
|
|
487
|
+
this.hiddenTextarea.value = '';
|
|
488
|
+
}
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
// Prevent focus when in selection mode or when selection is active
|
|
492
|
+
this.hiddenTextarea.addEventListener('focus', (e) => {
|
|
493
|
+
if (this.touchState.isSelecting || this.selection.active) {
|
|
494
|
+
e.preventDefault();
|
|
495
|
+
this.hiddenTextarea.blur();
|
|
496
|
+
// Also collapse any active keyboard
|
|
497
|
+
this._collapseKeyboard();
|
|
498
|
+
}
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
* Attach event listeners to terminal element
|
|
504
|
+
* @private
|
|
505
|
+
*/
|
|
506
|
+
_attachEventListeners() {
|
|
507
|
+
// Touch events for selection
|
|
508
|
+
this.container.addEventListener('touchstart', this._boundHandleTouchStart, { passive: false });
|
|
509
|
+
this.container.addEventListener('touchmove', this._boundHandleTouchMove, { passive: false });
|
|
510
|
+
this.container.addEventListener('touchend', this._boundHandleTouchEnd, { passive: false });
|
|
511
|
+
this.container.addEventListener('touchcancel', this._boundHandleTouchEnd, { passive: false });
|
|
512
|
+
|
|
513
|
+
// Context menu (right-click on desktop)
|
|
514
|
+
this.container.addEventListener('contextmenu', this._boundHandleContextMenu);
|
|
515
|
+
|
|
516
|
+
// Viewport management
|
|
517
|
+
window.addEventListener('resize', this._boundHandleResize);
|
|
518
|
+
|
|
519
|
+
// Visual viewport API for keyboard detection
|
|
520
|
+
if (window.visualViewport) {
|
|
521
|
+
window.visualViewport.addEventListener('resize', this._boundHandleVisualViewport);
|
|
522
|
+
window.visualViewport.addEventListener('scroll', this._boundHandleVisualViewport);
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
// Keyboard events
|
|
526
|
+
document.addEventListener('keydown', this._boundHandleKeyDown);
|
|
527
|
+
|
|
528
|
+
// Terminal resize events - refresh selection overlay when dimensions change
|
|
529
|
+
if (this.terminal && this.terminal.onResize) {
|
|
530
|
+
this._resizeDisposable = this.terminal.onResize(() => {
|
|
531
|
+
requestAnimationFrame(() => {
|
|
532
|
+
if (this.selection.active) {
|
|
533
|
+
this._updateSelection();
|
|
534
|
+
}
|
|
535
|
+
});
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* Detach all event listeners
|
|
542
|
+
*/
|
|
543
|
+
destroy() {
|
|
544
|
+
if (this.container) {
|
|
545
|
+
this.container.removeEventListener('touchstart', this._boundHandleTouchStart);
|
|
546
|
+
this.container.removeEventListener('touchmove', this._boundHandleTouchMove);
|
|
547
|
+
this.container.removeEventListener('touchend', this._boundHandleTouchEnd);
|
|
548
|
+
this.container.removeEventListener('touchcancel', this._boundHandleTouchEnd);
|
|
549
|
+
this.container.removeEventListener('contextmenu', this._boundHandleContextMenu);
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
window.removeEventListener('resize', this._boundHandleResize);
|
|
553
|
+
|
|
554
|
+
if (window.visualViewport) {
|
|
555
|
+
window.visualViewport.removeEventListener('resize', this._boundHandleVisualViewport);
|
|
556
|
+
window.visualViewport.removeEventListener('scroll', this._boundHandleVisualViewport);
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
document.removeEventListener('keydown', this._boundHandleKeyDown);
|
|
560
|
+
|
|
561
|
+
// Dispose terminal resize listener
|
|
562
|
+
if (this._resizeDisposable) {
|
|
563
|
+
this._resizeDisposable.dispose();
|
|
564
|
+
this._resizeDisposable = null;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
// Remove UI elements
|
|
568
|
+
if (this.selectionOverlay && this.selectionOverlay.parentNode) {
|
|
569
|
+
this.selectionOverlay.parentNode.removeChild(this.selectionOverlay);
|
|
570
|
+
}
|
|
571
|
+
if (this.startHandle && this.startHandle.parentNode) {
|
|
572
|
+
this.startHandle.parentNode.removeChild(this.startHandle);
|
|
573
|
+
}
|
|
574
|
+
if (this.endHandle && this.endHandle.parentNode) {
|
|
575
|
+
this.endHandle.parentNode.removeChild(this.endHandle);
|
|
576
|
+
}
|
|
577
|
+
if (this.contextMenu && this.contextMenu.parentNode) {
|
|
578
|
+
this.contextMenu.parentNode.removeChild(this.contextMenu);
|
|
579
|
+
}
|
|
580
|
+
if (this.hiddenTextarea && this.hiddenTextarea.parentNode) {
|
|
581
|
+
this.hiddenTextarea.parentNode.removeChild(this.hiddenTextarea);
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
console.log('[MobileTerminal] Handler destroyed for session:', this.sessionId);
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
/**
|
|
588
|
+
* Handle touch start event
|
|
589
|
+
* @private
|
|
590
|
+
*/
|
|
591
|
+
_handleTouchStart(e) {
|
|
592
|
+
// Ignore if not on mobile or if dragging a handle
|
|
593
|
+
if (!this.app.isMobile || this.touchState.isDragging) return;
|
|
594
|
+
|
|
595
|
+
// Set selecting mode immediately to prevent keyboard from appearing
|
|
596
|
+
// during long press detection. This will be reset in touchEnd if it's
|
|
597
|
+
// a quick tap for text input.
|
|
598
|
+
this.touchState.isSelecting = true;
|
|
599
|
+
|
|
600
|
+
// Always prevent keyboard from appearing during touch interactions
|
|
601
|
+
this._collapseKeyboard();
|
|
602
|
+
|
|
603
|
+
const touch = e.touches[0];
|
|
604
|
+
const now = Date.now();
|
|
605
|
+
|
|
606
|
+
this.touchState.startTime = now;
|
|
607
|
+
this.touchState.startX = touch.clientX;
|
|
608
|
+
this.touchState.startY = touch.clientY;
|
|
609
|
+
this.touchState.isLongPress = false;
|
|
610
|
+
|
|
611
|
+
// Check for double-tap
|
|
612
|
+
const timeSinceLastTap = now - this.touchState.lastTapTime;
|
|
613
|
+
if (timeSinceLastTap < this.config.doubleTapDelay) {
|
|
614
|
+
// Double-tap detected - select line
|
|
615
|
+
e.preventDefault();
|
|
616
|
+
this._handleDoubleTap(touch);
|
|
617
|
+
this.touchState.lastTapTime = 0;
|
|
618
|
+
return;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
// Start long-press timer
|
|
622
|
+
this.touchState.longPressTimer = setTimeout(() => {
|
|
623
|
+
this.touchState.isLongPress = true;
|
|
624
|
+
this._handleLongPress(touch);
|
|
625
|
+
}, this.config.longPressDelay);
|
|
626
|
+
|
|
627
|
+
this.touchState.lastTapTime = now;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
/**
|
|
631
|
+
* Handle touch move event
|
|
632
|
+
* @private
|
|
633
|
+
*/
|
|
634
|
+
_handleTouchMove(e) {
|
|
635
|
+
if (!this.app.isMobile) return;
|
|
636
|
+
|
|
637
|
+
// Cancel long-press if moved too much
|
|
638
|
+
if (this.touchState.longPressTimer) {
|
|
639
|
+
const touch = e.touches[0];
|
|
640
|
+
const dx = touch.clientX - this.touchState.startX;
|
|
641
|
+
const dy = touch.clientY - this.touchState.startY;
|
|
642
|
+
const distance = Math.sqrt(dx * dx + dy * dy);
|
|
643
|
+
|
|
644
|
+
if (distance > 10) { // 10px threshold
|
|
645
|
+
clearTimeout(this.touchState.longPressTimer);
|
|
646
|
+
this.touchState.longPressTimer = null;
|
|
647
|
+
// Reset selecting mode since this is not a long press anymore
|
|
648
|
+
// (user is scrolling or moving, not selecting)
|
|
649
|
+
this.touchState.isSelecting = false;
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
// Handle selection dragging
|
|
654
|
+
if (this.selection.active && this.touchState.isDragging) {
|
|
655
|
+
e.preventDefault();
|
|
656
|
+
const touch = e.touches[0];
|
|
657
|
+
const coords = this._screenToTerminalCoords(touch.clientX, touch.clientY);
|
|
658
|
+
|
|
659
|
+
if (coords) {
|
|
660
|
+
this.selection.end = coords;
|
|
661
|
+
this._updateSelection();
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
/**
|
|
667
|
+
* Handle touch end event
|
|
668
|
+
* @private
|
|
669
|
+
*/
|
|
670
|
+
_handleTouchEnd(e) {
|
|
671
|
+
if (!this.app.isMobile) return;
|
|
672
|
+
|
|
673
|
+
// Clear long-press timer
|
|
674
|
+
if (this.touchState.longPressTimer) {
|
|
675
|
+
clearTimeout(this.touchState.longPressTimer);
|
|
676
|
+
this.touchState.longPressTimer = null;
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
// If it was a long-press, don't do anything else
|
|
680
|
+
if (this.touchState.isLongPress) {
|
|
681
|
+
return;
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
// If we're dragging, finalize selection
|
|
685
|
+
if (this.touchState.isDragging) {
|
|
686
|
+
this.touchState.isDragging = false;
|
|
687
|
+
return;
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
// Single tap - clear selection if exists, otherwise focus for input
|
|
691
|
+
const touch = e.changedTouches[0];
|
|
692
|
+
const timeSinceStart = Date.now() - this.touchState.startTime;
|
|
693
|
+
|
|
694
|
+
if (timeSinceStart < 200) { // Quick tap
|
|
695
|
+
if (this.selection.active) {
|
|
696
|
+
this.clearSelection();
|
|
697
|
+
} else {
|
|
698
|
+
// Reset selecting mode to allow keyboard input for quick tap
|
|
699
|
+
this.touchState.isSelecting = false;
|
|
700
|
+
// Focus hidden textarea for keyboard input
|
|
701
|
+
this._focusHiddenTextarea();
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
/**
|
|
707
|
+
* Handle double-tap - select line
|
|
708
|
+
* @private
|
|
709
|
+
*/
|
|
710
|
+
_handleDoubleTap(touch) {
|
|
711
|
+
const coords = this._screenToTerminalCoords(touch.clientX, touch.clientY);
|
|
712
|
+
if (!coords) return;
|
|
713
|
+
|
|
714
|
+
// Set selecting mode to prevent keyboard from appearing
|
|
715
|
+
this.touchState.isSelecting = true;
|
|
716
|
+
|
|
717
|
+
// Force keyboard to collapse
|
|
718
|
+
this._collapseKeyboard();
|
|
719
|
+
|
|
720
|
+
// Select the entire line
|
|
721
|
+
this.selection.start = { x: 0, y: coords.y };
|
|
722
|
+
this.selection.end = { x: this.terminal.cols - 1, y: coords.y };
|
|
723
|
+
this.selection.active = true;
|
|
724
|
+
|
|
725
|
+
this._updateSelection();
|
|
726
|
+
this._showContextMenu(touch.clientX, touch.clientY);
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
/**
|
|
730
|
+
* Handle long-press - select word and show context menu
|
|
731
|
+
* @private
|
|
732
|
+
*/
|
|
733
|
+
_handleLongPress(touch) {
|
|
734
|
+
const coords = this._screenToTerminalCoords(touch.clientX, touch.clientY);
|
|
735
|
+
if (!coords) return;
|
|
736
|
+
|
|
737
|
+
// Set selecting mode to prevent keyboard from appearing
|
|
738
|
+
this.touchState.isSelecting = true;
|
|
739
|
+
|
|
740
|
+
// Force keyboard to collapse
|
|
741
|
+
this._collapseKeyboard();
|
|
742
|
+
|
|
743
|
+
// Select word at position
|
|
744
|
+
const wordRange = this._getWordAtPosition(coords.x, coords.y);
|
|
745
|
+
if (wordRange) {
|
|
746
|
+
this.selection.start = wordRange.start;
|
|
747
|
+
this.selection.end = wordRange.end;
|
|
748
|
+
this.selection.active = true;
|
|
749
|
+
|
|
750
|
+
this._updateSelection();
|
|
751
|
+
this._showContextMenu(touch.clientX, touch.clientY);
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
/**
|
|
756
|
+
* Get word boundaries at position
|
|
757
|
+
* @private
|
|
758
|
+
*/
|
|
759
|
+
_getWordAtPosition(x, y) {
|
|
760
|
+
// Get the line buffer
|
|
761
|
+
const buffer = this.terminal.buffer.active;
|
|
762
|
+
const line = buffer.getLine(y);
|
|
763
|
+
|
|
764
|
+
if (!line) return null;
|
|
765
|
+
|
|
766
|
+
const lineText = line.translateToString(true);
|
|
767
|
+
|
|
768
|
+
// Find word boundaries
|
|
769
|
+
let start = x;
|
|
770
|
+
let end = x;
|
|
771
|
+
|
|
772
|
+
// Expand left
|
|
773
|
+
while (start > 0 && this._isWordChar(lineText[start - 1])) {
|
|
774
|
+
start--;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
// Expand right
|
|
778
|
+
while (end < lineText.length && this._isWordChar(lineText[end])) {
|
|
779
|
+
end++;
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
if (start === end) {
|
|
783
|
+
// No word found, select single character
|
|
784
|
+
return { start: { x, y }, end: { x, y } };
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
return { start: { x: start, y }, end: { x: end - 1, y } };
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
/**
|
|
791
|
+
* Check if character is part of a word
|
|
792
|
+
* @private
|
|
793
|
+
*/
|
|
794
|
+
_isWordChar(char) {
|
|
795
|
+
return /[\w\-_./@]/.test(char);
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
/**
|
|
799
|
+
* Convert screen coordinates to terminal coordinates
|
|
800
|
+
* Uses actual terminal rendering metrics for accurate mapping
|
|
801
|
+
* @private
|
|
802
|
+
*/
|
|
803
|
+
_screenToTerminalCoords(screenX, screenY) {
|
|
804
|
+
const metrics = this._getTerminalMetrics();
|
|
805
|
+
if (!metrics) return null;
|
|
806
|
+
|
|
807
|
+
const { cellWidth, cellHeight, contentLeft, contentTop, scrollOffset, cols } = metrics;
|
|
808
|
+
|
|
809
|
+
const x = screenX - contentLeft;
|
|
810
|
+
const y = screenY - contentTop;
|
|
811
|
+
|
|
812
|
+
const termX = Math.floor(x / cellWidth);
|
|
813
|
+
const termY = Math.floor(y / cellHeight);
|
|
814
|
+
|
|
815
|
+
const bufferY = termY + scrollOffset;
|
|
816
|
+
const bufferLength = this.terminal.buffer.active.length;
|
|
817
|
+
|
|
818
|
+
return {
|
|
819
|
+
x: Math.max(0, Math.min(cols - 1, termX)),
|
|
820
|
+
y: Math.max(0, Math.min(bufferLength - 1, bufferY))
|
|
821
|
+
};
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
/**
|
|
825
|
+
* Convert terminal coordinates to container-relative screen position
|
|
826
|
+
* Returns coordinates relative to the container for absolute positioning
|
|
827
|
+
* @private
|
|
828
|
+
*/
|
|
829
|
+
_terminalToScreenCoords(termX, termY) {
|
|
830
|
+
const metrics = this._getTerminalMetrics();
|
|
831
|
+
if (!metrics) return null;
|
|
832
|
+
|
|
833
|
+
const { cellWidth, cellHeight, offsetX, offsetY, scrollOffset } = metrics;
|
|
834
|
+
const viewportY = termY - scrollOffset;
|
|
835
|
+
|
|
836
|
+
return {
|
|
837
|
+
x: offsetX + (termX * cellWidth),
|
|
838
|
+
y: offsetY + (viewportY * cellHeight),
|
|
839
|
+
width: cellWidth,
|
|
840
|
+
height: cellHeight
|
|
841
|
+
};
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
/**
|
|
845
|
+
* Update selection overlay and handles
|
|
846
|
+
* @private
|
|
847
|
+
*/
|
|
848
|
+
_updateSelection() {
|
|
849
|
+
if (!this.selection.active || !this.selection.start || !this.selection.end) {
|
|
850
|
+
this.selectionOverlay.style.display = 'none';
|
|
851
|
+
this.startHandle.style.display = 'none';
|
|
852
|
+
this.endHandle.style.display = 'none';
|
|
853
|
+
return;
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
// Force keyboard to collapse when selection is active
|
|
857
|
+
this._collapseKeyboard();
|
|
858
|
+
|
|
859
|
+
// Normalize selection (start should be before end)
|
|
860
|
+
const start = this.selection.start;
|
|
861
|
+
const end = this.selection.end;
|
|
862
|
+
|
|
863
|
+
// Get selection text
|
|
864
|
+
this.selection.text = this._getSelectionText();
|
|
865
|
+
|
|
866
|
+
// Update selection overlay
|
|
867
|
+
this._updateSelectionOverlay(start, end);
|
|
868
|
+
|
|
869
|
+
// Update handles
|
|
870
|
+
this._updateHandles(start, end);
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
/**
|
|
874
|
+
* Get selected text from terminal
|
|
875
|
+
* @private
|
|
876
|
+
*/
|
|
877
|
+
_getSelectionText() {
|
|
878
|
+
if (!this.selection.active || !this.selection.start || !this.selection.end) {
|
|
879
|
+
return '';
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
const buffer = this.terminal.buffer.active;
|
|
883
|
+
|
|
884
|
+
// Determine actual start and end positions (handle reversed selection)
|
|
885
|
+
const startPos = (this.selection.start.y < this.selection.end.y) ||
|
|
886
|
+
(this.selection.start.y === this.selection.end.y && this.selection.start.x <= this.selection.end.x)
|
|
887
|
+
? this.selection.start : this.selection.end;
|
|
888
|
+
const endPos = (this.selection.start.y < this.selection.end.y) ||
|
|
889
|
+
(this.selection.start.y === this.selection.end.y && this.selection.start.x <= this.selection.end.x)
|
|
890
|
+
? this.selection.end : this.selection.start;
|
|
891
|
+
|
|
892
|
+
let text = '';
|
|
893
|
+
|
|
894
|
+
for (let y = startPos.y; y <= endPos.y; y++) {
|
|
895
|
+
const line = buffer.getLine(y);
|
|
896
|
+
if (!line) continue;
|
|
897
|
+
|
|
898
|
+
let lineText = '';
|
|
899
|
+
|
|
900
|
+
if (startPos.y === endPos.y) {
|
|
901
|
+
// Single line selection
|
|
902
|
+
lineText = line.translateToString(false, startPos.x, endPos.x + 1);
|
|
903
|
+
} else if (y === startPos.y) {
|
|
904
|
+
// First line - from start.x to end of line
|
|
905
|
+
lineText = line.translateToString(false, startPos.x);
|
|
906
|
+
} else if (y === endPos.y) {
|
|
907
|
+
// Last line - from beginning to end.x
|
|
908
|
+
lineText = line.translateToString(false, 0, endPos.x + 1);
|
|
909
|
+
} else {
|
|
910
|
+
// Middle lines - full line
|
|
911
|
+
lineText = line.translateToString(false);
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
text += lineText;
|
|
915
|
+
if (y < endPos.y) {
|
|
916
|
+
text += '\n';
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
return text.trim();
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
/**
|
|
924
|
+
* Update selection overlay visual
|
|
925
|
+
* @private
|
|
926
|
+
*/
|
|
927
|
+
_updateSelectionOverlay(start, end) {
|
|
928
|
+
const startPos = this._terminalToScreenCoords(start.x, start.y);
|
|
929
|
+
const endPos = this._terminalToScreenCoords(end.x, end.y);
|
|
930
|
+
|
|
931
|
+
if (!startPos || !endPos) return;
|
|
932
|
+
|
|
933
|
+
const startY = Math.min(start.y, end.y);
|
|
934
|
+
const endY = Math.max(start.y, end.y);
|
|
935
|
+
|
|
936
|
+
this.selectionOverlay.innerHTML = '';
|
|
937
|
+
|
|
938
|
+
for (let y = startY; y <= endY; y++) {
|
|
939
|
+
const lineStartX = (y === startY) ? Math.min(start.x, end.x) : 0;
|
|
940
|
+
const lineEndX = (y === endY) ? Math.max(start.x, end.x) : this.terminal.cols - 1;
|
|
941
|
+
|
|
942
|
+
const lineStartPos = this._terminalToScreenCoords(lineStartX, y);
|
|
943
|
+
const lineEndPos = this._terminalToScreenCoords(lineEndX, y);
|
|
944
|
+
|
|
945
|
+
if (lineStartPos && lineEndPos) {
|
|
946
|
+
const rect = document.createElement('div');
|
|
947
|
+
rect.className = 'mobile-selection-rect';
|
|
948
|
+
rect.style.position = 'absolute';
|
|
949
|
+
rect.style.left = `${lineStartPos.x}px`;
|
|
950
|
+
rect.style.top = `${lineStartPos.y}px`;
|
|
951
|
+
rect.style.width = `${lineEndPos.x - lineStartPos.x + lineEndPos.width}px`;
|
|
952
|
+
rect.style.height = `${lineStartPos.height}px`;
|
|
953
|
+
this.selectionOverlay.appendChild(rect);
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
this.selectionOverlay.style.display = 'block';
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
/**
|
|
961
|
+
* Update selection handles positions
|
|
962
|
+
* @private
|
|
963
|
+
*/
|
|
964
|
+
_updateHandles(start, end) {
|
|
965
|
+
const isStartFirst = (start.y < end.y) || (start.y === end.y && start.x <= end.x);
|
|
966
|
+
|
|
967
|
+
const actualStart = isStartFirst ? start : end;
|
|
968
|
+
const actualEnd = isStartFirst ? end : start;
|
|
969
|
+
|
|
970
|
+
const startPos = this._terminalToScreenCoords(actualStart.x, actualStart.y);
|
|
971
|
+
if (startPos) {
|
|
972
|
+
this.startHandle.style.display = 'block';
|
|
973
|
+
this.startHandle.style.left = `${startPos.x}px`;
|
|
974
|
+
this.startHandle.style.top = `${startPos.y + startPos.height}px`;
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
const endPos = this._terminalToScreenCoords(actualEnd.x, actualEnd.y);
|
|
978
|
+
if (endPos) {
|
|
979
|
+
this.endHandle.style.display = 'block';
|
|
980
|
+
this.endHandle.style.left = `${endPos.x + endPos.width}px`;
|
|
981
|
+
this.endHandle.style.top = `${endPos.y + endPos.height}px`;
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
/**
|
|
986
|
+
* Show context menu at position
|
|
987
|
+
* @private
|
|
988
|
+
*/
|
|
989
|
+
_showContextMenu(x, y) {
|
|
990
|
+
if (!this.contextMenu) return;
|
|
991
|
+
|
|
992
|
+
// Position menu above the start handle
|
|
993
|
+
this._positionContextMenu();
|
|
994
|
+
|
|
995
|
+
// Pre-read clipboard for paste action
|
|
996
|
+
this._readClipboard();
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
/**
|
|
1000
|
+
* Position context menu above the start selection handle
|
|
1001
|
+
* @private
|
|
1002
|
+
*/
|
|
1003
|
+
_positionContextMenu() {
|
|
1004
|
+
if (!this.contextMenu || !this.selection.active) return;
|
|
1005
|
+
|
|
1006
|
+
// If user has manually positioned the menu, keep it there
|
|
1007
|
+
if (this.contextMenuUserPositioned) {
|
|
1008
|
+
this.contextMenu.style.display = 'block';
|
|
1009
|
+
return;
|
|
1010
|
+
}
|
|
1011
|
+
|
|
1012
|
+
// Get start handle position
|
|
1013
|
+
const startHandleRect = this.startHandle.getBoundingClientRect();
|
|
1014
|
+
const menuWidth = this.config.contextMenuWidth;
|
|
1015
|
+
const menuHeight = 50; // Approximate height for single row menu
|
|
1016
|
+
|
|
1017
|
+
// Position above the start handle
|
|
1018
|
+
let posX = startHandleRect.left + (startHandleRect.width / 2) - (menuWidth / 2);
|
|
1019
|
+
let posY = startHandleRect.top - menuHeight - 10; // 10px gap above handle
|
|
1020
|
+
|
|
1021
|
+
// Adjust position to stay within viewport
|
|
1022
|
+
if (posX + menuWidth > window.innerWidth) {
|
|
1023
|
+
posX = window.innerWidth - menuWidth - 10;
|
|
1024
|
+
}
|
|
1025
|
+
if (posX < 10) {
|
|
1026
|
+
posX = 10;
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
// If menu would go above viewport, position below start handle instead
|
|
1030
|
+
if (posY < 10) {
|
|
1031
|
+
posY = startHandleRect.bottom + 10;
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
this.contextMenu.style.left = `${posX}px`;
|
|
1035
|
+
this.contextMenu.style.top = `${posY}px`;
|
|
1036
|
+
this.contextMenu.style.display = 'block';
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
/**
|
|
1040
|
+
* Hide context menu
|
|
1041
|
+
*/
|
|
1042
|
+
hideContextMenu() {
|
|
1043
|
+
if (this.contextMenu) {
|
|
1044
|
+
this.contextMenu.style.display = 'none';
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
/**
|
|
1049
|
+
* Handle context menu action
|
|
1050
|
+
* @private
|
|
1051
|
+
*/
|
|
1052
|
+
async _handleMenuAction(action) {
|
|
1053
|
+
switch (action) {
|
|
1054
|
+
case 'copy':
|
|
1055
|
+
await this._copySelection();
|
|
1056
|
+
// Reset selecting mode after copy
|
|
1057
|
+
this.touchState.isSelecting = false;
|
|
1058
|
+
break;
|
|
1059
|
+
case 'paste':
|
|
1060
|
+
await this._pasteFromClipboard();
|
|
1061
|
+
// Reset selecting mode after paste
|
|
1062
|
+
this.touchState.isSelecting = false;
|
|
1063
|
+
break;
|
|
1064
|
+
case 'clear':
|
|
1065
|
+
this.clearSelection(); // Already resets isSelecting
|
|
1066
|
+
break;
|
|
1067
|
+
case 'selectAll':
|
|
1068
|
+
this._selectAll();
|
|
1069
|
+
break;
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
this.hideContextMenu();
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
/**
|
|
1076
|
+
* Copy selection to clipboard
|
|
1077
|
+
* @private
|
|
1078
|
+
*/
|
|
1079
|
+
async _copySelection() {
|
|
1080
|
+
if (!this.selection.text) return;
|
|
1081
|
+
|
|
1082
|
+
try {
|
|
1083
|
+
await navigator.clipboard.writeText(this.selection.text);
|
|
1084
|
+
this.app.showToast('Copied to clipboard', 'success');
|
|
1085
|
+
} catch (err) {
|
|
1086
|
+
console.error('[MobileTerminal] Failed to copy:', err);
|
|
1087
|
+
this.app.showToast('Failed to copy', 'error');
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
/**
|
|
1092
|
+
* Paste from clipboard
|
|
1093
|
+
* @private
|
|
1094
|
+
*/
|
|
1095
|
+
async _pasteFromClipboard() {
|
|
1096
|
+
try {
|
|
1097
|
+
const text = await navigator.clipboard.readText();
|
|
1098
|
+
this._pasteText(text);
|
|
1099
|
+
} catch (err) {
|
|
1100
|
+
console.error('[MobileTerminal] Failed to paste:', err);
|
|
1101
|
+
this.app.showToast('Failed to paste', 'error');
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
/**
|
|
1106
|
+
* Paste text into terminal
|
|
1107
|
+
* @private
|
|
1108
|
+
*/
|
|
1109
|
+
_pasteText(text) {
|
|
1110
|
+
if (!text) return;
|
|
1111
|
+
|
|
1112
|
+
// Send paste to server
|
|
1113
|
+
const session = this.app.sessions.get(this.sessionId);
|
|
1114
|
+
if (session && session.socket && session.connected) {
|
|
1115
|
+
session.socket.emit('terminal:input', {
|
|
1116
|
+
sessionId: this.sessionId,
|
|
1117
|
+
data: text
|
|
1118
|
+
});
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
this.app.showToast('Pasted', 'success');
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
/**
|
|
1125
|
+
* Read clipboard content (pre-read for context menu)
|
|
1126
|
+
* @private
|
|
1127
|
+
*/
|
|
1128
|
+
async _readClipboard() {
|
|
1129
|
+
try {
|
|
1130
|
+
this.app.terminalClipboardContent = await navigator.clipboard.readText();
|
|
1131
|
+
} catch (err) {
|
|
1132
|
+
// Clipboard read might fail due to permissions
|
|
1133
|
+
this.app.terminalClipboardContent = null;
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
|
|
1137
|
+
/**
|
|
1138
|
+
* Select all terminal content
|
|
1139
|
+
* @private
|
|
1140
|
+
*/
|
|
1141
|
+
_selectAll() {
|
|
1142
|
+
const buffer = this.terminal.buffer.active;
|
|
1143
|
+
const maxY = buffer.length - 1;
|
|
1144
|
+
|
|
1145
|
+
// Set selecting mode to prevent keyboard
|
|
1146
|
+
this.touchState.isSelecting = true;
|
|
1147
|
+
|
|
1148
|
+
this.selection.start = { x: 0, y: 0 };
|
|
1149
|
+
this.selection.end = { x: this.terminal.cols - 1, y: maxY };
|
|
1150
|
+
this.selection.active = true;
|
|
1151
|
+
|
|
1152
|
+
this._updateSelection();
|
|
1153
|
+
}
|
|
1154
|
+
|
|
1155
|
+
/**
|
|
1156
|
+
* Clear selection
|
|
1157
|
+
*/
|
|
1158
|
+
clearSelection() {
|
|
1159
|
+
this.selection.active = false;
|
|
1160
|
+
this.selection.start = null;
|
|
1161
|
+
this.selection.end = null;
|
|
1162
|
+
this.selection.text = '';
|
|
1163
|
+
|
|
1164
|
+
// Reset selecting mode to allow keyboard input
|
|
1165
|
+
this.touchState.isSelecting = false;
|
|
1166
|
+
|
|
1167
|
+
// Reset context menu positioning flag
|
|
1168
|
+
this.contextMenuUserPositioned = false;
|
|
1169
|
+
|
|
1170
|
+
this._updateSelection();
|
|
1171
|
+
|
|
1172
|
+
// Allow keyboard input again after selection is cleared
|
|
1173
|
+
this._enableKeyboardInput();
|
|
1174
|
+
}
|
|
1175
|
+
|
|
1176
|
+
/**
|
|
1177
|
+
* Refresh selection overlay positions
|
|
1178
|
+
* Called after font size changes or terminal dimension changes
|
|
1179
|
+
*/
|
|
1180
|
+
refreshSelection() {
|
|
1181
|
+
if (this.selection.active) {
|
|
1182
|
+
requestAnimationFrame(() => {
|
|
1183
|
+
if (this.selection.active) {
|
|
1184
|
+
this._updateSelection();
|
|
1185
|
+
}
|
|
1186
|
+
});
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
/**
|
|
1191
|
+
* Focus hidden textarea for keyboard input
|
|
1192
|
+
* @private
|
|
1193
|
+
*/
|
|
1194
|
+
_focusHiddenTextarea() {
|
|
1195
|
+
// Don't focus keyboard if we're in selection mode or selection is active
|
|
1196
|
+
if (this.touchState.isSelecting || this.selection.active) {
|
|
1197
|
+
return;
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1200
|
+
if (this.hiddenTextarea) {
|
|
1201
|
+
// Remove readonly to allow keyboard
|
|
1202
|
+
this.hiddenTextarea.removeAttribute('readonly');
|
|
1203
|
+
this.hiddenTextarea.focus();
|
|
1204
|
+
}
|
|
1205
|
+
}
|
|
1206
|
+
|
|
1207
|
+
/**
|
|
1208
|
+
* Collapse keyboard by blurring hidden textarea
|
|
1209
|
+
* @private
|
|
1210
|
+
*/
|
|
1211
|
+
_collapseKeyboard() {
|
|
1212
|
+
if (this.hiddenTextarea) {
|
|
1213
|
+
this.hiddenTextarea.blur();
|
|
1214
|
+
// Restore readonly to prevent keyboard on any accidental focus
|
|
1215
|
+
this.hiddenTextarea.setAttribute('readonly', 'true');
|
|
1216
|
+
// Also blur any active element that might have focus
|
|
1217
|
+
if (document.activeElement && document.activeElement !== document.body) {
|
|
1218
|
+
document.activeElement.blur();
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1222
|
+
|
|
1223
|
+
/**
|
|
1224
|
+
* Enable keyboard input by removing readonly from hidden textarea
|
|
1225
|
+
* @private
|
|
1226
|
+
*/
|
|
1227
|
+
_enableKeyboardInput() {
|
|
1228
|
+
if (this.hiddenTextarea) {
|
|
1229
|
+
this.hiddenTextarea.removeAttribute('readonly');
|
|
1230
|
+
}
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
/**
|
|
1234
|
+
* Send text to terminal
|
|
1235
|
+
* @private
|
|
1236
|
+
*/
|
|
1237
|
+
_sendToTerminal(text) {
|
|
1238
|
+
const session = this.app.sessions.get(this.sessionId);
|
|
1239
|
+
if (session && session.socket && session.connected && session.isController) {
|
|
1240
|
+
session.socket.emit('terminal:input', {
|
|
1241
|
+
sessionId: this.sessionId,
|
|
1242
|
+
data: text
|
|
1243
|
+
});
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1247
|
+
/**
|
|
1248
|
+
* Handle context menu event (right-click)
|
|
1249
|
+
* @private
|
|
1250
|
+
*/
|
|
1251
|
+
_handleContextMenu(e) {
|
|
1252
|
+
if (!this.app.isMobile) return;
|
|
1253
|
+
|
|
1254
|
+
e.preventDefault();
|
|
1255
|
+
|
|
1256
|
+
const coords = this._screenToTerminalCoords(e.clientX, e.clientY);
|
|
1257
|
+
if (coords) {
|
|
1258
|
+
// If no selection, select word at position
|
|
1259
|
+
if (!this.selection.active) {
|
|
1260
|
+
const wordRange = this._getWordAtPosition(coords.x, coords.y);
|
|
1261
|
+
if (wordRange) {
|
|
1262
|
+
this.selection.start = wordRange.start;
|
|
1263
|
+
this.selection.end = wordRange.end;
|
|
1264
|
+
this.selection.active = true;
|
|
1265
|
+
this._updateSelection();
|
|
1266
|
+
}
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1269
|
+
this._showContextMenu(e.clientX, e.clientY);
|
|
1270
|
+
}
|
|
1271
|
+
}
|
|
1272
|
+
|
|
1273
|
+
/**
|
|
1274
|
+
* Handle window resize
|
|
1275
|
+
* @private
|
|
1276
|
+
*/
|
|
1277
|
+
_handleResize() {
|
|
1278
|
+
if (this.selection.active) {
|
|
1279
|
+
requestAnimationFrame(() => {
|
|
1280
|
+
if (this.selection.active) {
|
|
1281
|
+
this._updateSelection();
|
|
1282
|
+
}
|
|
1283
|
+
});
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
|
|
1287
|
+
/**
|
|
1288
|
+
* Handle visual viewport changes (keyboard show/hide)
|
|
1289
|
+
* @private
|
|
1290
|
+
*/
|
|
1291
|
+
_handleVisualViewport() {
|
|
1292
|
+
if (!window.visualViewport) return;
|
|
1293
|
+
|
|
1294
|
+
const viewportHeight = window.visualViewport.height;
|
|
1295
|
+
const windowHeight = window.innerHeight;
|
|
1296
|
+
|
|
1297
|
+
// Detect keyboard visibility
|
|
1298
|
+
const keyboardHeight = windowHeight - viewportHeight;
|
|
1299
|
+
const wasKeyboardVisible = this.viewport.isKeyboardVisible;
|
|
1300
|
+
this.viewport.isKeyboardVisible = keyboardHeight > 100; // Threshold for keyboard detection
|
|
1301
|
+
this.viewport.keyboardHeight = keyboardHeight;
|
|
1302
|
+
|
|
1303
|
+
// Adjust terminal container if keyboard is visible
|
|
1304
|
+
if (this.viewport.isKeyboardVisible !== wasKeyboardVisible) {
|
|
1305
|
+
this._adjustForKeyboard();
|
|
1306
|
+
}
|
|
1307
|
+
}
|
|
1308
|
+
|
|
1309
|
+
/**
|
|
1310
|
+
* Adjust terminal for virtual keyboard
|
|
1311
|
+
* @private
|
|
1312
|
+
*/
|
|
1313
|
+
_adjustForKeyboard() {
|
|
1314
|
+
if (!this.terminalElement) return;
|
|
1315
|
+
|
|
1316
|
+
if (this.viewport.isKeyboardVisible) {
|
|
1317
|
+
// Keyboard is visible - adjust viewport
|
|
1318
|
+
const availableHeight = window.visualViewport.height;
|
|
1319
|
+
this.terminalElement.style.height = `${availableHeight}px`;
|
|
1320
|
+
|
|
1321
|
+
// Scroll terminal into view
|
|
1322
|
+
this.terminalElement.scrollIntoView({ behavior: 'smooth', block: 'start' });
|
|
1323
|
+
} else {
|
|
1324
|
+
// Keyboard is hidden - restore original height
|
|
1325
|
+
this.terminalElement.style.height = '';
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1328
|
+
// Refit terminal
|
|
1329
|
+
if (this.session.fitAddon) {
|
|
1330
|
+
requestAnimationFrame(() => {
|
|
1331
|
+
try {
|
|
1332
|
+
this.session.fitAddon.fit();
|
|
1333
|
+
} catch (err) {
|
|
1334
|
+
console.warn('[MobileTerminal] Failed to fit terminal:', err);
|
|
1335
|
+
}
|
|
1336
|
+
});
|
|
1337
|
+
}
|
|
1338
|
+
}
|
|
1339
|
+
|
|
1340
|
+
/**
|
|
1341
|
+
* Handle keydown events
|
|
1342
|
+
* @private
|
|
1343
|
+
*/
|
|
1344
|
+
_handleKeyDown(e) {
|
|
1345
|
+
// Handle keyboard shortcuts
|
|
1346
|
+
if (e.ctrlKey || e.metaKey) {
|
|
1347
|
+
switch (e.key.toLowerCase()) {
|
|
1348
|
+
case 'c':
|
|
1349
|
+
if (this.selection.active) {
|
|
1350
|
+
e.preventDefault();
|
|
1351
|
+
this._copySelection();
|
|
1352
|
+
}
|
|
1353
|
+
break;
|
|
1354
|
+
case 'v':
|
|
1355
|
+
e.preventDefault();
|
|
1356
|
+
this._pasteFromClipboard();
|
|
1357
|
+
break;
|
|
1358
|
+
case 'a':
|
|
1359
|
+
e.preventDefault();
|
|
1360
|
+
this._selectAll();
|
|
1361
|
+
break;
|
|
1362
|
+
}
|
|
1363
|
+
}
|
|
1364
|
+
}
|
|
1365
|
+
}
|
|
1366
|
+
|
|
1367
|
+
// Export for use in app.js
|
|
1368
|
+
if (typeof window !== 'undefined') {
|
|
1369
|
+
window.MobileTerminalHandler = MobileTerminalHandler;
|
|
1370
|
+
}
|