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>
162 lines
5.1 KiB
TypeScript
162 lines
5.1 KiB
TypeScript
import { describe, it, expect, vi, afterEach } from "vitest";
|
|
import * as React from "react";
|
|
import { createAppStore } from "@/lib/state/store";
|
|
import { buildHostApi } from "./host-api";
|
|
|
|
/** Curated primitives a plugin needs to build a full native-feeling page. */
|
|
const EXPECTED_UI_PRIMITIVES = [
|
|
"Alert",
|
|
"Badge",
|
|
"Button",
|
|
"Card",
|
|
"Checkbox",
|
|
"Dialog",
|
|
"DropdownMenu",
|
|
"Input",
|
|
"Label",
|
|
"Pagination",
|
|
"ScrollArea",
|
|
"Select",
|
|
"Sheet",
|
|
"SheetClose",
|
|
"SheetContent",
|
|
"SheetDescription",
|
|
"SheetFooter",
|
|
"SheetHeader",
|
|
"SheetTitle",
|
|
"SheetTrigger",
|
|
"Spinner",
|
|
"Switch",
|
|
"Table",
|
|
"Tabs",
|
|
"Textarea",
|
|
"Tooltip",
|
|
];
|
|
|
|
describe("buildHostApi", () => {
|
|
const originalFetch = global.fetch;
|
|
|
|
afterEach(() => {
|
|
global.fetch = originalFetch;
|
|
vi.unstubAllEnvs();
|
|
});
|
|
|
|
it("scopes api.fetch to /api/plugins/{pluginId}/... and forwards init", async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(new Response(null, { status: 200 }));
|
|
global.fetch = fetchMock as unknown as typeof fetch;
|
|
|
|
const host = buildHostApi("jira", createAppStore(), "light");
|
|
await host.api.fetch("/issues", { method: "POST" });
|
|
|
|
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
const [url, init] = fetchMock.mock.calls[0];
|
|
expect(url).toContain("/api/plugins/jira/issues");
|
|
expect(init).toEqual({ method: "POST" });
|
|
});
|
|
|
|
it("normalizes a path that doesn't start with a slash", async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(new Response(null, { status: 200 }));
|
|
global.fetch = fetchMock as unknown as typeof fetch;
|
|
|
|
const host = buildHostApi("jira", createAppStore(), "light");
|
|
await host.api.fetch("issues");
|
|
|
|
const [url] = fetchMock.mock.calls[0];
|
|
expect(url).toContain("/api/plugins/jira/issues");
|
|
});
|
|
|
|
it("exposes the host React instance and a jsx alias for React.createElement", () => {
|
|
const host = buildHostApi("jira", createAppStore(), "dark");
|
|
|
|
expect(host.React).toBe(React);
|
|
expect(host.jsx).toBe(React.createElement);
|
|
});
|
|
|
|
it("wires store.getState/setState/subscribe to the passed StoreApi", () => {
|
|
const store = createAppStore();
|
|
const getStateSpy = vi.spyOn(store, "getState");
|
|
const setStateSpy = vi.spyOn(store, "setState");
|
|
const subscribeSpy = vi.spyOn(store, "subscribe");
|
|
|
|
const host = buildHostApi("jira", store, "dark");
|
|
|
|
expect(host.store.getState()).toBe(store.getState());
|
|
host.store.setState({});
|
|
const listener = vi.fn();
|
|
const unsubscribe = host.store.subscribe(listener);
|
|
|
|
expect(getStateSpy).toHaveBeenCalled();
|
|
expect(setStateSpy).toHaveBeenCalled();
|
|
expect(subscribeSpy).toHaveBeenCalled();
|
|
unsubscribe();
|
|
});
|
|
|
|
it("exposes the requested theme and a curated ui component subset", () => {
|
|
const host = buildHostApi("jira", createAppStore(), "dark");
|
|
|
|
expect(host.theme).toBe("dark");
|
|
// Expanded primitive set for full native-feeling plugin pages.
|
|
for (const name of EXPECTED_UI_PRIMITIVES) {
|
|
expect(host.ui[name], `host.ui.${name}`).toBeDefined();
|
|
}
|
|
});
|
|
|
|
it("exposes first-party app components for native flows and page chrome", () => {
|
|
const host = buildHostApi("jira", createAppStore(), "light");
|
|
expect(host.ui.PageTopbar).toBeDefined();
|
|
expect(host.ui.TaskCreateDialog).toBeDefined();
|
|
expect(host.ui.Combobox).toBeDefined();
|
|
});
|
|
|
|
it("exposes navigate() that soft-navigates via history push/replace", () => {
|
|
const host = buildHostApi("jira", createAppStore(), "light");
|
|
const pushSpy = vi.spyOn(window.history, "pushState");
|
|
const replaceSpy = vi.spyOn(window.history, "replaceState");
|
|
|
|
host.navigate("/somewhere");
|
|
expect(pushSpy).toHaveBeenCalledWith(
|
|
expect.objectContaining({ __kandevNavigationPosition: expect.any(Number) }),
|
|
"",
|
|
"/somewhere",
|
|
);
|
|
|
|
host.navigate("/elsewhere", { replace: true });
|
|
expect(replaceSpy).toHaveBeenCalledWith(
|
|
expect.objectContaining({ __kandevNavigationPosition: expect.any(Number) }),
|
|
"",
|
|
"/elsewhere",
|
|
);
|
|
|
|
pushSpy.mockRestore();
|
|
replaceSpy.mockRestore();
|
|
});
|
|
|
|
it("exposes the backend API origin on api.baseUrl", () => {
|
|
const host = buildHostApi("jira", createAppStore(), "light");
|
|
expect(typeof host.api.baseUrl).toBe("string");
|
|
});
|
|
|
|
it("sets pluginId on the returned host api", () => {
|
|
const host = buildHostApi("jira", createAppStore(), "light");
|
|
expect(host.pluginId).toBe("jira");
|
|
});
|
|
|
|
it("routes openModal to the modal manager, scoped to this plugin's id", async () => {
|
|
const { pluginModalManager } = await import("./modal-manager");
|
|
const host = buildHostApi("jira", createAppStore(), "light");
|
|
|
|
const before = pluginModalManager.getSnapshot().length;
|
|
const handle = host.openModal({ content: () => null, title: "Test" });
|
|
|
|
const snapshot = pluginModalManager.getSnapshot();
|
|
expect(snapshot).toHaveLength(before + 1);
|
|
expect(snapshot[snapshot.length - 1]).toMatchObject({
|
|
pluginId: "jira",
|
|
options: { title: "Test" },
|
|
});
|
|
|
|
handle.close();
|
|
expect(pluginModalManager.getSnapshot()).toHaveLength(before);
|
|
});
|
|
});
|