mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-21 23:50:45 +00:00
* fix(terminal): commit live IME transcription once xterm sends a rewritten dictation hypothesis as the interim, the full final string, and the tail. Keep the pending textarea diff from firing again when composition starts, and send only the part that is not already on the PTY. * fix(terminal): retype rewritten IME tails as separate backspaces A shared suffix has to be deleted and typed again because the cursor stays at the end of the line. One-character hypotheses are real prefixes, and each backspace is its own input event so Ctrl-H hosts still erase. * fix(terminal): rewrite a revised IME hypothesis in place A composition that replaces the snapshot, instead of extending it, sends the cancelled textarea edit. Deletions are counted in graphemes so a surrogate pair is one backspace. * fix(terminal): send a new IME word that shares a finished prefix A composition that starts at or after the snapshot is a new word. Sharing the first character with text already on the PTY is not enough to treat it as the same hypothesis. * fix(terminal): remember a rewritten IME hypothesis in full A 229 rewrite stored only the inserted tail, so a later compositionend with no composition marker sent the revised line again. Remember the whole revised textarea instead. * fix(terminal): preserve IME rewrite state across delayed commits
182 lines
8.1 KiB
TypeScript
182 lines
8.1 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { readFileSync } from "node:fs";
|
|
import test from "node:test";
|
|
import { runInNewContext } from "node:vm";
|
|
import ts from "typescript";
|
|
import { resolveKittyKeyboardBroadcastInput, type KittyKeyboardBroadcastInput } from "./kittyKeyboardBroadcast";
|
|
import { createKittyKeyboardModeState, setKittyKeyboardModeFlags, shouldEncodeKittyCompositionText, shouldMarkKittyTextInputEvent, encodeKittyCompositionText } from "./kittyKeyboardProtocol";
|
|
import { shouldBlockKeyPressForImeTextInput, shouldCommitDeferredImeTextInput } from "./terminalImeTextInput";
|
|
import { sanitizeTerminalInput } from "./terminalInputSanitize";
|
|
|
|
// Execute the actual registered key/data callbacks, without constructing the
|
|
// renderer and its WebGL/addon stack. In particular, retain their real timer
|
|
// cleanup and raw-broadcast suppression rather than reproducing that logic here.
|
|
const source = readFileSync(new URL("./createXTermRuntime.ts", import.meta.url), "utf8");
|
|
function section(start: string, end: string): string {
|
|
const from = source.indexOf(start);
|
|
const to = source.indexOf(end, from);
|
|
assert.ok(from >= 0 && to > from, `runtime section missing: ${start}`);
|
|
return source.slice(from, to);
|
|
}
|
|
const declarations = section(" let suppressNextTerminalDataBroadcast =", " const broadcastKittyInput =");
|
|
const keyboardCallback = section(" term.attachCustomKeyEventHandler((e: KeyboardEvent) => {", " const handleMiddleClick =");
|
|
const dataCallback = section(" term.onData((data) => {", " const handleKittyKeyboardBroadcast =");
|
|
const compositionMarker = section(" const markKittyCompositionPending =", " const finishKittyComposition =");
|
|
const textInputMarker = section(" const markKittyTextInput =", ' textarea?.addEventListener("compositionstart"');
|
|
const suppression = section(" const suppressTerminalBroadcast =", " // skipBroadcast");
|
|
const callbackCode = ts.transpileModule(`
|
|
${declarations}
|
|
let win32InputModePendingEvent = null;
|
|
let kittyCompositionPending = false;
|
|
let kittyCompositionClearTimer;
|
|
const handleTerminalInputData = (data) => {
|
|
const inputSource = "terminal";
|
|
${suppression}
|
|
if (!suppressTerminalBroadcast) raw(data);
|
|
};
|
|
const textarea = null;
|
|
${compositionMarker}
|
|
${textInputMarker}
|
|
${keyboardCallback}
|
|
${dataCallback}
|
|
globalThis.controls = { mark: markBroadcastLegacyDataPending, clear: clearBroadcastLegacyDataPending, input: markKittyTextInput, composition: markKittyCompositionPending };
|
|
`, { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText;
|
|
|
|
function setup(flags = 0, sourceFlags = 0) {
|
|
const writes: string[] = [];
|
|
const timers = new Map<number, () => void>();
|
|
let timerId = 0;
|
|
let receive!: (data: string) => void;
|
|
let keyboard!: (event: Partial<KeyboardEvent>) => boolean;
|
|
const forwarded = new Map<string, { targetSessionIds: string[] }>();
|
|
const mode = createKittyKeyboardModeState();
|
|
setKittyKeyboardModeFlags(mode, flags);
|
|
const sourceMode = createKittyKeyboardModeState();
|
|
setKittyKeyboardModeFlags(sourceMode, sourceFlags);
|
|
const options = {
|
|
kittyProtocolEnabled: flags !== 0, kittyMode: mode, applicationCursorMode: false,
|
|
encodedKeys: new Set<string>(), legacySuppressedKeys: new Set<string>(),
|
|
};
|
|
const normalized = (input: KittyKeyboardBroadcastInput) => {
|
|
const result = resolveKittyKeyboardBroadcastInput(input, options);
|
|
if (result) writes.push(result.data);
|
|
};
|
|
const context = {
|
|
controls: undefined as unknown as { mark: (identity: string) => void; clear: () => void; input: (event: { data: string; inputType: string }) => void; composition: () => void },
|
|
window: {
|
|
setTimeout(fn: () => void) { const id = ++timerId; timers.set(id, fn); return id; },
|
|
clearTimeout(id: number) { timers.delete(id); },
|
|
},
|
|
term: {
|
|
modes: { win32InputMode: false },
|
|
onData(fn: typeof receive) { receive = fn; },
|
|
attachCustomKeyEventHandler(fn: typeof keyboard) { keyboard = fn; },
|
|
},
|
|
ctx: { terminalSettingsRef: { current: {} }, isBroadcastEnabledRef: { current: true }, onBroadcastInputRef: { current: () => undefined } },
|
|
imeTextInputDeferredKey: null, imeTextInputDeferredKittyEvent: null,
|
|
shouldBlockKeyPressForImeTextInput, shouldCommitDeferredImeTextInput, shouldMarkKittyTextInputEvent, shouldEncodeKittyCompositionText, encodeKittyCompositionText,
|
|
kittyKeyboardMode: sourceMode, shouldSplitImeTextInputForWire: () => false,
|
|
kittyKeyIdentity: (event: Partial<KeyboardEvent>) => event.code || event.key,
|
|
broadcastForwardedKeys: forwarded,
|
|
broadcastKittyInput: normalized,
|
|
sanitizeTerminalInput,
|
|
shouldSplitRawPasteInputForWire: () => false,
|
|
raw: (data: string) => writes.push(data),
|
|
};
|
|
runInNewContext(callbackCode, context);
|
|
return {
|
|
writes, receive: (data: string) => receive(data), controls: context.controls,
|
|
keypress: (key: string, code: string) => keyboard({ type: "keypress", key, code }),
|
|
press(key: string, code: string) {
|
|
normalized({ kind: "key", event: { type: "keydown", key, code }, fallbackToLegacy: true });
|
|
forwarded.set(code, { targetSessionIds: ["target"] });
|
|
context.controls.mark(code);
|
|
},
|
|
flushTimers() { for (const [id, fn] of timers) { timers.delete(id); fn(); } },
|
|
release(key: string, code: string) {
|
|
normalized({ kind: "key", event: { type: "keyup", key, code }, fallbackToLegacy: true });
|
|
forwarded.delete(code);
|
|
context.controls.clear();
|
|
},
|
|
};
|
|
}
|
|
|
|
test("IME rewrite backspaces leave Kitty composition routing for replacement text", () => {
|
|
const runtime = setup(0, 24);
|
|
runtime.controls.composition();
|
|
runtime.receive("\x7f");
|
|
runtime.receive("Xde");
|
|
assert.equal(runtime.writes[0], "\x7f");
|
|
assert.ok(runtime.writes.includes("\x1b[0;;88:100:101u"));
|
|
assert.ok(!runtime.writes.includes("\x1b[0;;127u"));
|
|
});
|
|
|
|
for (const flags of [0, 8]) {
|
|
for (const [key, code] of [["A", "KeyA"], [" ", "Space"]]) {
|
|
test(`broadcast pairs delayed ${code} keypress once with target flags ${flags}`, () => {
|
|
const runtime = setup(flags);
|
|
runtime.press(key, code);
|
|
const firstWrite = runtime.writes.join("");
|
|
assert.ok(firstWrite);
|
|
runtime.flushTimers();
|
|
assert.equal(runtime.keypress(key, code), true);
|
|
runtime.receive(key);
|
|
assert.equal(runtime.writes.join(""), firstWrite, "the source's later text must not duplicate its physical key broadcast");
|
|
});
|
|
}
|
|
}
|
|
|
|
test("repeated physical presses each reach the broadcast target once", () => {
|
|
const runtime = setup();
|
|
for (let i = 0; i < 3; i++) {
|
|
runtime.press("A", "KeyA");
|
|
runtime.flushTimers();
|
|
runtime.keypress("A", "KeyA");
|
|
runtime.receive("A");
|
|
}
|
|
assert.equal(runtime.writes.join(""), "AAA");
|
|
});
|
|
|
|
test("keypress without an earlier broadcast and text after release are not swallowed", () => {
|
|
const runtime = setup();
|
|
runtime.keypress("B", "KeyB");
|
|
runtime.receive("B");
|
|
runtime.press("A", "KeyA");
|
|
runtime.flushTimers();
|
|
runtime.keypress("A", "KeyA");
|
|
runtime.receive("A");
|
|
runtime.release("A", "KeyA");
|
|
runtime.receive("paste");
|
|
assert.equal(runtime.writes.join(""), "BApaste");
|
|
});
|
|
|
|
test("unmatched keypress cleanup still permits a later paste", () => {
|
|
const runtime = setup();
|
|
runtime.press("A", "KeyA");
|
|
runtime.flushTimers();
|
|
runtime.keypress("A", "KeyA");
|
|
runtime.flushTimers();
|
|
runtime.receive("paste");
|
|
assert.equal(runtime.writes.join(""), "Apaste");
|
|
});
|
|
|
|
test("trailing insertText for Space does not turn the next physical key into duplicate composition text", () => {
|
|
const runtime = setup();
|
|
for (const [key, code] of [[" ", "Space"], ["A", "KeyA"], ["B", "KeyB"], ["C", "KeyC"]]) {
|
|
runtime.press(key, code);
|
|
runtime.keypress(key, code);
|
|
runtime.receive(key);
|
|
runtime.controls.input({ data: key, inputType: "insertText" });
|
|
runtime.release(key, code);
|
|
// Real trace: the next key can arrive before insertText's zero-delay timer.
|
|
}
|
|
assert.equal(runtime.writes.join(""), " ABC");
|
|
});
|
|
|
|
test("keyless insertText is still broadcast as actual text", () => {
|
|
const runtime = setup();
|
|
runtime.controls.input({ data: "中文", inputType: "insertText" });
|
|
runtime.receive("中文");
|
|
assert.equal(runtime.writes.join(""), "中文");
|
|
});
|