The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 08:15 UTC
fix(web): honor editableFocus in shortcut conditions (#24134)
Co-authored-by: maria-rcks <maria@kuuro.net>
Ivan Fedyunin committed
Nov 1, 2026 at 17:55 UTC
fcb1d0b1db6f69ab67c21f90bbc1153dcafa3dcd
2 files changed
+110
-2
apps/web/src/keybindings.ts
+10
-2
@@ -15,15 +15,20 @@ import {
15
matchesKeybindingShortcutModifiers,
16
normalizeEventKey,
17
resolveEventKeys,
18
- type ShortcutEventLike,
18
+ type ShortcutEventLike as SharedShortcutEventLike,
19
type ShortcutModifierStateLike,
20
} from "@t3tools/shared/keybindings";
21
import { isElectron } from "./env";
22
+import { isEditableFocused } from "./lib/editableFocus";
23
import { projectScriptIdFromCommand } from "./projectScripts";
24
24
-export type { ShortcutEventLike, ShortcutModifierStateLike } from "@t3tools/shared/keybindings";
25
+export type { ShortcutModifierStateLike } from "@t3tools/shared/keybindings";
26
export { shortcutKeyFromEvent } from "@t3tools/shared/keybindings";
27
28
+export interface ShortcutEventLike extends SharedShortcutEventLike {
29
+ target?: EventTarget | null;
30
+}
31
+
32
export interface ShortcutMatchContext {
33
terminalFocus: boolean;
34
terminalOpen: boolean;
@@ -177,6 +182,9 @@ export function resolveShortcutCommand(
182
): KeybindingCommand | null {
183
const platform = resolvePlatform(options);
184
const context = resolveContext(options);
185
+ if (options?.context?.editableFocus === undefined && typeof document !== "undefined") {
186
+ context.editableFocus = isEditableFocused(event.target ?? document.activeElement);
187
+ }
188
189
for (let index = keybindings.length - 1; index >= 0; index -= 1) {
190
const binding = keybindings[index];
apps/web/src/keybindingsFocus.test.ts
new
+100
@@ -0,0 +1,100 @@
1
+import { describe, expect, it, vi } from "vite-plus/test";
2
+import type { KeybindingCommand } from "@t3tools/contracts";
3
+import { compileResolvedKeybindingsConfig } from "@t3tools/shared/keybindings";
4
+import { resolveShortcutCommand } from "./keybindings";
5
+import * as editableFocus from "./lib/editableFocus";
6
+
7
+function setupFocus() {
8
+ const editor = new EventTarget();
9
+ const other = new EventTarget();
10
+ vi.stubGlobal("document", { activeElement: editor });
11
+ const focus = vi
12
+ .spyOn(editableFocus, "isEditableFocused")
13
+ .mockImplementation((target) => target === editor);
14
+ return {
15
+ editor,
16
+ other,
17
+ [Symbol.dispose]() {
18
+ focus.mockRestore();
19
+ vi.unstubAllGlobals();
20
+ },
21
+ };
22
+}
23
+
24
+function keydown(target?: EventTarget) {
25
+ return {
26
+ key: "k",
27
+ ctrlKey: true,
28
+ metaKey: false,
29
+ altKey: false,
30
+ shiftKey: false,
31
+ ...(target ? { target } : {}),
32
+ };
33
+}
34
+
35
+function bindings(command: KeybindingCommand, when = "!editableFocus") {
36
+ return compileResolvedKeybindingsConfig([{ key: "mod+k", command, when }]);
37
+}
38
+
39
+describe("editable shortcut conditions", () => {
40
+ it.each(["commandPalette.toggle", "sidebar.toggle", "thread.next", "thread.jump.1"] as const)(
41
+ "honors !editableFocus for %s without a caller-provided focus context",
42
+ (command) => {
43
+ using focus = setupFocus();
44
+ const rules = bindings(command);
45
+
46
+ expect(
47
+ resolveShortcutCommand(keydown(focus.editor), rules, {
48
+ platform: "Linux",
49
+ context: { terminalFocus: false },
50
+ }),
51
+ ).toBeNull();
52
+ expect(resolveShortcutCommand(keydown(focus.other), rules, { platform: "Linux" })).toBe(
53
+ command,
54
+ );
55
+ },
56
+ );
57
+
58
+ it("uses the active element when a forwarded shortcut has no target", () => {
59
+ using _focus = setupFocus();
60
+ expect(
61
+ resolveShortcutCommand(keydown(), bindings("commandPalette.toggle"), { platform: "Linux" }),
62
+ ).toBeNull();
63
+ });
64
+
65
+ it("honors a positive editableFocus condition", () => {
66
+ using focus = setupFocus();
67
+ const rules = bindings("commandPalette.toggle", "editableFocus");
68
+ expect(resolveShortcutCommand(keydown(focus.editor), rules, { platform: "Linux" })).toBe(
69
+ "commandPalette.toggle",
70
+ );
71
+ expect(resolveShortcutCommand(keydown(focus.other), rules, { platform: "Linux" })).toBeNull();
72
+ });
73
+
74
+ it("preserves explicit focus overrides", () => {
75
+ using focus = setupFocus();
76
+ const rules = bindings("commandPalette.toggle");
77
+
78
+ expect(
79
+ resolveShortcutCommand(keydown(focus.editor), rules, {
80
+ platform: "Linux",
81
+ context: { editableFocus: false },
82
+ }),
83
+ ).toBe("commandPalette.toggle");
84
+ expect(
85
+ resolveShortcutCommand(keydown(focus.other), rules, {
86
+ platform: "Linux",
87
+ context: { editableFocus: true },
88
+ }),
89
+ ).toBeNull();
90
+ });
91
+
92
+ it("keeps shortcuts without an editable-focus condition active while editing", () => {
93
+ using focus = setupFocus();
94
+ expect(
95
+ resolveShortcutCommand(keydown(focus.editor), bindings("commandPalette.toggle", "true"), {
96
+ platform: "Linux",
97
+ }),
98
+ ).toBe("commandPalette.toggle");
99
+ });
100
+});