99dfbbde58
* feat(plugins): add keybindings and modal window capabilities
Add two independent plugin UI capabilities.
Keybindings: plugins declare keybindings in the manifest under
ui.keybindings ({id, default, description}), validated backend-side
(combo grammar + unique ids, requires a UI bundle) and exposed to the
frontend. Plugin JS binds a handler via registry.registerKeybinding(id,
handler); a global dispatcher fires it. Combos are user-overridable in
Settings > Keyboard Shortcuts (namespaced plugin:{pluginId}:{id}) with
conflict detection against core and other plugins.
Modals: host.openModal({title?, content, size?, dismissible?}) returns a
{ close() } handle and renders in a single host-owned Dialog isolated by
PluginErrorBoundary, cleaned up on plugin unload.
Fixture plugin, PLUGIN-API.md / spec, and a Playwright e2e demonstrate a
keybinding handler opening a modal.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* fix(plugins): address review feedback on keybinding dispatch and validation
Core shortcuts now take precedence over plugin keybindings on a colliding
combo (defaultPrevented cooperation between the two capture-phase
dispatchers), a throwing plugin handler no longer aborts dispatch to other
plugins, and the keybinding combo grammar is tightened: the backend validator
and frontend parser both drop the undocumented control/super modifier
aliases and reject shift+digit/symbol combos that can never actually fire
(KeyboardEvent.key reports the shifted character, not the token).
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* fix(plugins): stop conflicting core shortcut from double-firing with plugin keybindings
The earlier defaultPrevented guard only covered the central useAppShortcuts
dispatcher. Per-component core shortcuts registered via useKeyboardShortcut
run in the bubble phase, after the capture-phase plugin keybinding dispatcher,
so a plugin-handled-and-prevented combo still triggered the core action too.
Guard useKeyboardShortcut (and use-editor-keybinds, another capture-phase core
listener) on event.defaultPrevented so exactly one action fires per combo.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* fix(plugins): make core shortcuts always win over plugin keybindings
Panel-search, task-switcher, and other core listeners never checked
event.defaultPrevented, so a plugin bound to the same combo (e.g. Cmd/Ctrl+F)
could still fire alongside them. Instead of patching every core listener,
usePluginShortcuts now computes the set of effective core shortcut combos
(registry, resolved through user overrides, plus FIND_IN_PANEL/SAVE) and
skips invoking any plugin handler whose combo is in that set, so the
matching core listener always handles the event exactly once.
Also adds a hook-level regression test for useEditorKeybinds' existing
defaultPrevented guard, and fixes a leaked capture-phase window listener in
use-keyboard-shortcut.test.ts that could bleed into later tests.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* fix(web): keep Cmd+K closing the command panel after shortcut-precedence guard
cmdk's built-in vim bindings intercept Ctrl/Cmd+K and +P as list-navigation
shortcuts and call preventDefault() before the event reaches our window-level
useKeyboardShortcut listeners. Once that hook started bailing on
event.defaultPrevented (added to fix core/plugin shortcut double-firing), the
palette's own Cmd+K close/reopen toggle got silently suppressed whenever focus
was inside the open panel. Disable cmdk's vim bindings on the command palette
so its Ctrl/Cmd+K and +P always reach the app-level toggle.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* docs(plugins): document keybindings and modal window plugin APIs
Fill in the public author-facing docs that the internal plugin-keybindings
and modal-window contract (PLUGIN-API.md, spec.md) already covers: the
registry.registerKeybinding / host.openModal JS surface, the ui.keybindings
manifest field with its combo grammar, and a capability mention on the
Plugins overview page.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Kandev Agent <agent@kandev.dev>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
157 lines
4.8 KiB
TypeScript
157 lines
4.8 KiB
TypeScript
/**
|
|
* Parses a plugin manifest keybinding combo string (e.g. "mod+shift+k") into
|
|
* the core `KeyboardShortcut` shape consumed by `matchesShortcut`.
|
|
*
|
|
* Grammar mirrors the backend validator
|
|
* (`apps/backend/internal/plugins/manifest/validate.go` `parseKeybindingCombo`),
|
|
* which already rejected malformed combos at plugin registration time:
|
|
* `+`-separated tokens, modifiers from a fixed vocabulary, and exactly one
|
|
* non-modifier key token. This parser is defensive (returns `null` instead of
|
|
* throwing) since it also runs against values that already passed backend
|
|
* validation.
|
|
*
|
|
* The accepted modifier vocabulary — `mod|ctrl|cmd|meta|alt|option|shift` —
|
|
* matches the public contract in `docs/plans/plugins/PLUGIN-API.md` exactly.
|
|
* "control"/"super" are deliberately NOT accepted (kept in lockstep with the
|
|
* backend validator, which also rejects them).
|
|
*
|
|
* `mod` maps to `ctrlOrCmd` in the `KeyboardShortcut` shape — `matchesShortcut`
|
|
* already resolves `ctrlOrCmd` to Cmd on macOS / Ctrl elsewhere, so no
|
|
* separate platform branch is needed here.
|
|
*
|
|
* Combos pairing `shift` with a key whose printable character Shift would
|
|
* alter (digits, symbols — see `SHIFT_ALTERED_KEY_TOKENS`) are rejected: the
|
|
* combo is matched against `KeyboardEvent.key`, but a physically-held Shift
|
|
* means the browser reports the shifted character ("!", "?") instead of the
|
|
* unshifted token, so the combo could never actually dispatch. This mirrors
|
|
* the backend validator's `shiftAlteredKeys` rejection.
|
|
*/
|
|
import type { Key, KeyboardShortcut } from "./constants";
|
|
|
|
type ModifierToken = "mod" | "ctrl" | "cmd" | "meta" | "alt" | "option" | "shift";
|
|
|
|
const MODIFIER_TOKENS: Record<ModifierToken, keyof NonNullable<KeyboardShortcut["modifiers"]>> = {
|
|
mod: "ctrlOrCmd",
|
|
ctrl: "ctrl",
|
|
cmd: "cmd",
|
|
meta: "cmd",
|
|
alt: "alt",
|
|
option: "alt",
|
|
shift: "shift",
|
|
};
|
|
|
|
/**
|
|
* Non-modifier key tokens whose reported `KeyboardEvent.key` changes when
|
|
* Shift is held (e.g. "1" -> "!", "slash" -> "?"). Must stay in sync with the
|
|
* backend's `shiftAlteredKeys` in
|
|
* `apps/backend/internal/plugins/manifest/validate.go`.
|
|
*/
|
|
const SHIFT_ALTERED_KEY_TOKENS = new Set<string>([
|
|
"comma",
|
|
"period",
|
|
"slash",
|
|
"backslash",
|
|
"semicolon",
|
|
"quote",
|
|
"minus",
|
|
"equal",
|
|
"bracketleft",
|
|
"bracketright",
|
|
"backquote",
|
|
"0",
|
|
"1",
|
|
"2",
|
|
"3",
|
|
"4",
|
|
"5",
|
|
"6",
|
|
"7",
|
|
"8",
|
|
"9",
|
|
]);
|
|
|
|
/** Named-key tokens mapped to their `KeyboardEvent.key` equivalent. */
|
|
const NAMED_KEY_TOKENS: Record<string, string> = {
|
|
enter: "Enter",
|
|
escape: "Escape",
|
|
esc: "Escape",
|
|
tab: "Tab",
|
|
space: " ",
|
|
backspace: "Backspace",
|
|
delete: "Delete",
|
|
insert: "Insert",
|
|
arrowup: "ArrowUp",
|
|
arrowdown: "ArrowDown",
|
|
arrowleft: "ArrowLeft",
|
|
arrowright: "ArrowRight",
|
|
up: "ArrowUp",
|
|
down: "ArrowDown",
|
|
left: "ArrowLeft",
|
|
right: "ArrowRight",
|
|
home: "Home",
|
|
end: "End",
|
|
pageup: "PageUp",
|
|
pagedown: "PageDown",
|
|
comma: ",",
|
|
period: ".",
|
|
slash: "/",
|
|
backslash: "\\",
|
|
semicolon: ";",
|
|
quote: "'",
|
|
minus: "-",
|
|
equal: "=",
|
|
bracketleft: "[",
|
|
bracketright: "]",
|
|
backquote: "`",
|
|
};
|
|
|
|
function isSingleAlphanumeric(token: string): boolean {
|
|
return /^[a-z0-9]$/.test(token);
|
|
}
|
|
|
|
function isFunctionKey(token: string): boolean {
|
|
return /^f([1-9]|1[0-2])$/.test(token);
|
|
}
|
|
|
|
/** Resolves a single non-modifier token to a `KeyboardShortcut["key"]`, or null if unrecognized. */
|
|
function resolveKeyToken(token: string): Key | null {
|
|
if (token in NAMED_KEY_TOKENS) return NAMED_KEY_TOKENS[token] as Key;
|
|
if (isSingleAlphanumeric(token) || isFunctionKey(token)) return token as Key;
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Parses a combo string like "mod+shift+k" into a `KeyboardShortcut`.
|
|
* Returns null when the combo is empty, contains an unknown token, does not
|
|
* contain exactly one non-modifier key token, or pairs `shift` with a key
|
|
* whose reported character shift would alter (see `SHIFT_ALTERED_KEY_TOKENS`).
|
|
*/
|
|
export function parseCombo(combo: string): KeyboardShortcut | null {
|
|
if (!combo || !combo.trim()) return null;
|
|
|
|
const tokens = combo.split("+").map((token) => token.trim().toLowerCase());
|
|
const modifiers: NonNullable<KeyboardShortcut["modifiers"]> = {};
|
|
let key: Key | null = null;
|
|
let keyToken: string | null = null;
|
|
|
|
for (const token of tokens) {
|
|
if (!token) return null;
|
|
|
|
if (token in MODIFIER_TOKENS) {
|
|
modifiers[MODIFIER_TOKENS[token as ModifierToken]] = true;
|
|
continue;
|
|
}
|
|
|
|
const resolvedKey = resolveKeyToken(token);
|
|
if (!resolvedKey) return null;
|
|
if (key !== null) return null; // more than one non-modifier key token
|
|
key = resolvedKey;
|
|
keyToken = token;
|
|
}
|
|
|
|
if (key === null || keyToken === null) return null;
|
|
if (modifiers.shift && SHIFT_ALTERED_KEY_TOKENS.has(keyToken)) return null;
|
|
|
|
return Object.keys(modifiers).length > 0 ? { key, modifiers } : { key };
|
|
}
|