Official
palette
Built-in command palette (⌘⇧P) and chat search (⌘P).
The app opens the listing; nothing installs until an agent in your Plugins workspace has read the files and you enable the plugin. In a terminal: dvg install divergence/palette@0.2.4
Permissions in 0.2.4
Files
view.ts6.7 KB
// Render the palette overlay without starting work; handlers run scoped effects through their view context.
// Typed factories avoid HTM whitespace changes when Prettier formats a template.
import { LIST, h } from "divergence";
import type { DivProps, Element } from "divergence";
import type { EffectViewContext } from "divergence/effect";
import type { Controller, Row } from "./controller.ts";
import { VISIBLE, VISIBLE_CHATS, modeOf, move, shownRows } from "./model.ts";
import { CHAT_ROW, chatRow } from "../sdk/ui/chat-row.ts";
const column = { display: "flex", flexDirection: "column" } satisfies DivProps;
const row = { display: "flex", flexDirection: "row", alignItems: "center" } satisfies DivProps;
const fill = { flexGrow: 1, flexShrink: 1, flexBasis: "0%", minWidth: 0 } satisfies DivProps;
const clickable = { group: "hover" } satisfies DivProps;
// A command's row height; a chat's is two lines (`CHAT_ROW`).
const ROW = 34;
function titleOf(entry: Row) {
// The positions are UTF-16 offsets, as the matcher counts them: a
// character outside the basic plane (an emoji) takes two.
const marked = new Set(entry.positions);
const parts = [];
let run = "";
let bold = false;
let offset = 0;
for (const char of entry.title) {
const hit = marked.has(offset);
offset += char.length;
if (hit !== bold && run) {
parts.push({ text: run, bold });
run = "";
}
bold = hit;
run += char;
}
if (run) parts.push({ text: run, bold });
return h(
"div",
{ ...row, ...fill, gap: 0, whiteSpace: "nowrap", overflow: "hidden" },
parts.map((part, index) =>
h(
"text",
{
key: index,
style: "body",
size: 13,
weight: part.bold ? 650 : 400,
tone: part.bold ? "foreground" : "muted",
},
part.text,
),
),
);
}
const rowId = (entry: Row) => `palette-row-${entry.kind}-${entry.id}`;
// Makes each row's element, or gives back the one kept in `state.drawn`.
function keeper(palette: Controller, ctx: EffectViewContext<object>, list: Row[], now: number) {
const { state } = palette;
// What every row shares: its place in the list, the hover wash and the click that picks it.
const frame = (entry: Row, index: number) =>
({
id: rowId(entry),
key: entry.id,
...clickable,
paddingX: 10,
paddingY: 6,
borderRadius: "sm",
hover: { background: "hover" },
onClick: () => {
state.selected = index;
ctx.run(palette.pick(list, index));
},
}) satisfies DivProps;
function rowOf(entry: Row, index: number): Element {
if (entry.chat) return chatRow(frame(entry, index), titleOf(entry), entry.chat, entry.detail, now);
return h(
"div",
{ ...frame(entry, index), ...row, gap: 10, height: ROW },
titleOf(entry),
entry.detail ? h("text", { style: "body", size: 11, tone: "faint", truncate: true }, entry.detail) : null,
entry.conflict && !entry.key
? h(
"div",
{ ...row, gap: 6, flexShrink: 0, stopPropagation: true },
h("badge", { variant: "warning" }, "Conflict"),
h("button", {
id: `palette-assign-${entry.id}`,
label: "Assign…",
variant: "ghost",
small: true,
tooltip: `${entry.conflict.label} is used by ${entry.conflict.withTitle}`,
onClick: () => ctx.run(palette.assign(entry.id)),
}),
)
: null,
entry.key ? h("text", { keys: true }, entry.key) : null,
);
}
return (entry: Row, index: number) => {
let element = state.drawn.get(entry);
if (!element) state.drawn.set(entry, (element = rowOf(entry, index)));
return element;
};
}
export function render(palette: Controller, ctx: EffectViewContext<object>) {
const { state } = palette;
const list = palette.rows();
const selection = () => Math.min(state.selected, Math.max(0, list.length - 1));
const selected = selection();
const now = Date.now();
const typed = (value: string) => {
state.query = value;
state.selected = 0;
state.top = 0;
if (state.open === "chats") {
const mode = modeOf(value, state.providers);
if (mode.mode === "provider") ctx.run(palette.search(mode.provider, mode.query));
}
ctx.update();
};
// A chat's row is two lines tall, so fewer of them show at once.
const visible = list[0]?.kind === "chat" ? VISIBLE_CHATS : VISIBLE;
const step = (delta: number) => {
state.selected = move(selection(), list.length, delta);
ctx.update();
};
const pick = (index = selection()) => ctx.run(palette.pick(list, index));
const kept = keeper(palette, ctx, list, now);
const [lo, hi] = shownRows(list.length, state.top, selected, visible);
const chosen = list[selected];
const placeholder =
state.open === "commands" ? "Type a command" : "Search chats by title or workspace; type > for commands";
const empty = state.open === "commands" ? "No command matches." : "Nothing matches.";
const keys = {
up: () => step(-1),
down: () => step(1),
pageup: () => step(-visible),
pagedown: () => step(visible),
escape: palette.close,
};
return h(
"overlay",
{ id: "palette", open: !!state.open, width: 620, top: 0.12, keys, onDismiss: palette.close },
h(
"div",
// No divider: the input and the rows share the panel, the rows'
// selection fill is enough to tell them apart.
{ ...column, gap: 2, padding: 2 },
h("input", {
id: "palette-input",
value: state.query,
placeholder,
ghost: true,
onChange: (event) => typed(event.value),
onSubmit: () => pick(),
}),
list.length
? h(
"div",
{ ...column, height: Math.min(list.length, visible) * (chosen?.chat ? CHAT_ROW : ROW) },
// The wheel and the trackpad scroll the list where it is; the
// arrow keys move the selection, and the list brings it into view.
h(
"scroller",
{
id: "palette-rows",
...LIST,
reveal: chosen && rowId(chosen),
rowsBefore: lo || undefined,
rowsAfter: list.length - hi || undefined,
onRange: (event) => {
state.top = event.first;
ctx.update();
},
},
chosen ? h("div", { follows: rowId(chosen), width: "100%", height: "100%", background: "accent", borderRadius: "sm" }) : null,
list.slice(lo, hi).map((entry, index) => kept(entry, lo + index)),
),
)
: h("div", { ...row, padding: 10 }, h("text", { style: "body", size: 12, tone: "muted" }, empty)),
),
);
}
No comments yet.