Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

PreviewKeyboard.ts

203 lines7.6 KB

1import type { PreviewAutomationPressInput } from "@v12/contracts";
2​
3interface KeyDefinition {
4 readonly code: string;
5 readonly key: string;
6 readonly keyCode: number;
7 readonly text?: string;
8 readonly location?: number;
9 readonly shiftedKey?: string;
11​
12export interface PreviewAutomationKeyEvent {
13 readonly [key: string]: unknown;
14 readonly type: "keyDown" | "rawKeyDown" | "keyUp";
15 readonly key: string;
16 readonly code: string;
17 readonly modifiers: number;
18 readonly windowsVirtualKeyCode: number;
19 readonly location: number;
20 readonly isKeypad: boolean;
21 readonly text?: string;
22 readonly unmodifiedText?: string;
23 readonly commands?: ReadonlyArray<string>;
25​
26export interface PreviewAutomationKeySequence {
27 readonly keyDown: PreviewAutomationKeyEvent;
28 readonly keyUp: PreviewAutomationKeyEvent;
29 readonly signal: {
30 readonly kind: "key";
31 readonly key: string;
32 readonly code: string;
33 };
35​
36const NAMED_KEYS: Readonly<Record<string, KeyDefinition>> = {
37 Escape: { code: "Escape", key: "Escape", keyCode: 27 },
38 Backspace: { code: "Backspace", key: "Backspace", keyCode: 8 },
39 Tab: { code: "Tab", key: "Tab", keyCode: 9 },
40 Enter: { code: "Enter", key: "Enter", keyCode: 13, text: "\r" },
41 Shift: { code: "ShiftLeft", key: "Shift", keyCode: 16, location: 1 },
42 Control: { code: "ControlLeft", key: "Control", keyCode: 17, location: 1 },
43 Alt: { code: "AltLeft", key: "Alt", keyCode: 18, location: 1 },
44 Meta: { code: "MetaLeft", key: "Meta", keyCode: 91, location: 1 },
45 CapsLock: { code: "CapsLock", key: "CapsLock", keyCode: 20 },
46 Space: { code: "Space", key: " ", keyCode: 32, text: " " },
47 PageUp: { code: "PageUp", key: "PageUp", keyCode: 33 },
48 PageDown: { code: "PageDown", key: "PageDown", keyCode: 34 },
49 End: { code: "End", key: "End", keyCode: 35 },
50 Home: { code: "Home", key: "Home", keyCode: 36 },
51 ArrowLeft: { code: "ArrowLeft", key: "ArrowLeft", keyCode: 37 },
52 ArrowUp: { code: "ArrowUp", key: "ArrowUp", keyCode: 38 },
53 ArrowRight: { code: "ArrowRight", key: "ArrowRight", keyCode: 39 },
54 ArrowDown: { code: "ArrowDown", key: "ArrowDown", keyCode: 40 },
55 Insert: { code: "Insert", key: "Insert", keyCode: 45 },
56 Delete: { code: "Delete", key: "Delete", keyCode: 46 },
57};
58​
59const PRINTABLE_KEYS: ReadonlyArray<KeyDefinition> = [
60 { code: "Backquote", key: "`", shiftedKey: "~", keyCode: 192 },
61 { code: "Digit1", key: "1", shiftedKey: "!", keyCode: 49 },
62 { code: "Digit2", key: "2", shiftedKey: "@", keyCode: 50 },
63 { code: "Digit3", key: "3", shiftedKey: "#", keyCode: 51 },
64 { code: "Digit4", key: "4", shiftedKey: "$", keyCode: 52 },
65 { code: "Digit5", key: "5", shiftedKey: "%", keyCode: 53 },
66 { code: "Digit6", key: "6", shiftedKey: "^", keyCode: 54 },
67 { code: "Digit7", key: "7", shiftedKey: "&", keyCode: 55 },
68 { code: "Digit8", key: "8", shiftedKey: "*", keyCode: 56 },
69 { code: "Digit9", key: "9", shiftedKey: "(", keyCode: 57 },
70 { code: "Digit0", key: "0", shiftedKey: ")", keyCode: 48 },
71 { code: "Minus", key: "-", shiftedKey: "_", keyCode: 189 },
72 { code: "Equal", key: "=", shiftedKey: "+", keyCode: 187 },
73 { code: "Backslash", key: "\\", shiftedKey: "|", keyCode: 220 },
74 { code: "BracketLeft", key: "[", shiftedKey: "{", keyCode: 219 },
75 { code: "BracketRight", key: "]", shiftedKey: "}", keyCode: 221 },
76 { code: "Semicolon", key: ";", shiftedKey: ":", keyCode: 186 },
77 { code: "Quote", key: "'", shiftedKey: '"', keyCode: 222 },
78 { code: "Comma", key: ",", shiftedKey: "<", keyCode: 188 },
79 { code: "Period", key: ".", shiftedKey: ">", keyCode: 190 },
80 { code: "Slash", key: "/", shiftedKey: "?", keyCode: 191 },
81];
82​
83/**
84 * Chromium does not infer macOS editing commands from synthetic Meta chords.
85 * Keep the common browser editing/navigation shortcuts explicit so dispatched
86 * key events behave like their physical-key equivalents.
87 */
88const MAC_EDITING_COMMANDS: Readonly<Record<string, string>> = {
89 "Meta+Backspace": "deleteToBeginningOfLine",
90 "Meta+ArrowUp": "moveToBeginningOfDocument",
91 "Meta+ArrowDown": "moveToEndOfDocument",
92 "Meta+ArrowLeft": "moveToLeftEndOfLine",
93 "Meta+ArrowRight": "moveToRightEndOfLine",
94 "Shift+Meta+ArrowUp": "moveToBeginningOfDocumentAndModifySelection",
95 "Shift+Meta+ArrowDown": "moveToEndOfDocumentAndModifySelection",
96 "Shift+Meta+ArrowLeft": "moveToLeftEndOfLineAndModifySelection",
97 "Shift+Meta+ArrowRight": "moveToRightEndOfLineAndModifySelection",
98 "Meta+KeyA": "selectAll",
99 "Meta+KeyC": "copy",
100 "Meta+KeyX": "cut",
101 "Meta+KeyV": "paste",
102 "Meta+KeyZ": "undo",
103 "Shift+Meta+KeyZ": "redo",
104};
105const SHORTCUT_MODIFIER_ORDER = ["Shift", "Control", "Alt", "Meta"] as const;
106​
107const macEditingCommands = (
108 code: string,
109 modifiers: PreviewAutomationPressInput["modifiers"],
110): ReadonlyArray<string> => {
111 const shortcut = [
112 ...SHORTCUT_MODIFIER_ORDER.filter((modifier) => modifiers?.includes(modifier)),
113 code,
114 ].join("+");
115 const command = MAC_EDITING_COMMANDS[shortcut];
116 return command ? [command] : [];
117};
118​
119const modifierMask = (modifiers: PreviewAutomationPressInput["modifiers"]): number =>
120 (modifiers ?? []).reduce((value, modifier) => {
121 switch (modifier) {
122 case "Alt":
123 return value | 1;
124 case "Control":
125 return value | 2;
126 case "Meta":
127 return value | 4;
128 case "Shift":
129 return value | 8;
130 }
131 }, 0);
132​
133function resolveKeyDefinition(input: PreviewAutomationPressInput): KeyDefinition {
134 const named = NAMED_KEYS[input.key];
135 if (named) return named;
136​
137 const functionKey = /^F([1-9]|1[0-2])$/.exec(input.key);
138 if (functionKey) {
139 const number = Number(functionKey[1]);
140 return { code: input.key, key: input.key, keyCode: 111 + number };
141 }
142​
143 if (/^[a-z]$/i.test(input.key)) {
144 const upper = input.key.toUpperCase();
145 const shifted = input.modifiers?.includes("Shift") ?? false;
146 const key = shifted || input.key === upper ? upper : input.key;
147 return { code: `Key${upper}`, key, keyCode: upper.charCodeAt(0), text: key };
148 }
149​
150 const printable = PRINTABLE_KEYS.find(
151 (definition) => definition.key === input.key || definition.shiftedKey === input.key,
152 );
153 if (printable) {
154 const shifted = input.modifiers?.includes("Shift") ?? false;
155 const key =
156 printable.shiftedKey && (shifted || input.key === printable.shiftedKey)
157 ? printable.shiftedKey
158 : printable.key;
159 return { ...printable, key, text: key };
160 }
161​
162 return {
163 code: input.key.length > 1 ? input.key : "",
164 key: input.key,
165 keyCode: 0,
166 ...(input.key.length === 1 ? { text: input.key } : {}),
167 };
169​
170/**
171 * Build Chromium CDP key packets using the same required fields and down-event
172 * choice as Playwright's pinned Chromium keyboard implementation.
173 */
174export function makePreviewAutomationKeySequence(
175 input: PreviewAutomationPressInput,
176 options?: { readonly isMac?: boolean },
177): PreviewAutomationKeySequence {
178 const definition = resolveKeyDefinition(input);
179 const modifiers = modifierMask(input.modifiers);
180 const suppressText = input.modifiers?.some((modifier) => modifier !== "Shift") ?? false;
181 const text = suppressText ? "" : (definition.text ?? "");
182 const location = definition.location ?? 0;
183 const commands = options?.isMac ? macEditingCommands(definition.code, input.modifiers) : [];
184 const shared = {
185 key: definition.key,
186 code: definition.code,
187 modifiers,
188 windowsVirtualKeyCode: definition.keyCode,
189 location,
190 isKeypad: location === 3,
191 };
192​
193 return {
194 keyDown: {
195 type: text ? "keyDown" : "rawKeyDown",
196 ...shared,
197 ...(text ? { text, unmodifiedText: text } : {}),
198 ...(commands.length > 0 ? { commands } : {}),
199 },
200 keyUp: { type: "keyUp", ...shared },
201 signal: { kind: "key", key: definition.key, code: definition.code },
202 };