feat(plugins): add keybindings and modal window capabilities (#1895)
* 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>
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import { act, cleanup, render, screen } from "@testing-library/react";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { pluginModalManager } from "@/lib/plugins/modal-manager";
|
||||
import { PluginModalHost } from "./plugin-modal-host";
|
||||
|
||||
function cleanupModals(pluginId: string) {
|
||||
pluginModalManager.closeAllForPlugin(pluginId);
|
||||
}
|
||||
|
||||
describe("PluginModalHost", () => {
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
cleanupModals("plugin-a");
|
||||
});
|
||||
|
||||
it("renders nothing when no plugin has an open modal", () => {
|
||||
const { container } = render(<PluginModalHost />);
|
||||
expect(container.innerHTML).toBe("");
|
||||
});
|
||||
|
||||
it("renders an open modal's title and content", () => {
|
||||
pluginModalManager.openModal("plugin-a", {
|
||||
title: "My Modal",
|
||||
content: () => <div data-testid="modal-content">Hello</div>,
|
||||
});
|
||||
|
||||
render(<PluginModalHost />);
|
||||
|
||||
expect(screen.getByText("My Modal")).not.toBeNull();
|
||||
expect(screen.getByTestId("modal-content")).not.toBeNull();
|
||||
});
|
||||
|
||||
it("removes the modal from the DOM once its handle is closed", () => {
|
||||
const handle = pluginModalManager.openModal("plugin-a", {
|
||||
content: () => <div data-testid="modal-content">Hello</div>,
|
||||
});
|
||||
|
||||
render(<PluginModalHost />);
|
||||
expect(screen.getByTestId("modal-content")).not.toBeNull();
|
||||
|
||||
act(() => {
|
||||
handle.close();
|
||||
});
|
||||
|
||||
expect(screen.queryByTestId("modal-content")).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,73 @@
|
||||
"use client";
|
||||
|
||||
import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@kandev/ui/dialog";
|
||||
import {
|
||||
pluginModalManager,
|
||||
usePluginModals,
|
||||
type OpenPluginModal,
|
||||
} from "@/lib/plugins/modal-manager";
|
||||
import type { PluginModalOptions } from "@/lib/plugins/types";
|
||||
import { PluginErrorBoundary } from "./plugin-error-boundary";
|
||||
|
||||
/** Maps `PluginModalOptions.size` to the host's Dialog width classes. */
|
||||
const SIZE_CLASSES: Record<NonNullable<PluginModalOptions["size"]>, string> = {
|
||||
sm: "sm:max-w-sm",
|
||||
md: "sm:max-w-xl",
|
||||
lg: "sm:max-w-3xl",
|
||||
xl: "sm:max-w-5xl",
|
||||
};
|
||||
|
||||
function preventWhenNotDismissible(dismissible: boolean) {
|
||||
return (event: Event) => {
|
||||
if (!dismissible) event.preventDefault();
|
||||
};
|
||||
}
|
||||
|
||||
function PluginModalInstance({ modal }: { modal: OpenPluginModal }) {
|
||||
const { instanceId, pluginId, options } = modal;
|
||||
const dismissible = options.dismissible ?? true;
|
||||
const Content = options.content;
|
||||
const guardClose = preventWhenNotDismissible(dismissible);
|
||||
|
||||
const handleOpenChange = (open: boolean) => {
|
||||
if (open || !dismissible) return;
|
||||
pluginModalManager.close(instanceId);
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open onOpenChange={handleOpenChange}>
|
||||
<DialogContent
|
||||
className={SIZE_CLASSES[options.size ?? "md"]}
|
||||
showCloseButton={dismissible}
|
||||
onEscapeKeyDown={guardClose}
|
||||
onInteractOutside={guardClose}
|
||||
>
|
||||
{options.title && (
|
||||
<DialogHeader>
|
||||
<DialogTitle>{options.title}</DialogTitle>
|
||||
</DialogHeader>
|
||||
)}
|
||||
<PluginErrorBoundary context={`modal "${instanceId}" (plugin "${pluginId}")`}>
|
||||
<Content />
|
||||
</PluginErrorBoundary>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders every open plugin modal (`host.openModal(...)`) in a `@kandev/ui`
|
||||
* `Dialog`, each isolated behind its own `PluginErrorBoundary`. Mounted once
|
||||
* at the app root, alongside `<PluginBootBridge/>`.
|
||||
*/
|
||||
export function PluginModalHost() {
|
||||
const modals = usePluginModals();
|
||||
if (modals.length === 0) return null;
|
||||
return (
|
||||
<>
|
||||
{modals.map((modal) => (
|
||||
<PluginModalInstance key={modal.instanceId} modal={modal} />
|
||||
))}
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user