diff --git a/apps/app/src/components/promptbox/PromptBoxInternal.test.tsx b/apps/app/src/components/promptbox/PromptBoxInternal.test.tsx index b3df1cb6c89..e64604df1ff 100644 --- a/apps/app/src/components/promptbox/PromptBoxInternal.test.tsx +++ b/apps/app/src/components/promptbox/PromptBoxInternal.test.tsx @@ -6486,9 +6486,13 @@ 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]); @@ -6496,14 +6500,14 @@ describe("thread URL clipboard paste", () => { 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 () => { diff --git a/apps/app/src/components/thread/terminal/terminal-clipboard.ts b/apps/app/src/components/thread/terminal/terminal-clipboard.ts index d265a22e124..468250fa6b5 100644 --- a/apps/app/src/components/thread/terminal/terminal-clipboard.ts +++ b/apps/app/src/components/thread/terminal/terminal-clipboard.ts @@ -1,3 +1,4 @@ +import { matchesAppShortcutKey } from "@bb/domain"; import { isImeComposing } from "@bb/shared-ui/lib/ime-keyboard"; import type { Terminal } from "@xterm/xterm"; @@ -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; } diff --git a/apps/app/src/lib/keyboard-shortcut-settings.test.ts b/apps/app/src/lib/keyboard-shortcut-settings.test.ts index 1b75ae8ea5b..497f0d0ca97 100644 --- a/apps/app/src/lib/keyboard-shortcut-settings.test.ts +++ b/apps/app/src/lib/keyboard-shortcut-settings.test.ts @@ -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( diff --git a/apps/app/src/lib/plugin-sdk-app-impl.tsx b/apps/app/src/lib/plugin-sdk-app-impl.tsx index 01dbd8fcd11..3d4834bbbbf 100644 --- a/apps/app/src/lib/plugin-sdk-app-impl.tsx +++ b/apps/app/src/lib/plugin-sdk-app-impl.tsx @@ -1,4 +1,5 @@ import { applicationLinkApprovals } from "./application-url"; +import { matchesAppShortcutKey } from "@bb/domain"; import { LazyPluginMarkdown, LazyPluginDiff, @@ -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, diff --git a/apps/desktop/src/desktop-reload-shortcut.ts b/apps/desktop/src/desktop-reload-shortcut.ts index 3ad822f455e..0bf2ad8c215 100644 --- a/apps/desktop/src/desktop-reload-shortcut.ts +++ b/apps/desktop/src/desktop-reload-shortcut.ts @@ -1,5 +1,8 @@ +import { matchesAppShortcutKey } from "@bb/domain"; + interface DesktopReloadShortcutInput { alt: boolean; + code: string; control: boolean; isAutoRepeat: boolean; isComposing: boolean; @@ -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; } diff --git a/apps/desktop/test/desktop-reload-shortcut.test.ts b/apps/desktop/test/desktop-reload-shortcut.test.ts index a96495a4199..fb5d72e008c 100644 --- a/apps/desktop/test/desktop-reload-shortcut.test.ts +++ b/apps/desktop/test/desktop-reload-shortcut.test.ts @@ -6,6 +6,7 @@ function input( ): Parameters[0] { return { alt: false, + code: "KeyR", control: false, isAutoRepeat: false, isComposing: false, @@ -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(); diff --git a/docs/api_to_audit.md b/docs/api_to_audit.md index e70cc2e6c38..97206b7418a 100644 --- a/docs/api_to_audit.md +++ b/docs/api_to_audit.md @@ -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 diff --git a/packages/domain/src/app-keybindings.ts b/packages/domain/src/app-keybindings.ts index eb6aec9538d..7b793b1433b 100644 --- a/packages/domain/src/app-keybindings.ts +++ b/packages/domain/src/app-keybindings.ts @@ -200,29 +200,72 @@ const SHIFTED_KEY_BASES: Readonly> = { "?": "/", }; +const PUNCTUATION_KEYS_BY_CODE: Readonly> = { + 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; @@ -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 && diff --git a/packages/domain/src/plugin-sdk-version.ts b/packages/domain/src/plugin-sdk-version.ts index 763233b3e01..45adcc2bcf6 100644 --- a/packages/domain/src/plugin-sdk-version.ts +++ b/packages/domain/src/plugin-sdk-version.ts @@ -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]); diff --git a/packages/domain/test/app-keybindings-layouts.test.ts b/packages/domain/test/app-keybindings-layouts.test.ts new file mode 100644 index 00000000000..9d816ad286e --- /dev/null +++ b/packages/domain/test/app-keybindings-layouts.test.ts @@ -0,0 +1,176 @@ +import { describe, expect, it } from "vitest"; +import { + matchesAppShortcut, + normalizeAppShortcutInputKey, + type AppShortcut, + type AppShortcutInput, +} from "../src/app-keybindings.js"; + +function shortcut(key: string, modifiers: Partial = {}) { + return { + key, + mod: false, + meta: false, + control: false, + alt: false, + shift: false, + ...modifiers, + }; +} + +function press( + key: string, + code: string, + modifiers: Partial = {}, +): AppShortcutInput { + return { + key, + code, + altKey: false, + ctrlKey: false, + metaKey: false, + shiftKey: false, + ...modifiers, + }; +} + +const ctrl = { ctrlKey: true }; +const ctrlShift = { ctrlKey: true, shiftKey: true }; + +describe("app shortcut keys across keyboard layouts", () => { + it.each([ + { layout: "US", input: press("k", "KeyK", ctrl), key: "k" }, + { layout: "Russian", input: press("л", "KeyK", ctrl), key: "k" }, + { layout: "Russian", input: press("З", "KeyP", ctrlShift), key: "p" }, + { + layout: "Russian", + input: press("Х", "BracketLeft", ctrlShift), + key: "[", + }, + { + layout: "Russian", + input: press("Ъ", "BracketRight", ctrlShift), + key: "]", + }, + { layout: "Russian", input: press("б", "Comma", ctrl), key: "," }, + { layout: "Russian", input: press("1", "Digit1", ctrl), key: "1" }, + { layout: "Ukrainian", input: press("і", "KeyS", ctrl), key: "s" }, + { layout: "Greek", input: press("κ", "KeyK", ctrl), key: "k" }, + { layout: "Greek", input: press("ς", "KeyW", ctrl), key: "w" }, + { layout: "Hebrew", input: press("ל", "KeyK", ctrl), key: "k" }, + { layout: "Hebrew", input: press("/", "KeyQ", ctrl), key: "/" }, + { layout: "Arabic", input: press("ن", "KeyK", ctrl), key: "k" }, + { layout: "Arabic", input: press("١", "Digit1", ctrl), key: "1" }, + { layout: "Dvorak", input: press("k", "KeyV", ctrl), key: "k" }, + { layout: "Colemak", input: press("e", "KeyK", ctrl), key: "e" }, + { layout: "AZERTY", input: press("a", "KeyQ", ctrl), key: "a" }, + { layout: "AZERTY", input: press("&", "Digit1", ctrl), key: "&" }, + { layout: "QWERTZ", input: press("z", "KeyY", ctrl), key: "z" }, + { layout: "QWERTZ", input: press("ü", "BracketLeft", ctrl), key: "ü" }, + { layout: "Turkish", input: press("ı", "KeyI", ctrl), key: "ı" }, + ])("$layout $input.key on $input.code is $key", ({ input, key }) => { + expect(normalizeAppShortcutInputKey(input).toLowerCase()).toBe(key); + }); + + it.each([ + { layout: "Russian", input: press("л", "KeyK", ctrl) }, + { layout: "Greek", input: press("κ", "KeyK", ctrl) }, + { layout: "Hebrew", input: press("ל", "KeyK", ctrl) }, + { layout: "Arabic", input: press("ن", "KeyK", ctrl) }, + { layout: "Dvorak", input: press("k", "KeyV", ctrl) }, + ])("$layout fires Ctrl+K", ({ input }) => { + expect(matchesAppShortcut(input, shortcut("k", { mod: true }), false)).toBe( + true, + ); + }); + + it("keeps Latin layouts on their own letters", () => { + expect( + matchesAppShortcut( + press("k", "KeyV", ctrl), + shortcut("v", { mod: true }), + false, + ), + ).toBe(false); + expect( + matchesAppShortcut( + press("e", "KeyK", ctrl), + shortcut("k", { mod: true }), + false, + ), + ).toBe(false); + expect( + matchesAppShortcut( + press("ü", "BracketLeft", ctrlShift), + shortcut("[", { mod: true, shift: true }), + false, + ), + ).toBe(false); + }); + + it("fires Mod+Shift+[ from the Cyrillic Х position on macOS", () => { + expect( + matchesAppShortcut( + press("Х", "BracketLeft", { metaKey: true, shiftKey: true }), + shortcut("[", { mod: true, shift: true }), + true, + ), + ).toBe(true); + }); + + it("still matches shortcuts saved under a non-Latin character", () => { + expect( + matchesAppShortcut( + press("л", "KeyK", ctrl), + shortcut("л", { mod: true }), + false, + ), + ).toBe(true); + }); + + it.each([ + { + layout: "Polish", + input: press("ą", "KeyA", { ctrlKey: true, altKey: true }), + }, + { + layout: "German", + input: press("@", "KeyQ", { ctrlKey: true, altKey: true }), + }, + { + layout: "Serbian", + input: press("ђ", "BracketLeft", { ctrlKey: true, altKey: true }), + }, + ])( + "does not turn Windows AltGr text on $layout into Ctrl or Alt shortcuts", + ({ input }) => { + const physical = normalizeAppShortcutInputKey(input); + for (const modifiers of [ + { mod: true }, + { alt: true }, + { control: true }, + ]) { + expect( + matchesAppShortcut(input, shortcut(physical, modifiers), false), + ).toBe(false); + } + }, + ); + + it("keeps macOS Option compositions on the physical key", () => { + expect( + matchesAppShortcut( + press("π", "KeyP", { altKey: true }), + shortcut("p", { alt: true }), + true, + ), + ).toBe(true); + expect( + matchesAppShortcut( + press("π", "KeyP", { altKey: true }), + shortcut("π", { alt: true }), + true, + ), + ).toBe(false); + }); +}); diff --git a/packages/plugin-sdk/package.json b/packages/plugin-sdk/package.json index d4388316b34..0e976a383a3 100644 --- a/packages/plugin-sdk/package.json +++ b/packages/plugin-sdk/package.json @@ -1,6 +1,6 @@ { "name": "@get-bb/plugin-sdk", - "version": "0.6.159", + "version": "0.6.160", "license": "MIT", "homepage": "https://github.com/get-bb/bb#readme", "bugs": { diff --git a/packages/plugin-sdk/src/app-contract.ts b/packages/plugin-sdk/src/app-contract.ts index 023dc96bff3..f45d7136f5c 100644 --- a/packages/plugin-sdk/src/app-contract.ts +++ b/packages/plugin-sdk/src/app-contract.ts @@ -3689,6 +3689,14 @@ export interface PluginCommandShortcut { shift?: boolean; } +/** The fields of a keydown that {@link PluginSdkApp.experimental_matchesShortcutKey} reads; DOM and React keyboard events both fit. */ +export interface ExperimentalShortcutKeyEvent { + key: string; + code: string; + altKey: boolean; + shiftKey: boolean; +} + /** * A command registered with `app.commands.register`, listed in bb's quick * palette (Mod+Shift+P) under the plugin's name @@ -6313,6 +6321,19 @@ export interface PluginSdkApp { experimental_VoiceRecordingBar: ComponentType; experimental_FailedVoiceRecordings: ComponentType; experimental_ShortcutHint: ComponentType; + /** + * Whether `event` presses `key` (a `PluginCommandShortcut` key such as + * `"k"`, `"["` or `"Enter"`), compared the way bb compares keys for its own + * keybindings; the caller checks modifiers. Letter, digit and punctuation + * keys match by physical position when the active layout types a non-Latin + * character there, so Ctrl+K is found on Cyrillic, Greek, Hebrew or Arabic + * layouts, while Latin layouts such as Dvorak keep their own letters. + * Experimental: see docs/api_to_audit.md. + */ + experimental_matchesShortcutKey( + event: ExperimentalShortcutKeyEvent, + key: string, + ): boolean; experimental_ImageLightbox: ComponentType; experimental_PromptMentionIcon: ComponentType; /** diff --git a/packages/plugin-sdk/src/app.ts b/packages/plugin-sdk/src/app.ts index 1a3a94a3a11..8ca6f433a02 100644 --- a/packages/plugin-sdk/src/app.ts +++ b/packages/plugin-sdk/src/app.ts @@ -253,6 +253,9 @@ export const experimental_ImageLightbox = runtimeComponent( export const experimental_PromptMentionIcon = runtimeComponent( "experimental_PromptMentionIcon", ); +export const experimental_matchesShortcutKey = runtimeFunction( + "experimental_matchesShortcutKey", +); export const experimental_describePromptMention = runtimeFunction( "experimental_describePromptMention", ); diff --git a/packages/plugin-sdk/src/testing/app.tsx b/packages/plugin-sdk/src/testing/app.tsx index d4f09b6f6ac..ed62473b2fc 100644 --- a/packages/plugin-sdk/src/testing/app.tsx +++ b/packages/plugin-sdk/src/testing/app.tsx @@ -164,6 +164,7 @@ import { type SourceCodeProps, type JsonValue, } from "@get-bb/plugin-sdk"; +import { matchesAppShortcutKey } from "@bb/domain"; import { isComposerDraftEmpty } from "../internal/composer-view.js"; import { appendComposerDraft, @@ -1865,6 +1866,7 @@ const testPluginSdkApp = { experimental_VoiceRecordingBar: TestVoiceRecordingBar, experimental_FailedVoiceRecordings: TestFailedVoiceRecordings, experimental_ShortcutHint: TestShortcutHint, + experimental_matchesShortcutKey: matchesAppShortcutKey, experimental_ImageLightbox: TestImageLightbox, experimental_PromptMentionIcon: TestPromptMentionIcon, experimental_describePromptMention( diff --git a/plugins/bb-guide/skills/bb-plugin-authoring/references/frontend-renderer-slots.md b/plugins/bb-guide/skills/bb-plugin-authoring/references/frontend-renderer-slots.md index 4c14b9a04d3..6872f6b4a7b 100644 --- a/plugins/bb-guide/skills/bb-plugin-authoring/references/frontend-renderer-slots.md +++ b/plugins/bb-guide/skills/bb-plugin-authoring/references/frontend-renderer-slots.md @@ -250,6 +250,11 @@ message, Original }` — render fenced code blocks of chosen languages in `plugin:/`. Keyboard invocation uses the same current context and error handling as the palette and is suppressed while a modal is open. Shortcuts run only while the plugin frontend is active. + Registered shortcuts match by physical key when the active layout types a + non-Latin character (Cyrillic, Greek, Hebrew, Arabic), and by the typed + character on Latin layouts such as Dvorak. A plugin that handles keys + itself compares them the same way with + `experimental_matchesShortcutKey(event, key)` and checks modifiers itself. A command that acts on the composer the user is typing in registers through `app.composer.experimental_registerCommand` instead; see the composer section of frontend-hooks-and-ui. diff --git a/plugins/composer/package.json b/plugins/composer/package.json index aa864408397..a71d22763ac 100644 --- a/plugins/composer/package.json +++ b/plugins/composer/package.json @@ -6,7 +6,7 @@ "description": "bb's composer: the prompt box with rich text, @-mentions, slash commands, voice and attachments.", "engines": { "bb": ">=0.0", - "bbPluginSdk": ">=0.6.77" + "bbPluginSdk": ">=0.6.160" }, "bb": { "name": "Composer", diff --git a/plugins/composer/src/PromptBoxInternal.tsx b/plugins/composer/src/PromptBoxInternal.tsx index d648def7b49..d0be1f1bc16 100644 --- a/plugins/composer/src/PromptBoxInternal.tsx +++ b/plugins/composer/src/PromptBoxInternal.tsx @@ -2,6 +2,7 @@ import { experimental_ComposerRoot as ComposerRoot, experimental_FailedVoiceRecordings as FailedVoiceRecordings, experimental_Icon as SdkIcon, + experimental_matchesShortcutKey as matchesShortcutKey, experimental_ShortcutHint as AppCommandShortcutHint, experimental_useComposerCommandSearch as useComposerCommandSearch, experimental_useComposerContributions as useComposerContributions, @@ -1794,7 +1795,7 @@ export function PromptBoxInternal({ pasteWithoutFormattingRef.current = (event.metaKey || event.ctrlKey) && event.shiftKey && - event.key.toLowerCase() === "v"; + matchesShortcutKey(event, "v"); if ( !_view.editable || !isIPadOSWebKitDevice || diff --git a/plugins/plugin-api-docs/src/surfaces.ts b/plugins/plugin-api-docs/src/surfaces.ts index d0b45f6b872..770ea3fd8a6 100644 --- a/plugins/plugin-api-docs/src/surfaces.ts +++ b/plugins/plugin-api-docs/src/surfaces.ts @@ -649,6 +649,8 @@ export const SURFACE_GROUPS: SurfaceGroup[] = [ "PluginCommandRegistration", "PluginCommandContext", "PluginCommandShortcut", + "experimental_matchesShortcutKey", + "ExperimentalShortcutKeyEvent", ], firstParty: ["File Editor"], }, diff --git a/plugins/prompt-library/app.tsx b/plugins/prompt-library/app.tsx index 2d7552e15ec..65a32ec9493 100644 --- a/plugins/prompt-library/app.tsx +++ b/plugins/prompt-library/app.tsx @@ -12,6 +12,7 @@ import { observeSharedResize } from "@/lib/shared-resize-observer"; import { cn } from "@/lib/utils"; import { definePluginApp, + experimental_matchesShortcutKey as matchesShortcutKey, experimental_usePluginId, Markdown, useBbContext, @@ -105,7 +106,7 @@ function formatAge(timestamp: number, now: number): string { function isStarShortcut(event: ReactKeyboardEvent): boolean { return ( - event.key.toLowerCase() === "s" && + matchesShortcutKey(event, "s") && (event.metaKey || event.ctrlKey) && !event.altKey && !event.shiftKey diff --git a/plugins/prompt-library/package.json b/plugins/prompt-library/package.json index 29a8715e109..d225d4c9c4f 100644 --- a/plugins/prompt-library/package.json +++ b/plugins/prompt-library/package.json @@ -6,7 +6,7 @@ "description": "Search previous prompts, star favorites, and insert them into the composer.", "engines": { "bb": ">=0.0", - "bbPluginSdk": ">=0.6.109" + "bbPluginSdk": ">=0.6.160" }, "bb": { "name": "Prompt Library", diff --git a/plugins/tasks/package.json b/plugins/tasks/package.json index aaead525ba7..7e2c378d996 100644 --- a/plugins/tasks/package.json +++ b/plugins/tasks/package.json @@ -23,7 +23,7 @@ ], "engines": { "bb": ">=0.0", - "bbPluginSdk": ">=0.6.113" + "bbPluginSdk": ">=0.6.160" }, "bb": { "name": "Tasks", diff --git a/plugins/tasks/shell/app-shell.tsx b/plugins/tasks/shell/app-shell.tsx index 0542cea994e..cf771f50979 100644 --- a/plugins/tasks/shell/app-shell.tsx +++ b/plugins/tasks/shell/app-shell.tsx @@ -1,5 +1,8 @@ import { useCallback, useEffect, useMemo, useRef, useState } from "react"; -import type { PluginNavPanelProps } from "@get-bb/plugin-sdk/app"; +import { + experimental_matchesShortcutKey as matchesShortcutKey, + type PluginNavPanelProps, +} from "@get-bb/plugin-sdk/app"; import { useProjects } from "./data.js"; import { parseTasksRoute, @@ -154,7 +157,13 @@ function TasksAppShellContent({ subPath }: PluginNavPanelProps) { useEffect(() => { const onKeyDown = (event: KeyboardEvent) => { - if (event.key !== "c" || event.metaKey || event.ctrlKey || event.altKey) + if ( + !matchesShortcutKey(event, "c") || + event.metaKey || + event.ctrlKey || + event.altKey || + event.shiftKey + ) return; if (event.defaultPrevented || event.repeat) return; if (isEditableTarget(event.target)) return; diff --git a/plugins/tasks/views/list/property-menus.tsx b/plugins/tasks/views/list/property-menus.tsx index d98adbdecec..73038b37ae8 100644 --- a/plugins/tasks/views/list/property-menus.tsx +++ b/plugins/tasks/views/list/property-menus.tsx @@ -1,4 +1,8 @@ import type { ReactNode } from "react"; +import { + experimental_matchesShortcutKey as matchesShortcutKey, + type ExperimentalShortcutKeyEvent, +} from "@get-bb/plugin-sdk/app"; import { TASK_STATUSES, type Label, @@ -48,6 +52,15 @@ export const PRIORITY_MENU_ORDER: readonly TaskPriority[] = [ "low", ]; +const SHORTCUT_DIGITS = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]; + +function shortcutDigit(event: ExperimentalShortcutKeyEvent): string { + return ( + SHORTCUT_DIGITS.find((digit) => matchesShortcutKey(event, digit)) ?? + event.key + ); +} + export function statusForShortcut(key: string): TaskStatus | null { if (!/^[0-9]$/.test(key)) return null; const index = Number(key) - 1; @@ -153,7 +166,7 @@ export function StatusEditor({ className="min-w-56" mobileTitle="Change status" onKeyDown={(event) => { - const status = statusForShortcut(event.key); + const status = statusForShortcut(shortcutDigit(event)); if (status !== null && isBareKey(event)) { event.preventDefault(); select(status); @@ -213,7 +226,7 @@ export function PriorityEditor({ className="min-w-52" mobileTitle="Set priority" onKeyDown={(event) => { - const priority = priorityForShortcut(event.key); + const priority = priorityForShortcut(shortcutDigit(event)); if (priority !== null && isBareKey(event)) { event.preventDefault(); select(priority); diff --git a/plugins/tasks/views/list/row.tsx b/plugins/tasks/views/list/row.tsx index 04d0ae686ba..d5c53af9de9 100644 --- a/plugins/tasks/views/list/row.tsx +++ b/plugins/tasks/views/list/row.tsx @@ -5,6 +5,7 @@ import type { Task, TaskThread, } from "../../shared/contract.js"; +import { experimental_matchesShortcutKey as matchesShortcutKey } from "@get-bb/plugin-sdk/app"; import { Icon } from "@/components/ui/icon"; import { observeSharedResize } from "@/lib/shared-resize-observer"; import { cn } from "@/lib/utils"; @@ -189,11 +190,10 @@ export const TaskRow = memo(function TaskRow({ onClick={() => onOpen(task.key)} onKeyDown={(event) => { if (!isBareKey(event)) return; - const key = event.key.toLowerCase(); - if (key === "s") { + if (matchesShortcutKey(event, "s")) { event.preventDefault(); setOpenMenu("status"); - } else if (key === "p") { + } else if (matchesShortcutKey(event, "p")) { event.preventDefault(); setOpenMenu("priority"); }