Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions apps/web/src/keybindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@ import {
matchesKeybindingShortcutModifiers,
normalizeEventKey,
resolveEventKeys,
type ShortcutEventLike,
type ShortcutEventLike as SharedShortcutEventLike,
type ShortcutModifierStateLike,
} from "@t3tools/shared/keybindings";
import { isElectron } from "./env";
import { isEditableFocused } from "./lib/editableFocus";
import { projectScriptIdFromCommand } from "./projectScripts";

export type { ShortcutEventLike, ShortcutModifierStateLike } from "@t3tools/shared/keybindings";
export type { ShortcutModifierStateLike } from "@t3tools/shared/keybindings";
export { shortcutKeyFromEvent } from "@t3tools/shared/keybindings";

export interface ShortcutEventLike extends SharedShortcutEventLike {
target?: EventTarget | null;
}

export interface ShortcutMatchContext {
terminalFocus: boolean;
terminalOpen: boolean;
Expand Down Expand Up @@ -177,6 +182,9 @@ export function resolveShortcutCommand(
): KeybindingCommand | null {
const platform = resolvePlatform(options);
const context = resolveContext(options);
if (options?.context?.editableFocus === undefined && typeof document !== "undefined") {
context.editableFocus = isEditableFocused(event.target ?? document.activeElement);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

for (let index = keybindings.length - 1; index >= 0; index -= 1) {
const binding = keybindings[index];
Expand Down
100 changes: 100 additions & 0 deletions apps/web/src/keybindingsFocus.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { describe, expect, it, vi } from "vite-plus/test";
import type { KeybindingCommand } from "@t3tools/contracts";
import { compileResolvedKeybindingsConfig } from "@t3tools/shared/keybindings";
import { resolveShortcutCommand } from "./keybindings";
import * as editableFocus from "./lib/editableFocus";

function setupFocus() {
const editor = new EventTarget();
const other = new EventTarget();
vi.stubGlobal("document", { activeElement: editor });
const focus = vi
.spyOn(editableFocus, "isEditableFocused")
.mockImplementation((target) => target === editor);
return {
editor,
other,
[Symbol.dispose]() {
focus.mockRestore();
vi.unstubAllGlobals();
},
};
}

function keydown(target?: EventTarget) {
return {
key: "k",
ctrlKey: true,
metaKey: false,
altKey: false,
shiftKey: false,
...(target ? { target } : {}),
};
}

function bindings(command: KeybindingCommand, when = "!editableFocus") {
return compileResolvedKeybindingsConfig([{ key: "mod+k", command, when }]);
}

describe("editable shortcut conditions", () => {
it.each(["commandPalette.toggle", "sidebar.toggle", "thread.next", "thread.jump.1"] as const)(
"honors !editableFocus for %s without a caller-provided focus context",
(command) => {
using focus = setupFocus();
const rules = bindings(command);

expect(
resolveShortcutCommand(keydown(focus.editor), rules, {
platform: "Linux",
context: { terminalFocus: false },
}),
).toBeNull();
expect(resolveShortcutCommand(keydown(focus.other), rules, { platform: "Linux" })).toBe(
command,
);
},
);

it("uses the active element when a forwarded shortcut has no target", () => {
using _focus = setupFocus();
expect(
resolveShortcutCommand(keydown(), bindings("commandPalette.toggle"), { platform: "Linux" }),
).toBeNull();
});

it("honors a positive editableFocus condition", () => {
using focus = setupFocus();
const rules = bindings("commandPalette.toggle", "editableFocus");
expect(resolveShortcutCommand(keydown(focus.editor), rules, { platform: "Linux" })).toBe(
"commandPalette.toggle",
);
expect(resolveShortcutCommand(keydown(focus.other), rules, { platform: "Linux" })).toBeNull();
});

it("preserves explicit focus overrides", () => {
using focus = setupFocus();
const rules = bindings("commandPalette.toggle");

expect(
resolveShortcutCommand(keydown(focus.editor), rules, {
platform: "Linux",
context: { editableFocus: false },
}),
).toBe("commandPalette.toggle");
expect(
resolveShortcutCommand(keydown(focus.other), rules, {
platform: "Linux",
context: { editableFocus: true },
}),
).toBeNull();
});

it("keeps shortcuts without an editable-focus condition active while editing", () => {
using focus = setupFocus();
expect(
resolveShortcutCommand(keydown(focus.editor), bindings("commandPalette.toggle", "true"), {
platform: "Linux",
}),
).toBe("commandPalette.toggle");
});
});
Loading