-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmobile-controller.js
More file actions
540 lines (503 loc) · 26.1 KB
/
Copy pathmobile-controller.js
File metadata and controls
540 lines (503 loc) · 26.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
// ttyd Mobile Controller — v2
// Fixed: CSS class/id mismatch, enter sends, no-zoom, always-visible arrows, 1-finger scroll, no ~ spam.
(function () {
'use strict';
const SEND_DELAY_MS = 200;
const isMobile = () => window.matchMedia('(pointer: coarse)').matches;
const boot = new MutationObserver((_, obs) => {
const container = document.querySelector('#terminal-container');
if (container && (window.term || window.socket)) {
obs.disconnect();
initClipboard(container);
initComposition();
initShiftEnter();
if (isMobile()) initMobile(container);
}
});
boot.observe(document.body, { childList: true, subtree: true });
// ── Core send ──────────────────────────────────────────────────────────────
function sendData(seq) {
const enc = new TextEncoder();
if (window.socket?.readyState === 1) window.socket.send(enc.encode(seq));
else if (window.term?.input) window.term.input(seq);
}
// ── Clipboard ──────────────────────────────────────────────────────────────
// tmux has set-clipboard on → sends OSC 52 on copy. xterm.js's default
// handler calls writeText outside a gesture frame (silent fail). We register
// our own handler via term.parser (proposed API, enabled by ttyd's
// allowProposedApi:true) to intercept OSC 52 and write to clipboard.
function initClipboard(container) {
// Right-click → browser's native context menu. With tmux mouse on, xterm.js
// forwards the right button to the app and suppresses the menu; capture
// before xterm.js (don't preventDefault) so the browser's own menu shows.
container.addEventListener('mousedown', e => {
if (e.button === 2) e.stopImmediatePropagation();
}, { capture: true });
container.addEventListener('contextmenu', e => {
e.stopImmediatePropagation();
}, { capture: true });
const setup = () => {
if (!window.term?.parser) { setTimeout(setup, 200); return; }
try {
window.term.parser.registerOscHandler(52, (data) => {
// data = "Pc;Pd" e.g. "c;SGVsbG8="
const semi = data.indexOf(';');
if (semi < 0) return false;
const b64 = data.slice(semi + 1);
if (!b64 || b64 === '?') return false;
try {
// atob yields one char per BYTE — decoding as UTF-8 keeps
// multibyte chars (é, 轮) intact instead of latin-1 mojibake.
const bytes = Uint8Array.from(atob(b64), c => c.charCodeAt(0));
const text = new TextDecoder().decode(bytes);
navigator.clipboard.writeText(text).catch(() => {});
} catch {}
return false; // let xterm also handle it
});
} catch (e) {
console.error('[clip] registerOscHandler unavailable:', e);
}
};
setup();
}
// ── Dead-key composition fix (xterm.js #2661) ──────────────────────────────
// Gecko delivers real keyCodes (not 229) for keydowns during dead-key
// composition (mac option+e, e → é). xterm's CompositionHelper force-finalizes
// the composition on any non-229 keydown, sending the placeholder accent ("´")
// in addition to the composed char — the shell sees "´é". Swallow those
// keydowns; the composition events already deliver the final text. Also swallow
// 'Dead' keydowns so xterm's _unprocessedDeadKey latch can't eat the next
// regular keystroke. Chromium/WebKit send keyCode 229 here, so they take the
// same code path as before.
function initComposition() {
const setup = () => {
if (!window.term?.attachCustomKeyEventHandler) { setTimeout(setup, 200); return; }
window.term.attachCustomKeyEventHandler((e) => {
if (e.type !== 'keydown') return true;
if (e.key === 'Dead') return false;
if (e.isComposing && e.keyCode !== 229) return false;
return true;
});
};
setup();
}
// ── Shift+Enter → Ctrl+J (chat:newline) without submitting ────────────────
// Capture phase runs before xterm.js; stopImmediatePropagation prevents
// xterm.js from also processing Enter and sending \r (which would submit).
function initShiftEnter() {
document.addEventListener('keydown', (e) => {
if (e.key === 'Enter' && e.shiftKey) {
e.preventDefault();
e.stopImmediatePropagation();
sendData('\x0a');
}
}, true);
}
// ── Mobile UI ──────────────────────────────────────────────────────────────
function initMobile(container) {
// Prevent iOS auto-zoom on input focus (triggered when font-size < 16px)
let vp = document.querySelector('meta[name=viewport]');
if (!vp) { vp = document.createElement('meta'); vp.name = 'viewport'; document.head.appendChild(vp); }
vp.content = 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no';
injectCSS();
const bar = mk('div', 'mb-bar');
const keyRow = mk('div', 'mb-keys');
const inpRow = mk('div', 'mb-input-row');
const arrows = mk('div', 'mb-arrows');
const tmux = mk('div', 'mb-tmux');
bar.append(keyRow, inpRow);
document.body.append(bar, arrows, tmux);
// ── Modifier state ────────────────────────────────────────────────────
let ctrlOn = false, shiftOn = false, tmuxMode = false;
function clearCtrl() { ctrlOn = false; kEl.ctrl.classList.remove('mod-on'); }
function clearShift() { shiftOn = false; kEl.shft.classList.remove('mod-on'); }
function clearTmux() { tmuxMode = false; kEl.cb.classList.remove('mod-on'); tmux.classList.remove('show'); }
function vib(ms) { if (navigator.vibrate) navigator.vibrate(ms); }
// ── Key row definitions ───────────────────────────────────────────────
// ↵ sends the textarea content (same as Send button) — not just a newline.
// CTRL enters combo mode: next Send transmits ctrl+char.
// SHF toggles shift for Tab and arrow keys.
// ↕ toggles the arrow popup.
const KEYS = [
{ id: 'esc', label: 'Esc', seq: '\x1b' },
{ id: 'cc', label: 'C-c', seq: '\x03', cls: 'danger' },
{
id: 'cb', label: 'C-b', cls: 'accent',
fn: () => {
if (tmuxMode) { clearTmux(); vib(10); return; }
sendData('\x02');
tmuxMode = true;
kEl.cb.classList.add('mod-on');
positionTmux();
tmux.classList.add('show');
vib(15); inp.focus();
}
},
{
id: 'tab', label: 'Tab',
fn: () => { sendData(shiftOn ? '\x1b[Z' : '\t'); clearShift(); vib(10); }
},
{
id: 'ctrl', label: 'CTRL', cls: 'mod',
fn: () => {
ctrlOn = !ctrlOn;
if (ctrlOn) clearShift();
kEl.ctrl.classList.toggle('mod-on', ctrlOn);
vib(15); inp.focus();
}
},
{
id: 'shft', label: 'SHF', cls: 'mod',
fn: () => {
shiftOn = !shiftOn;
if (shiftOn) clearCtrl();
kEl.shft.classList.toggle('mod-on', shiftOn);
vib(15);
}
},
{ id: 'arr', label: '↕', cls: 'mod', fn: toggleArrows },
];
const kEl = {};
KEYS.forEach(k => {
const b = mk('div', `mb-key${k.cls ? ' ' + k.cls : ''}`);
b.textContent = k.label;
b._key = k;
kEl[k.id] = b;
keyRow.appendChild(b);
});
// ── Textarea ──────────────────────────────────────────────────────────
const inp = document.createElement('textarea');
inp.id = 'mb-inp';
inp.rows = 1;
inp.placeholder = '';
inp.setAttribute('autocomplete', 'off');
inp.setAttribute('autocorrect', 'off');
inp.setAttribute('autocapitalize','none');
inp.setAttribute('spellcheck', 'false');
inp.setAttribute('inputmode', 'text')
inp.setAttribute('enterkeyhint', 'send');
inp.addEventListener('keydown', e => e.stopPropagation());
inp.addEventListener('input', () => {
inp.style.height = 'auto';
inp.style.height = Math.min(inp.scrollHeight, 100) + 'px';
});
const sendBtn = mk('button', 'mb-send');
sendBtn.textContent = '↵';
inpRow.append(inp, sendBtn);
// ── Arrow popup ───────────────────────────────────────────────────────
buildArrows(arrows);
// ── Send logic ────────────────────────────────────────────────────────
const doSend = () => {
// Reload to reconnect only when truly disconnected.
// window.socket may be null if ttyd stores it elsewhere; sendData
// falls back to window.term.input which works regardless.
const connected = window.socket?.readyState === 1;
const canSend = connected || !!window.term?.input;
if (!canSend) {
location.reload();
return;
}
const text = inp.value;
if (ctrlOn) {
const c = text.trim().charAt(0).toUpperCase();
if (c >= 'A' && c <= 'Z') sendData(String.fromCharCode(c.charCodeAt(0) - 64));
clearCtrl(); inp.value = ''; inp.style.height = 'auto'; vib(10);
return;
}
if (tmuxMode) {
// Send tmux command key(s) without trailing \r
if (text) sendData(text);
clearTmux(); inp.value = ''; inp.style.height = 'auto'; vib(10);
return;
}
if (text) {
sendData(text);
setTimeout(() => sendData('\r'), SEND_DELAY_MS);
} else {
sendData('\r');
}
inp.value = ''; inp.style.height = 'auto'; vib(20);
};
sendBtn.addEventListener('touchend', e => { e.preventDefault(); doSend(); });
sendBtn.addEventListener('click', doSend);
// ── Touch: single-finger scrolls terminal (both 1 and 2 finger) ─────────
let touchY = null;
const getTouchY = (e) => e.touches.length === 2
? (e.touches[0].clientY + e.touches[1].clientY) / 2
: e.touches[0].clientY;
container.addEventListener('touchstart', e => {
if (e.target.closest('.mb-bar, .mb-arrows')) return;
e.preventDefault();
e.stopPropagation();
touchY = getTouchY(e);
}, { passive: false, capture: true });
container.addEventListener('touchmove', e => {
e.preventDefault();
e.stopPropagation();
if (touchY === null) return;
const y = getTouchY(e);
const delta = touchY - y;
const lines = Math.round(delta / 20);
if (lines) {
const isAltScreen = window.term?.buffer?.active?.type === 'alternate';
if (isAltScreen) {
const seq = delta > 0 ? '\x1b[6~' : '\x1b[5~';
for (let i = 0; i < Math.abs(lines); i++) sendData(seq);
} else if (window.term?.scrollLines) {
window.term.scrollLines(lines);
}
touchY = y;
}
}, { passive: false, capture: true });
container.addEventListener('touchend', () => { touchY = null; }, { capture: true });
// ── Key row touch ─────────────────────────────────────────────────────
let activeKey = null;
keyRow.addEventListener('touchstart', e => {
const b = e.target.closest('.mb-key');
if (!b) return; e.preventDefault();
activeKey = b; b.classList.add('tap');
}, { passive: false });
keyRow.addEventListener('touchend', e => {
e.preventDefault();
if (!activeKey) return;
activeKey.classList.remove('tap');
const k = activeKey._key;
if (k.fn) k.fn(); else if (k.seq) { sendData(k.seq); vib(10); }
activeKey = null;
}, { passive: false });
keyRow.addEventListener('touchcancel', () => {
if (activeKey) { activeKey.classList.remove('tap'); activeKey = null; }
});
// ── Arrow panel touch ─────────────────────────────────────────────────
let activeArrow = null;
arrows.addEventListener('touchstart', e => {
const b = e.target.closest('.mb-key:not(.empty)');
if (!b) return; e.preventDefault();
activeArrow = b; b.classList.add('tap');
}, { passive: false });
arrows.addEventListener('touchend', e => {
e.preventDefault();
if (!activeArrow) return;
activeArrow.classList.remove('tap');
if (activeArrow.dataset.action === 'close') {
arrows.classList.remove('show');
} else {
const seq = activeArrow._getSeq ? activeArrow._getSeq() : activeArrow.dataset.seq;
if (seq) {
sendData(seq);
if (activeArrow._getSeq && shiftOn) clearShift();
vib(10);
}
}
activeArrow = null;
}, { passive: false });
arrows.addEventListener('touchcancel', () => {
if (activeArrow) { activeArrow.classList.remove('tap'); activeArrow = null; }
});
// ── Tmux panel touch ──────────────────────────────────────────────────
let activeTmux = null;
tmux.addEventListener('touchstart', e => {
const b = e.target.closest('.mb-key:not(.empty)');
if (!b) return; e.preventDefault();
activeTmux = b; b.classList.add('tap');
}, { passive: false });
tmux.addEventListener('touchend', e => {
e.preventDefault();
if (!activeTmux) return;
activeTmux.classList.remove('tap');
if (activeTmux.dataset.action === 'close') {
clearTmux();
} else {
const seq = activeTmux.dataset.seq;
if (seq) { sendData(seq); vib(10); }
clearTmux();
}
activeTmux = null;
}, { passive: false });
tmux.addEventListener('touchcancel', () => {
if (activeTmux) { activeTmux.classList.remove('tap'); activeTmux = null; }
});
buildTmux(tmux);
// ── Visual viewport: stay above keyboard, shrink terminal to match ───────
const updateLayout = () => {
const vv = window.visualViewport;
const kbH = vv ? Math.max(0, window.innerHeight - vv.offsetTop - vv.height) : 0;
bar.style.bottom = kbH + 'px';
const barH = bar.offsetHeight;
document.documentElement.style.setProperty('--mb-bar-h', (barH + kbH) + 'px');
// Explicitly set terminal height so xterm.js reflows when keyboard appears/disappears.
// Use setProperty('important') to override ttyd's own !important height rules.
const tc = document.querySelector('#terminal-container');
if (tc && vv) tc.style.setProperty('height', (vv.height - barH) + 'px', 'important');
// Double-RAF: let layout settle before xterm.js measures container dimensions
requestAnimationFrame(() => requestAnimationFrame(() => window.dispatchEvent(new Event('resize'))));
if (arrows.classList.contains('show')) positionArrows();
if (tmux.classList.contains('show')) positionTmux();
};
if (window.visualViewport) {
window.visualViewport.addEventListener('resize', updateLayout);
window.visualViewport.addEventListener('scroll', updateLayout);
}
new ResizeObserver(updateLayout).observe(bar);
requestAnimationFrame(updateLayout);
// ── Keep input focused ─────────────────────────────────────────────────
inp.addEventListener('blur', () => {
setTimeout(() => {
if (document.activeElement !== inp && document.activeElement !== sendBtn) inp.focus();
}, 80);
});
setTimeout(() => inp.focus(), 600);
// ── Arrow popup helpers ───────────────────────────────────────────────
function toggleArrows() {
const show = !arrows.classList.contains('show');
if (show) positionArrows();
arrows.classList.toggle('show', show);
vib(15);
}
function positionArrows() {
const r = kEl.arr.getBoundingClientRect();
const br = bar.getBoundingClientRect();
arrows.style.left = Math.min(r.left, window.innerWidth - 160) + 'px';
arrows.style.bottom = (window.innerHeight - br.top + 6) + 'px';
}
// ── Tmux popup helpers ────────────────────────────────────────────────
function positionTmux() {
const r = kEl.cb.getBoundingClientRect();
const br = bar.getBoundingClientRect();
const w = 4 * 46 + 3 * 4 + 12; // 4 cols × 46px + gaps + padding ≈ 208px
tmux.style.left = Math.min(r.left, window.innerWidth - w) + 'px';
tmux.style.bottom = (window.innerHeight - br.top + 6) + 'px';
}
// ── Build tmux panel ──────────────────────────────────────────────────
function buildTmux(panel) {
// Common tmux bindings: label, char sent after prefix
const CMDS = [
{ t: 'c', s: 'c', tip: 'new win' },
{ t: 'n', s: 'n', tip: 'next win' },
{ t: 'p', s: 'p', tip: 'prev win' },
{ t: '%', s: '%', tip: 'vsplit' },
{ t: '"', s: '"', tip: 'hsplit' },
{ t: 'z', s: 'z', tip: 'zoom' },
{ t: 'o', s: 'o', tip: 'next pane' },
{ t: '[', s: '[', tip: 'copy mode' },
{ t: 'd', s: 'd', tip: 'detach' },
{ t: 'x', s: 'x', tip: 'kill pane' },
{ t: ',', s: ',', tip: 'rename' },
{ t: '✕', action: 'close' },
];
const grid = mk('div', 'tmux-grid');
CMDS.forEach(k => {
const b = mk('div', 'mb-key');
b.textContent = k.t;
if (k.action) b.dataset.action = k.action;
else b.dataset.seq = k.s;
if (k.tip) b.title = k.tip;
grid.appendChild(b);
});
panel.appendChild(grid);
}
// ── Build arrow panel ─────────────────────────────────────────────────
function buildArrows(panel) {
const DEFS = [
null,
{ t: '▲', s: '\x1b[A', ss: '\x1b[1;2A' },
null,
{ t: '◀', s: '\x1b[D', ss: '\x1b[1;2D' },
{ t: '▼', s: '\x1b[B', ss: '\x1b[1;2B' },
{ t: '▶', s: '\x1b[C', ss: '\x1b[1;2C' },
{ t: '⇞', s: '\x1b[5~' },
{ t: '⇟', s: '\x1b[6~' },
{ t: '✕', action: 'close' },
];
const grid = mk('div', 'arrow-grid');
DEFS.forEach(k => {
const b = mk('div', k ? 'mb-key' : 'mb-key empty');
if (k) {
b.textContent = k.t;
if (k.action) b.dataset.action = k.action;
else if (k.ss) b._getSeq = () => shiftOn ? k.ss : k.s;
else if (k.s) b.dataset.seq = k.s;
}
grid.appendChild(b);
});
panel.appendChild(grid);
}
}
// ── Shared helpers ─────────────────────────────────────────────────────────
function mk(tag, cls) {
const e = document.createElement(tag);
if (cls) e.className = cls;
return e;
}
function injectCSS() {
const s = document.createElement('style');
s.textContent = `
.mb-bar {
position: fixed; left: 0; right: 0; bottom: 0;
z-index: 2147483647;
background: rgba(13,13,18,0.97);
border-top: 1px solid rgba(255,255,255,0.1);
backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px);
padding: 4px;
padding-bottom: calc(4px + env(safe-area-inset-bottom, 0px));
display: flex; flex-direction: column; gap: 3px;
touch-action: none; user-select: none; -webkit-user-select: none;
}
.mb-keys { display: flex; gap: 3px; }
.mb-input-row { display: flex; gap: 3px; align-items: flex-end; }
.mb-key {
flex: 1; height: 34px; min-width: 0;
background: rgba(255,255,255,0.08); color: #ccc;
border-radius: 6px; border: 1px solid rgba(255,255,255,0.06);
font: 700 11px/1 monospace;
display: flex; align-items: center; justify-content: center;
white-space: nowrap; overflow: hidden;
}
.mb-key.danger { background: rgba(200,50,50,0.28); color: #f99; border-color: rgba(200,50,50,0.3); }
.mb-key.accent { background: rgba(30,100,220,0.28); color: #aad; border-color: rgba(30,100,220,0.3); }
.mb-key.mod { background: rgba(55,55,75,0.6); color: #99b; }
.mb-key.mod-on { background: rgba(255,165,0,0.38) !important; color: #ffd !important; border-color: rgba(255,165,0,0.55) !important; }
.mb-key.tap { background: rgba(255,255,255,0.25) !important; transform: scale(0.91); }
.mb-key.empty { background: transparent !important; border-color: transparent !important; pointer-events: none; }
#mb-inp {
flex: 1; min-height: 34px; max-height: 100px;
background: rgba(22,22,32,0.98); color: #e0e0e0;
border: 1px solid rgba(255,255,255,0.14); border-radius: 6px;
font: 16px/1.4 monospace; padding: 7px 9px;
resize: none; outline: none; touch-action: auto;
}
#mb-inp:focus { border-color: #2a8; }
.mb-send {
width: 42px; min-height: 34px; flex-shrink: 0;
background: #0a7; color: #fff; border: none; border-radius: 6px;
font-size: 15px; cursor: pointer;
display: flex; align-items: center; justify-content: center;
}
.mb-send:active { background: #0c8; }
.mb-arrows, .mb-tmux {
display: none; position: fixed; z-index: 2147483646;
background: rgba(13,13,18,0.97);
border: 1px solid rgba(255,255,255,0.12); border-radius: 10px;
padding: 6px;
backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px);
touch-action: none; user-select: none; -webkit-user-select: none;
}
.mb-arrows.show, .mb-tmux.show { display: block; }
.arrow-grid {
display: grid;
grid-template-columns: repeat(3, 42px);
grid-template-rows: repeat(3, 42px);
gap: 4px;
}
.arrow-grid .mb-key { flex: none; width: 42px; height: 42px; font-size: 17px; }
.tmux-grid {
display: grid;
grid-template-columns: repeat(4, 46px);
gap: 4px;
}
.tmux-grid .mb-key { flex: none; width: 46px; height: 40px; font-size: 15px; }
#terminal-container { bottom: var(--mb-bar-h, 0px) !important; }
`;
document.head.appendChild(s);
}
})();