Skip to content

Commit cf6cc10

Browse files
feat: add command palette (anomalyco#257)
1 parent bf876e6 commit cf6cc10

11 files changed

Lines changed: 1120 additions & 140 deletions

File tree

‎packages/app/src/app/app.tsx‎

Lines changed: 387 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { createSignal } from "solid-js";
2+
3+
import type { CommandRegistryItem } from "./types";
4+
5+
type RegisteredCommand = CommandRegistryItem & { key: string };
6+
7+
export function createCommandRegistry() {
8+
const [items, setItems] = createSignal<RegisteredCommand[]>([]);
9+
let counter = 0;
10+
11+
const registerCommand = (command: CommandRegistryItem) => {
12+
const key = `${command.id}-${counter++}`;
13+
const entry: RegisteredCommand = { ...command, key };
14+
setItems((prev: RegisteredCommand[]) => [...prev, entry]);
15+
return () =>
16+
setItems((prev: RegisteredCommand[]) => prev.filter((item) => item.key !== key));
17+
};
18+
19+
const registerCommands = (commands: CommandRegistryItem[]) => {
20+
const cleanups = commands.map(registerCommand);
21+
return () => {
22+
for (const cleanup of cleanups) cleanup();
23+
};
24+
};
25+
26+
return {
27+
items,
28+
registerCommand,
29+
registerCommands,
30+
};
31+
}
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
import { For, Show, createEffect, createMemo, createSignal, onCleanup } from "solid-js";
2+
import { Search, X } from "lucide-solid";
3+
4+
import Button from "./button";
5+
import { formatKeybindLabel } from "../utils/keybinds";
6+
7+
export type PaletteItem = {
8+
id: string;
9+
title: string;
10+
description?: string;
11+
category?: string;
12+
keybind?: string;
13+
onSelect: () => void;
14+
onHighlight?: () => void;
15+
};
16+
17+
export type PaletteGroup = {
18+
id: string;
19+
title: string;
20+
items: PaletteItem[];
21+
};
22+
23+
export type CommandPaletteModalProps = {
24+
open: boolean;
25+
mode: "command" | "file";
26+
query: string;
27+
setQuery: (value: string) => void;
28+
groups: PaletteGroup[];
29+
onClose: () => void;
30+
};
31+
32+
export default function CommandPaletteModal(props: CommandPaletteModalProps) {
33+
let searchInputRef: HTMLInputElement | undefined;
34+
const [activeIndex, setActiveIndex] = createSignal(0);
35+
36+
const filteredGroups = createMemo(() => {
37+
const query = props.query.trim().toLowerCase();
38+
if (!query) return props.groups;
39+
return props.groups
40+
.map((group: PaletteGroup) => {
41+
const items = group.items.filter((item) => {
42+
const haystack = `${item.title} ${item.description ?? ""} ${item.category ?? ""}`.toLowerCase();
43+
return haystack.includes(query);
44+
});
45+
return { ...group, items };
46+
})
47+
.filter((group) => group.items.length > 0);
48+
});
49+
50+
const flattenedItems = createMemo(() => filteredGroups().flatMap((group) => group.items));
51+
52+
createEffect(() => {
53+
if (props.open) {
54+
setActiveIndex(0);
55+
requestAnimationFrame(() => searchInputRef?.focus());
56+
}
57+
});
58+
59+
createEffect(() => {
60+
const item = flattenedItems()[activeIndex()];
61+
item?.onHighlight?.();
62+
});
63+
64+
createEffect(() => {
65+
if (!props.open) return;
66+
const onKeyDown = (event: KeyboardEvent) => {
67+
if (!props.open) return;
68+
if (event.key === "Escape") {
69+
event.preventDefault();
70+
event.stopPropagation();
71+
props.onClose();
72+
return;
73+
}
74+
if (event.key === "ArrowDown") {
75+
event.preventDefault();
76+
event.stopPropagation();
77+
setActiveIndex((current: number) => Math.min(current + 1, flattenedItems().length - 1));
78+
return;
79+
}
80+
if (event.key === "ArrowUp") {
81+
event.preventDefault();
82+
event.stopPropagation();
83+
setActiveIndex((current: number) => Math.max(current - 1, 0));
84+
return;
85+
}
86+
if (event.key === "Enter") {
87+
const item = flattenedItems()[activeIndex()];
88+
if (!item) return;
89+
event.preventDefault();
90+
event.stopPropagation();
91+
item.onSelect();
92+
props.onClose();
93+
}
94+
};
95+
window.addEventListener("keydown", onKeyDown, true);
96+
onCleanup(() => window.removeEventListener("keydown", onKeyDown, true));
97+
});
98+
99+
const placeholder = () =>
100+
props.mode === "file" ? "Search files" : "Search commands or files";
101+
102+
return (
103+
<Show when={props.open}>
104+
<div class="fixed inset-0 z-50 bg-gray-1/60 backdrop-blur-sm flex items-start justify-center p-4 overflow-y-auto">
105+
<div class="bg-gray-2 border border-gray-6/70 w-full max-w-xl rounded-2xl shadow-2xl overflow-hidden max-h-[calc(100vh-2rem)] flex flex-col">
106+
<div class="p-5 border-b border-gray-6/50 flex items-center gap-3">
107+
<Search size={16} class="text-gray-10" />
108+
<input
109+
ref={(el) => (searchInputRef = el)}
110+
type="text"
111+
value={props.query}
112+
onInput={(event: InputEvent) => props.setQuery((event.currentTarget as HTMLInputElement).value)}
113+
placeholder={placeholder()}
114+
class="flex-1 bg-transparent text-sm text-gray-12 placeholder-gray-7 focus:outline-none"
115+
/>
116+
<Button variant="ghost" class="!p-2 rounded-full" onClick={props.onClose}>
117+
<X size={16} />
118+
</Button>
119+
</div>
120+
121+
<div class="p-4 space-y-5 overflow-y-auto">
122+
<Show
123+
when={filteredGroups().length}
124+
fallback={<div class="text-sm text-gray-9">No results found.</div>}
125+
>
126+
<For each={filteredGroups()}>
127+
{(group: PaletteGroup) => (
128+
<div class="space-y-2">
129+
<div class="text-[11px] font-semibold uppercase tracking-wider text-gray-9">
130+
{group.title}
131+
</div>
132+
<div class="space-y-1">
133+
<For each={group.items}>
134+
{(item: PaletteItem) => {
135+
const isActive = createMemo(
136+
() => flattenedItems()[activeIndex()]?.id === item.id,
137+
);
138+
return (
139+
<button
140+
type="button"
141+
class={`w-full flex items-start justify-between gap-3 rounded-xl px-3 py-2 text-left transition-colors ${
142+
isActive()
143+
? "bg-gray-12/10 text-gray-12"
144+
: "text-gray-11 hover:bg-gray-12/5"
145+
}`}
146+
onMouseEnter={() => {
147+
const index = flattenedItems().findIndex((entry) => entry.id === item.id);
148+
if (index >= 0) setActiveIndex(index);
149+
item.onHighlight?.();
150+
}}
151+
onMouseDown={(event: MouseEvent) => {
152+
event.preventDefault();
153+
item.onSelect();
154+
props.onClose();
155+
}}
156+
>
157+
<div class="min-w-0">
158+
<div class="text-sm font-medium text-gray-12 truncate">{item.title}</div>
159+
<Show when={item.description}>
160+
<div class="text-[11px] text-gray-9 truncate">{item.description}</div>
161+
</Show>
162+
</div>
163+
<Show when={item.keybind}>
164+
<span class="text-[10px] text-gray-9 uppercase tracking-wide border border-gray-6/60 rounded-lg px-2 py-1">
165+
{formatKeybindLabel(item.keybind!)}
166+
</span>
167+
</Show>
168+
</button>
169+
);
170+
}}
171+
</For>
172+
</div>
173+
</div>
174+
)}
175+
</For>
176+
</Show>
177+
</div>
178+
</div>
179+
</div>
180+
</Show>
181+
);
182+
}

‎packages/app/src/app/components/session/composer.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type ComposerProps = {
1313
onSend: () => void;
1414
commandMatches: CommandItem[];
1515
onRunCommand: (commandId: string) => void;
16+
onAutocompleteCommand: (commandId: string) => void;
1617
selectedModelLabel: string;
1718
onModelClick: () => void;
1819
showNotionBanner: boolean;
@@ -78,10 +79,9 @@ export default function Composer(props: ComposerProps) {
7879
}
7980
if (event.key === "Tab") {
8081
event.preventDefault();
81-
// maybe autocomplete?
8282
const active = matches[commandIndex()] ?? matches[0];
83-
if (active) {
84-
props.onRunCommand(active.id);
83+
if (active) {
84+
props.onAutocompleteCommand(active.id);
8585
}
8686
return;
8787
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import { For, Show, createEffect, createMemo, createSignal, onCleanup } from "solid-js";
2+
3+
import Button from "./button";
4+
import { formatKeybindLabel, keybindFromEvent } from "../utils/keybinds";
5+
6+
export type KeybindSetting = {
7+
id: string;
8+
title: string;
9+
category?: string;
10+
description?: string;
11+
defaultKeybind?: string | null;
12+
overrideKeybind?: string | null;
13+
conflicts?: string[];
14+
};
15+
16+
export type SettingsKeybindsProps = {
17+
items: KeybindSetting[];
18+
onOverride: (id: string, keybind: string | null) => void;
19+
onReset: (id: string) => void;
20+
onResetAll: () => void;
21+
};
22+
23+
export default function SettingsKeybinds(props: SettingsKeybindsProps) {
24+
const [capturingId, setCapturingId] = createSignal<string | null>(null);
25+
26+
const grouped = createMemo(() => {
27+
const groups = new Map<string, KeybindSetting[]>();
28+
for (const item of props.items) {
29+
const category = item.category ?? "General";
30+
const list = groups.get(category) ?? [];
31+
list.push(item);
32+
groups.set(category, list);
33+
}
34+
return Array.from(groups.entries()).map(([title, items]) => ({
35+
title,
36+
items: items.sort((a, b) => a.title.localeCompare(b.title)),
37+
}));
38+
});
39+
40+
createEffect(() => {
41+
if (!capturingId()) return;
42+
const onKeyDown = (event: KeyboardEvent) => {
43+
if (!capturingId()) return;
44+
if (event.key === "Escape") {
45+
event.preventDefault();
46+
event.stopPropagation();
47+
setCapturingId(null);
48+
return;
49+
}
50+
const keybind = keybindFromEvent(event);
51+
if (!keybind) return;
52+
event.preventDefault();
53+
event.stopPropagation();
54+
props.onOverride(capturingId()!, keybind);
55+
setCapturingId(null);
56+
};
57+
window.addEventListener("keydown", onKeyDown, true);
58+
onCleanup(() => window.removeEventListener("keydown", onKeyDown, true));
59+
});
60+
61+
const hasOverrides = createMemo(() =>
62+
props.items.some((item) => item.overrideKeybind && item.overrideKeybind !== item.defaultKeybind),
63+
);
64+
65+
return (
66+
<div class="bg-gray-2/30 border border-gray-6/50 rounded-2xl p-5 space-y-4">
67+
<div class="flex items-center justify-between">
68+
<div>
69+
<div class="text-sm font-medium text-gray-12">Keybinds</div>
70+
<div class="text-xs text-gray-10">Customize global and session shortcuts.</div>
71+
</div>
72+
<Button variant="outline" disabled={!hasOverrides()} onClick={props.onResetAll}>
73+
Reset all
74+
</Button>
75+
</div>
76+
77+
<div class="space-y-6">
78+
<For each={grouped()}>
79+
{(group: { title: string; items: KeybindSetting[] }) => (
80+
<div class="space-y-3">
81+
<div class="text-xs font-semibold uppercase tracking-wider text-gray-9">{group.title}</div>
82+
<div class="space-y-2">
83+
<For each={group.items}>
84+
{(item: KeybindSetting) => {
85+
const activeKeybind = () => item.overrideKeybind ?? item.defaultKeybind ?? null;
86+
const conflictText = () =>
87+
item.conflicts?.length
88+
? `Conflicts with ${item.conflicts.join(", ")}`
89+
: null;
90+
return (
91+
<div class="bg-gray-1 border border-gray-6 rounded-xl px-4 py-3 flex items-center justify-between gap-4">
92+
<div class="min-w-0">
93+
<div class="text-sm text-gray-12">{item.title}</div>
94+
<Show when={item.description}>
95+
<div class="text-xs text-gray-8 mt-1">{item.description}</div>
96+
</Show>
97+
<Show when={conflictText()}>
98+
<div class="text-xs text-amber-11 mt-1">{conflictText()}</div>
99+
</Show>
100+
</div>
101+
<div class="flex items-center gap-2 shrink-0">
102+
<div class="text-xs text-gray-8 font-mono px-2 py-1 border border-gray-6 rounded-lg">
103+
{activeKeybind() ? formatKeybindLabel(activeKeybind()!) : "Unassigned"}
104+
</div>
105+
<Button
106+
variant={capturingId() === item.id ? "secondary" : "outline"}
107+
class="text-xs h-8 px-3"
108+
onClick={() =>
109+
setCapturingId(capturingId() === item.id ? null : item.id)
110+
}
111+
>
112+
{capturingId() === item.id ? "Press keys" : "Record"}
113+
</Button>
114+
<Button
115+
variant="ghost"
116+
class="text-xs h-8 px-3"
117+
disabled={!item.overrideKeybind}
118+
onClick={() => props.onReset(item.id)}
119+
>
120+
Reset
121+
</Button>
122+
</div>
123+
</div>
124+
);
125+
}}
126+
</For>
127+
</div>
128+
</div>
129+
)}
130+
</For>
131+
</div>
132+
</div>
133+
);
134+
}

‎packages/app/src/app/constants.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const VARIANT_PREF_KEY = "openwork.modelVariant";
77
export const DEMO_MODE_PREF_KEY = "openwork.demoMode";
88
export const DEMO_SEQUENCE_PREF_KEY = "openwork.demoSequence";
99
export const LANGUAGE_PREF_KEY = "openwork.language";
10+
export const KEYBIND_PREF_KEY = "openwork.keybinds";
1011

1112
export const DEFAULT_MODEL: ModelRef = {
1213
providerID: "opencode",

0 commit comments

Comments
 (0)