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
14 changes: 9 additions & 5 deletions apps/app/src/components/promptbox/PromptBoxInternal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6486,24 +6486,28 @@ describe("thread URL clipboard paste", () => {
}
});

it.each(["ctrlKey", "metaKey"])(
"keeps %s+Shift+V literal and converts the next normal clipboard paste through the SDK",
async (modifier) => {
it.each([
{ modifier: "ctrlKey", key: "V" },
{ modifier: "metaKey", key: "V" },
{ modifier: "ctrlKey", key: "\u041c" },
])(
"keeps $modifier+Shift+V ($key) literal and converts the next normal clipboard paste through the SDK",
async ({ modifier, key }) => {
const lookup = vi
.spyOn(sdk.threads, "resolveMentions")
.mockResolvedValue([resolved]);
try {
const { changes, promptBoxRef } = renderPromptBox("");
await focusPromptEnd(promptBoxRef);
fireEvent.keyDown(getPromptEditorElement(), {
key: "V",
key,
code: "KeyV",
shiftKey: true,
[modifier]: true,
});
pastePlainText(url);
fireEvent.keyUp(getPromptEditorElement(), {
key: "V",
key,
code: "KeyV",
});
await act(async () => {
Expand Down
15 changes: 7 additions & 8 deletions apps/app/src/components/thread/terminal/terminal-clipboard.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { matchesAppShortcutKey } from "@bb/domain";
import { isImeComposing } from "@bb/shared-ui/lib/ime-keyboard";
import type { Terminal } from "@xterm/xterm";

Expand All @@ -20,14 +21,12 @@ export function createTerminalClipboardKeyHandler({
return true;
}

const key = /^[a-z]$/iu.test(event.key)
? event.key.toLowerCase()
: event.code === "KeyC"
? "c"
: event.code === "KeyV"
? "v"
: event.key;
if (key !== "c" && key !== "v") {
const key = matchesAppShortcutKey(event, "c")
? "c"
: matchesAppShortcutKey(event, "v")
? "v"
: null;
if (key === null) {
return true;
}

Expand Down
23 changes: 23 additions & 0 deletions apps/app/src/lib/keyboard-shortcut-settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,29 @@ describe("keyboard shortcut settings", () => {
).toEqual([...APP_COMMAND_IDS].sort());
});

it("records the physical key on a non-Latin layout", () => {
expect(
appShortcutFromInput(
{
key: "л",
code: "KeyK",
altKey: false,
ctrlKey: true,
metaKey: false,
shiftKey: false,
},
"Win32",
),
).toEqual({
key: "k",
mod: true,
meta: false,
control: false,
alt: false,
shift: false,
});
});

it("records primary modifiers and unshifted punctuation", () => {
expect(
appShortcutFromInput(
Expand Down
2 changes: 2 additions & 0 deletions apps/app/src/lib/plugin-sdk-app-impl.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { applicationLinkApprovals } from "./application-url";
import { matchesAppShortcutKey } from "@bb/domain";
import {
LazyPluginMarkdown,
LazyPluginDiff,
Expand Down Expand Up @@ -185,6 +186,7 @@ export const pluginSdkAppImplementation = {
experimental_VoiceRecordingBar: VoiceRecordingBar,
experimental_FailedVoiceRecordings: FailedVoiceRecordings,
experimental_ShortcutHint: AppCommandShortcutHint,
experimental_matchesShortcutKey: matchesAppShortcutKey,
experimental_ImageLightbox: ImageLightbox,
experimental_PromptMentionIcon: ComposerMentionIcon,
experimental_describePromptMention: describePromptMention,
Expand Down
13 changes: 12 additions & 1 deletion apps/desktop/src/desktop-reload-shortcut.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { matchesAppShortcutKey } from "@bb/domain";

interface DesktopReloadShortcutInput {
alt: boolean;
code: string;
control: boolean;
isAutoRepeat: boolean;
isComposing: boolean;
Expand All @@ -21,7 +24,15 @@ export function resolveDesktopReloadShortcut(
input.alt ||
input.control ||
!input.meta ||
input.key.toLowerCase() !== "r"
!matchesAppShortcutKey(
{
key: input.key,
code: input.code,
altKey: input.alt,
shiftKey: input.shift,
},
"r",
)
) {
return null;
}
Expand Down
15 changes: 15 additions & 0 deletions apps/desktop/test/desktop-reload-shortcut.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function input(
): Parameters<typeof resolveDesktopReloadShortcut>[0] {
return {
alt: false,
code: "KeyR",
control: false,
isAutoRepeat: false,
isComposing: false,
Expand All @@ -25,6 +26,20 @@ describe("resolveDesktopReloadShortcut", () => {
);
});

it.each([
{ layout: "Russian", key: "к", code: "KeyR" },
{ layout: "Greek", key: "ρ", code: "KeyR" },
{ layout: "Dvorak", key: "r", code: "KeyO" },
])("reloads from the R key on a $layout layout", ({ key, code }) => {
expect(resolveDesktopReloadShortcut(input({ key, code }))).toBe("reload");
});

it("follows Latin layouts rather than the physical R position", () => {
expect(
resolveDesktopReloadShortcut(input({ key: "p", code: "KeyR" })),
).toBeNull();
});

it("ignores unrelated, repeated, and composing input", () => {
expect(resolveDesktopReloadShortcut(input({ key: "x" }))).toBeNull();
expect(resolveDesktopReloadShortcut(input({ meta: false }))).toBeNull();
Expand Down
25 changes: 25 additions & 0 deletions docs/api_to_audit.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,31 @@ and saved binding lifecycle before stabilizing the keyboard shortcut
contract. Audit cross-platform conflicts and plugin lifecycle before
extending the context model or default binding policy.

## `experimental_matchesShortcutKey` (`@get-bb/plugin-sdk/app`)

`experimental_matchesShortcutKey(event, key)` says whether a keydown presses
`key`, compared the way bb compares keys for its own and plugins' keybindings.
The caller checks modifiers. Letter, digit and punctuation keys match by
physical position (`event.code`) when the layout types a non-Latin character
there (or, with Alt held, anything but an ASCII letter or digit); otherwise
they match the typed character with Shift removed from US punctuation, so
Latin layouts such as Dvorak and AZERTY keep their own letters. Without Alt,
the typed character also matches, which keeps shortcuts saved under a
non-Latin character working.

Docs: `frontend-renderer-slots.md`.

Core callers: `matchesAppShortcut` in `@bb/domain` (app commands, plugin
commands, the desktop embedded browser and shortcut conflicts), the desktop
reload shortcut and the terminal copy/paste keys all call the same
`matchesAppShortcutKey`; the SDK function is that function. Plugin callers:
the Composer's paste-as-plain-text check, Prompt Library's Mod+S and Tasks'
single-key shortcuts.

Before stabilizing, decide whether plugins also need a full-chord matcher that
resolves `mod` per platform, and whether settings should label shortcuts with
the active layout's characters (`navigator.keyboard.getLayoutMap()`).

## Discoverable RPC

`bb.rpc.register` accepts `experimental_discoverable` and
Expand Down
58 changes: 50 additions & 8 deletions packages/domain/src/app-keybindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,29 +200,72 @@ const SHIFTED_KEY_BASES: Readonly<Record<string, string>> = {
"?": "/",
};

const PUNCTUATION_KEYS_BY_CODE: Readonly<Record<string, string>> = {
Backquote: "`",
Minus: "-",
Equal: "=",
BracketLeft: "[",
BracketRight: "]",
Backslash: "\\",
Semicolon: ";",
Quote: "'",
Comma: ",",
Period: ".",
Slash: "/",
};

function baseKeyFromCode(code: string): string | null {
if (/^Key[A-Z]$/u.test(code)) return code.slice(3).toLowerCase();
if (/^Digit[0-9]$/u.test(code)) return code.slice(5);
return null;
return PUNCTUATION_KEYS_BY_CODE[code] ?? null;
}

function isAsciiAlphanumeric(value: string): boolean {
return /^[a-z0-9]$/iu.test(value);
}

export function normalizeAppShortcutInputKey(input: AppShortcutInput): string {
function isNonLatinCharacter(value: string): boolean {
return /^(?![\p{Script=Latin}0-9])[\p{L}\p{N}]$/u.test(value);
}

type AppShortcutKeyInput = Pick<
AppShortcutInput,
"altKey" | "code" | "key" | "shiftKey"
>;

function layoutAppShortcutKey(input: AppShortcutKeyInput): string {
if (input.key === " " || input.key === "Spacebar") {
return "Space";
}
if (input.altKey && !isAsciiAlphanumeric(input.key)) {
const fromCode = baseKeyFromCode(input.code);
if (fromCode !== null) return fromCode;
}
return input.shiftKey
? (SHIFTED_KEY_BASES[input.key] ?? input.key)
: input.key;
}

export function normalizeAppShortcutInputKey(
input: AppShortcutKeyInput,
): string {
const usePhysicalKey = input.altKey
? !isAsciiAlphanumeric(input.key)
: isNonLatinCharacter(input.key);
if (usePhysicalKey) {
const fromCode = baseKeyFromCode(input.code);
if (fromCode !== null) return fromCode;
}
return layoutAppShortcutKey(input);
}

export function matchesAppShortcutKey(
input: AppShortcutKeyInput,
key: string,
): boolean {
const expected = key.toLowerCase();
return (
normalizeAppShortcutInputKey(input).toLowerCase() === expected ||
(!input.altKey && layoutAppShortcutKey(input).toLowerCase() === expected)
);
}

export const keyboardPlatformSchema = z.enum(["mac", "windows", "linux"]);
export type KeyboardPlatform = z.infer<typeof keyboardPlatformSchema>;

Expand Down Expand Up @@ -265,8 +308,7 @@ export function matchesAppShortcut(
const expectedMeta = shortcut.meta || (shortcut.mod && useMetaForMod);
const expectedControl = shortcut.control || (shortcut.mod && !useMetaForMod);
return (
normalizeAppShortcutInputKey(input).toLowerCase() ===
shortcut.key.toLowerCase() &&
matchesAppShortcutKey(input, shortcut.key) &&
input.metaKey === expectedMeta &&
input.ctrlKey === expectedControl &&
input.altKey === shortcut.alt &&
Expand Down
2 changes: 1 addition & 1 deletion packages/domain/src/plugin-sdk-version.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const PLUGIN_SDK_VERSION = "0.6.159";
export const PLUGIN_SDK_VERSION = "0.6.160";

export const PLUGIN_SDK_MAJOR = Number(PLUGIN_SDK_VERSION.split(".", 1)[0]);
Loading
Loading