Skip to content

Commit 0b5507d

Browse files
committed
fix(web): honor editable focus in shortcut conditions [unmbp]
1 parent 9828cad commit 0b5507d

2 files changed

Lines changed: 110 additions & 2 deletions

File tree

‎apps/web/src/keybindings.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,20 @@ import {
1515
matchesKeybindingShortcutModifiers,
1616
normalizeEventKey,
1717
resolveEventKeys,
18-
type ShortcutEventLike,
18+
type ShortcutEventLike as SharedShortcutEventLike,
1919
type ShortcutModifierStateLike,
2020
} from "@t3tools/shared/keybindings";
2121
import { isElectron } from "./env";
22+
import { isEditableFocused } from "./lib/editableFocus";
2223
import { projectScriptIdFromCommand } from "./projectScripts";
2324

24-
export type { ShortcutEventLike, ShortcutModifierStateLike } from "@t3tools/shared/keybindings";
25+
export type { ShortcutModifierStateLike } from "@t3tools/shared/keybindings";
2526
export { shortcutKeyFromEvent } from "@t3tools/shared/keybindings";
2627

28+
export interface ShortcutEventLike extends SharedShortcutEventLike {
29+
target?: EventTarget | null;
30+
}
31+
2732
export interface ShortcutMatchContext {
2833
terminalFocus: boolean;
2934
terminalOpen: boolean;
@@ -178,6 +183,9 @@ export function resolveShortcutCommand(
178183
): KeybindingCommand | null {
179184
const platform = resolvePlatform(options);
180185
const context = resolveContext(options);
186+
if (options?.context?.editableFocus === undefined && typeof document !== "undefined") {
187+
context.editableFocus = isEditableFocused(event.target ?? document.activeElement);
188+
}
181189

182190
for (let index = keybindings.length - 1; index >= 0; index -= 1) {
183191
const binding = keybindings[index];
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
});

0 commit comments

Comments
 (0)