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
// ── Mobile Input Bar ─────────────────────────────────────────────────
//
// Bottom bar with control-key ribbon + text input.
// Replaces direct xterm.js textarea interaction on mobile.
//
// - Ribbon buttons send control chars / escape sequences directly to PTY
// - Text input: native keyboard with autocomplete/voice. Enter sends + clears.
// - Bar appears on tap, hides when keyboard dismisses.
export function createInputBar(term, send) {
const bar = document.getElementById('inputBar');
const ribbon = document.getElementById('inputRibbon');
const input = document.getElementById('inputText');
const sendBtn = document.getElementById('inputSend');
// Complete no-op shape: callers invoke .show()/.hide(), so a partial stub
// would throw. Mirror the real public API below.
if (!bar || !input) return { show() {}, hide() {}, destroy() {} };
// ── Disable xterm.js textarea on mobile ───────────────────────────
// We own input now. Prevent xterm's textarea from stealing focus.
const textarea = term.textarea;
if (textarea) {
textarea.setAttribute('tabindex', '-1');
textarea.style.pointerEvents = 'none';
textarea.style.opacity = '0';
textarea.style.position = 'fixed';
textarea.style.top = '-9999px';
}
// ── Parse escape sequences from data-key attributes ───────────────
function parseKey(raw) {
return raw.replace(/\\x([0-9a-fA-F]{2})/g, (_, h) => String.fromCharCode(parseInt(h, 16)))
.replace(/\\t/g, '\t')
.replace(/\\n/g, '\n')
.replace(/\\r/g, '\r');
}
// ── Show/hide bar ─────────────────────────────────────────────────
// The bar is now a flex item (see style.css), so `.hidden` toggles
// `display: none`. Showing/hiding the bar resizes the flex children
// (#terminal / #reader); fire a synchronous resize so terminal-core
// and reader-view recompute their bounds in the same task.
function show() {
bar.classList.remove('hidden');
resizeTerminal();
}
function hide() {
bar.classList.add('hidden');
// terminal.js owns body.style.height tracking (renderer-agnostic
// visualViewport handler). It will clear the inline height the
// next time the viewport grows back; we don't touch it here so a
// hide() while the keyboard is still up doesn't cause body to snap
// to 100vh and re-cover the keyboard space.
input.blur();
resizeTerminal();
}
function computeKeyboardOffset(innerHeight, vvHeight, vvOffsetTop) {
return Math.max(0, innerHeight - vvHeight - vvOffsetTop);
}
function resizeTerminal() {
// Notify synchronously so layout-dependent consumers (terminal-core
// resize, reader-view re-pin) read the freshly-shrunk host height
// in the same task — no visible jump on the next frame.
window.dispatchEvent(new Event('resize'));
}
// ── Ribbon: send control chars directly to PTY ────────────────────
ribbon.addEventListener('click', (e) => {
const btn = e.target.closest('button[data-key]');
if (!btn) return;
e.preventDefault();
const seq = parseKey(btn.dataset.key);
send(seq);
// Keep focus on input so keyboard stays up
input.focus();
});
// Prevent ribbon buttons from stealing focus, but allow scroll
ribbon.addEventListener('mousedown', (e) => {
if (e.target.closest('button')) e.preventDefault();
});
// Don't preventDefault touchstart — it kills ribbon scrolling.
// Instead, prevent focus steal via mousedown only.
// ── Text input: two send modes ────────────────────────────────────
// Keyboard Enter: send text + \r (execute in shell)
// Green button: send text WITHOUT \r (inject into readline, still editable)
function sendAndExecute() {
const text = input.value;
if (text) send(text);
send('\r');
input.value = '';
}
function sendWithoutEnter() {
const text = input.value;
if (text) send(text);
input.value = '';
input.focus();
}
input.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
e.preventDefault();
sendAndExecute();
}
});
sendBtn.addEventListener('click', (e) => {
e.preventDefault();
sendWithoutEnter();
input.focus();
});
// ── Activate on touch/tap overlay ─────────────────────────────────
// Double-tap on terminal area shows the input bar
const overlay = document.getElementById('touchOverlay');
function activateInput() {
show();
// Small delay so the bar renders before focusing (avoids layout jump)
setTimeout(() => input.focus(), 50);
}
// ── Auto-hide bar when the keyboard dismisses ─────────────────────
// Body-height tracking (i.e. shrinking the layout to match
// visualViewport.height when the soft keyboard opens) is owned by
// terminal.js's renderer-agnostic visualViewport handler — it must
// work whether or not the input bar is mounted. This listener only
// handles bar UX: when the keyboard dismisses (viewport grows back
// by > 50px), tuck the bar away too so the user gets terminal-full
// space back.
if (window.visualViewport) {
const vv = window.visualViewport;
let lastHeight = vv.height;
const onViewportChange = () => {
const h = vv.height;
if (h > lastHeight + 50 && !bar.classList.contains('hidden')) {
hide();
}
lastHeight = h;
};
vv.addEventListener('resize', onViewportChange);
vv.addEventListener('scroll', onViewportChange);
}
// Also hide on Escape
input.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
e.preventDefault();
hide();
}
});
// ── Visible failure feedback ──────────────────────────────────────
// The old `.rec-error` tint was near-invisible and the attach path gave
// no UI feedback at all. Show a brief, clearly visible state on the
// relevant button plus a short, accessible message in the input bar.
const toast = document.getElementById('inputToast');
let toastTimer = null;
function showError(msg, btn) {
if (toast) {
toast.textContent = msg;
toast.classList.remove('hidden');
if (toastTimer) clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.add('hidden'), 4000);
}
if (btn) {
btn.classList.add('rec-error');
setTimeout(() => btn.classList.remove('rec-error'), 1500);
}
}
// ── Shared upload helper ──────────────────────────────────────────
// POSTs a File/Blob to /api/upload and drops the returned path into
// the terminal via send(). Used by both the attach button and the
// audio record button.
async function uploadFile(file) {
const form = new FormData();
form.append('file', file);
// Upload to whichever host drives the terminal: the returned path is only
// meaningful on that host's filesystem, so it must go through the relay.
const res = await window.MobuxMesh.apiFetch('/api/upload', { method: 'POST', body: form });
if (!res.ok) throw new Error(await res.text());
const { path } = await res.json();
// Send path directly to terminal, ready to use
send(path);
}
// ── File attach (any file type) ───────────────────────────────────
const uploadBtn = document.getElementById('uploadBtn');
const fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.accept = '*/*';
fileInput.style.display = 'none';
document.body.appendChild(fileInput);
if (uploadBtn) {
uploadBtn.addEventListener('click', (e) => {
e.preventDefault();
fileInput.click();
});
// Prevent focus steal
uploadBtn.addEventListener('mousedown', (e) => e.preventDefault());
}
fileInput.addEventListener('change', async () => {
const file = fileInput.files?.[0];
if (!file) return;
try {
await uploadFile(file);
} catch (err) {
console.error('Upload failed:', err);
showError('Attach failed: upload error', uploadBtn);
}
// Reset so the same file can be re-selected
fileInput.value = '';
});
// ── Audio record (MediaRecorder → /api/upload) ────────────────────
const recBtn = document.getElementById('recBtn');
let mediaRecorder = null;
let chunks = [];
let recStream = null;
// Synchronous guard: `mediaRecorder` stays null across the `await
// getUserMedia(...)`, so without this two quick taps would each start a
// stream and leak the first. Set true at the top of startRecording, before
// the await; cleared by recCleanup.
let recBusy = false;
function recCleanup() {
if (recStream) {
recStream.getTracks().forEach((t) => t.stop());
recStream = null;
}
mediaRecorder = null;
recBusy = false;
if (recBtn) recBtn.classList.remove('recording');
}
function extForMime(mime) {
if (!mime) return 'webm';
if (mime.includes('mp4')) return 'm4a';
if (mime.includes('ogg')) return 'ogg';
if (mime.includes('webm')) return 'webm';
return 'webm';
}
function pickMimeType() {
const prefs = ['audio/webm;codecs=opus', 'audio/webm', 'audio/mp4'];
for (const t of prefs) {
if (MediaRecorder.isTypeSupported(t)) return t;
}
return ''; // let the browser pick a default
}
async function startRecording() {
// Set the guard synchronously, before any await, so a second tap during
// the getUserMedia round-trip is a no-op (the click handler also checks).
recBusy = true;
try {
recStream = await navigator.mediaDevices.getUserMedia({ audio: true });
} catch (err) {
console.error('Microphone access failed:', err);
recCleanup();
showError('Mic access denied. In the TWA app, allow the Microphone permission.', recBtn);
return;
}
chunks = [];
const mimeType = pickMimeType();
try {
mediaRecorder = mimeType
? new MediaRecorder(recStream, { mimeType })
: new MediaRecorder(recStream);
} catch (err) {
console.error('MediaRecorder init failed:', err);
recCleanup();
showError('Could not start recording on this device.', recBtn);
return;
}
mediaRecorder.addEventListener('dataavailable', (e) => {
if (e.data && e.data.size > 0) chunks.push(e.data);
});
mediaRecorder.addEventListener('stop', async () => {
const type = mediaRecorder?.mimeType || 'audio/webm';
const ext = extForMime(type);
const blob = new Blob(chunks, { type });
chunks = [];
// Release the mic before the upload round-trip.
recCleanup();
if (blob.size === 0) return;
const file = new File([blob], 'recording-' + Date.now() + '.' + ext, { type });
try {
await uploadFile(file);
} catch (err) {
console.error('Upload failed:', err);
showError('Recording upload failed.', recBtn);
}
});
mediaRecorder.start();
if (recBtn) recBtn.classList.add('recording');
}
function stopRecording() {
if (mediaRecorder && mediaRecorder.state !== 'inactive') {
mediaRecorder.stop(); // triggers onstop → upload + cleanup
} else {
recCleanup();
}
}
if (recBtn) {
const recSupported =
!!navigator.mediaDevices?.getUserMedia && typeof MediaRecorder !== 'undefined';
if (!recSupported) {
recBtn.style.display = 'none';
} else {
recBtn.addEventListener('click', (e) => {
e.preventDefault();
if (mediaRecorder && mediaRecorder.state === 'recording') {
stopRecording();
} else if (!recBusy) {
// `recBusy` is set synchronously inside startRecording before the
// getUserMedia await; guarding here too means a rapid double-tap
// can't kick off a second stream while the first is still starting.
startRecording();
}
});
// Prevent focus steal
recBtn.addEventListener('mousedown', (e) => e.preventDefault());
}
}
// ── Public API ────────────────────────────────────────────────────
return {
_computeKeyboardOffset: computeKeyboardOffset,
show: activateInput,
hide,
destroy() {
if (textarea) {
textarea.removeAttribute('tabindex');
textarea.style.pointerEvents = '';
}
}
};
}