fix(client): fix xterm container overflow and IME ghost chars and add Passkey UI

This commit is contained in:
tyuXX 2026-11-26 22:08:25 +00:00
parent 874227e3da
commit 9040c426e8
8 changed files with 571 additions and 14 deletions

View file

@ -5,7 +5,12 @@ import { apiFetch } from '../api.js';
import { confirmDialog } from '../dialogs.js';
import { el, extractErrorMessage, fmtTime, type ClientState, type ServerEntry } from '../state.js';
import type { MultiplexWsClient } from '../ws-client.js';
import { createFittedTerminal, ensureXtermCss, readXtermTheme } from './xterm-runtime.js';
import {
clearTerminalComposition,
createFittedTerminal,
ensureXtermCss,
readXtermTheme,
} from './xterm-runtime.js';
interface PtySessionInfo {
sessionId: string;
@ -316,6 +321,7 @@ export async function renderTerminalView(
let term: Terminal | null = null;
let fitAddon: FitAddon | null = null;
let suppressCompositionUntil = 0;
const sendPtyInput = (raw: string) => {
if (!current || current.exited) return;
@ -350,10 +356,12 @@ export async function renderTerminalView(
);
const applyFontAndFit = () => {
if (!term || !fitAddon) return;
if (!term || !fitAddon || !termContainer.isConnected) return;
term.options.fontSize = fontSize;
term.options.theme = readXtermTheme();
fitAddon.fit();
termContainer.scrollLeft = 0;
termContainer.scrollTop = 0;
wsClient.send({
type: 'pty:resize',
sessionId: current.sessionId,
@ -385,28 +393,38 @@ export async function renderTerminalView(
await renderTerminalView(root, state, wsClient);
});
// Mobile Touch-Key Bar
// Mobile Touch-Key Bar (prevent pointer/mouse focus steal so Android IME does not jump or leave ghosts)
const touchBar = el('div', { class: 'touch-keybar', role: 'toolbar', 'aria-label': 'Mobile Terminal Keys' });
const keepTermFocus = (btn: HTMLButtonElement) => {
btn.addEventListener('pointerdown', (e) => e.preventDefault());
btn.addEventListener('mousedown', (e) => e.preventDefault());
};
const makeTouchBtn = (label: string, onPress: () => void) => {
const btn = el('button', { type: 'button', class: 'touch-key' }, label);
keepTermFocus(btn);
btn.addEventListener('click', () => {
if (term) clearTerminalComposition(term, termContainer);
onPress();
term?.focus();
});
return btn;
};
const ctrlBtn = el('button', { type: 'button', class: 'touch-key', 'aria-pressed': 'false' }, 'Ctrl');
keepTermFocus(ctrlBtn);
ctrlBtn.addEventListener('click', () => {
ctrlSticky = !ctrlSticky;
ctrlBtn.classList.toggle('active', ctrlSticky);
ctrlBtn.setAttribute('aria-pressed', ctrlSticky ? 'true' : 'false');
if (term) clearTerminalComposition(term, termContainer);
term?.focus();
});
const altBtn = el('button', { type: 'button', class: 'touch-key', 'aria-pressed': 'false' }, 'Alt');
keepTermFocus(altBtn);
altBtn.addEventListener('click', () => {
altSticky = !altSticky;
altBtn.classList.toggle('active', altSticky);
altBtn.setAttribute('aria-pressed', altSticky ? 'true' : 'false');
if (term) clearTerminalComposition(term, termContainer);
term?.focus();
});
@ -416,6 +434,7 @@ export async function renderTerminalView(
ctrlBtn,
altBtn,
makeTouchBtn('Ctrl+C', () => sendPtyInput('\x03')),
makeTouchBtn('Ctrl+X', () => sendPtyInput('\x18')),
makeTouchBtn('↑', () => sendPtyInput('\x1b[A')),
makeTouchBtn('↓', () => sendPtyInput('\x1b[B')),
makeTouchBtn('←', () => sendPtyInput('\x1b[D')),
@ -451,7 +470,91 @@ export async function renderTerminalView(
term = created.term;
fitAddon = created.fitAddon;
const consumeStickyText = (text: string): boolean => {
if (!ctrlSticky && !altSticky) return false;
if (!text) return false;
const ch = text.slice(-1);
suppressCompositionUntil = Date.now() + 140;
sendPtyInput(ch);
if (term) {
clearTerminalComposition(term, termContainer);
window.setTimeout(() => {
if (term && termContainer.isConnected) clearTerminalComposition(term, termContainer);
}, 20);
}
return true;
};
const ta = term.textarea;
const textareaCleanups: Array<() => void> = [];
if (ta) {
const addTa = <K extends keyof HTMLElementEventMap>(
evt: K,
handler: (e: HTMLElementEventMap[K]) => void,
) => {
ta.addEventListener(evt, handler as EventListener, true);
textareaCleanups.push(() => ta.removeEventListener(evt, handler as EventListener, true));
};
addTa('keydown', (ev) => {
if ((ctrlSticky || altSticky) && !ev.ctrlKey && !ev.altKey && !ev.metaKey && ev.key.length === 1) {
ev.preventDefault();
ev.stopImmediatePropagation();
consumeStickyText(ev.key);
}
});
addTa('beforeinput', (ev) => {
const ie = ev as InputEvent;
if (Date.now() < suppressCompositionUntil) {
ev.preventDefault();
ev.stopImmediatePropagation();
return;
}
if ((ctrlSticky || altSticky) && ie.data && consumeStickyText(ie.data)) {
ev.preventDefault();
ev.stopImmediatePropagation();
}
});
addTa('compositionupdate', (ev) => {
const ce = ev as CompositionEvent;
if (Date.now() < suppressCompositionUntil) {
ev.preventDefault();
ev.stopImmediatePropagation();
clearTerminalComposition(term!, termContainer);
return;
}
if ((ctrlSticky || altSticky) && ce.data && consumeStickyText(ce.data)) {
ev.preventDefault();
ev.stopImmediatePropagation();
}
});
addTa('input', (ev) => {
if (Date.now() < suppressCompositionUntil) {
ev.preventDefault();
ev.stopImmediatePropagation();
clearTerminalComposition(term!, termContainer);
}
});
addTa('compositionend', (ev) => {
if (Date.now() < suppressCompositionUntil) {
ev.preventDefault();
ev.stopImmediatePropagation();
clearTerminalComposition(term!, termContainer);
return;
}
window.setTimeout(() => {
const comp = termContainer.querySelector('.composition-view');
if (comp && !comp.textContent) comp.classList.remove('active');
}, 30);
});
}
const dataDisposable = term.onData((data) => {
if (Date.now() < suppressCompositionUntil) return;
sendPtyInput(data);
});
@ -464,11 +567,35 @@ export async function renderTerminalView(
});
});
const onWindowResize = () => {
if (!termContainer.isConnected) return;
fitAddon?.fit();
let fitRaf: number | null = null;
let lastFitCols = term.cols;
let lastFitRows = term.rows;
const scheduleFit = () => {
if (fitRaf !== null) return;
fitRaf = window.requestAnimationFrame(() => {
fitRaf = null;
if (!term || !fitAddon || !termContainer.isConnected) return;
fitAddon.fit();
termContainer.scrollLeft = 0;
termContainer.scrollTop = 0;
if (term.cols !== lastFitCols || term.rows !== lastFitRows) {
lastFitCols = term.cols;
lastFitRows = term.rows;
wsClient.send({
type: 'pty:resize',
sessionId: current.sessionId,
cols: term.cols,
rows: term.rows,
});
}
});
};
window.addEventListener('resize', onWindowResize);
window.addEventListener('resize', scheduleFit);
window.visualViewport?.addEventListener('resize', scheduleFit);
const ro =
typeof ResizeObserver !== 'undefined' ? new ResizeObserver(() => scheduleFit()) : null;
ro?.observe(termContainer);
const unsubFrame = wsClient.onFrame((frame: ServerWsFrame) => {
if (!term || !termContainer.isConnected) return;
@ -486,8 +613,12 @@ export async function renderTerminalView(
});
currentCleanup = () => {
if (fitRaf !== null) window.cancelAnimationFrame(fitRaf);
unsubFrame();
window.removeEventListener('resize', onWindowResize);
ro?.disconnect();
window.removeEventListener('resize', scheduleFit);
window.visualViewport?.removeEventListener('resize', scheduleFit);
for (const fn of textareaCleanups) fn();
dataDisposable.dispose();
resizeDisposable.dispose();
term?.dispose();