repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/ui/web/components/composer.js.ts

Raw
File outline
import type { UiCommandInfo } from "../../protocol";

export type ComposerKeyAction =
	| "submit"
	| "newline"
	| "abort"
	| "toggle-expand"
	| "cycle-model"
	| "menu-next"
	| "menu-prev"
	| "menu-select"
	| "none";

export type ComposerKeyInput = {
	key: string;
	ctrlKey?: boolean;
	shiftKey?: boolean;
	menuVisible?: boolean;
};

export function filterCommandItems(
	commands: UiCommandInfo[],
	input: string,
): UiCommandInfo[] {
	const prefix = input.startsWith("/") ? input.slice(1).toLowerCase() : "";
	return commands.filter((item) => item.name.toLowerCase().startsWith(prefix));
}

export function moveMenuIndex(
	current: number,
	delta: number,
	count: number,
): number {
	if (count <= 0) return 0;
	return (current + delta + count) % count;
}

export function composerKeyAction(input: ComposerKeyInput): ComposerKeyAction {
	const normalized = input.key.toLowerCase();
	if (input.menuVisible && input.key === "ArrowDown") return "menu-next";
	if (input.menuVisible && input.key === "ArrowUp") return "menu-prev";
	if (input.menuVisible && input.key === "Tab") return "menu-select";
	if (input.ctrlKey && normalized === "o") return "toggle-expand";
	if (input.ctrlKey && normalized === "p") return "cycle-model";
	if (input.key === "Escape") return "abort";
	if (input.key === "Enter" && input.shiftKey) return "newline";
	if (input.key === "Enter") return "submit";
	return "none";
}

export function renderComposerComponentJs(): string {
	return `function filterCommandItems(commands, input) {
	const prefix = input.startsWith('/') ? input.slice(1).toLowerCase() : '';
	return commands.filter(item => item.name.toLowerCase().startsWith(prefix));
}
function moveMenuIndex(current, delta, count) {
	if (count <= 0) return 0;
	return (current + delta + count) % count;
}
function composerKeyAction(input) {
	const normalized = input.key.toLowerCase();
	if (input.menuVisible && input.key === 'ArrowDown') return 'menu-next';
	if (input.menuVisible && input.key === 'ArrowUp') return 'menu-prev';
	if (input.menuVisible && input.key === 'Tab') return 'menu-select';
	if (input.ctrlKey && normalized === 'o') return 'toggle-expand';
	if (input.ctrlKey && normalized === 'p') return 'cycle-model';
	if (input.key === 'Escape') return 'abort';
	if (input.key === 'Enter' && input.shiftKey) return 'newline';
	if (input.key === 'Enter') return 'submit';
	return 'none';
}
function renderCommandMenu() {
	menu.innerHTML = '';
	if (!input.value.startsWith('/')) {
		menu.hidden = true;
		selectedCommandIndex = 0;
		return;
	}
	visibleCommands = filterCommandItems(commands, input.value);
	menu.hidden = visibleCommands.length === 0;
	for (const [index, command] of visibleCommands.entries()) {
		const item = document.createElement('button');
		item.type = 'button';
		item.className = 'ui-command-item' + (index === selectedCommandIndex ? ' is-selected' : '');
		item.textContent = '/' + command.name + (command.description ? ' — ' + command.description : '');
		item.addEventListener('click', () => selectCommand(index));
		menu.append(item);
	}
}
function selectCommand(index) {
	const command = visibleCommands[index];
	if (!command) return;
	input.value = '/' + command.name + ' ';
	input.focus();
	menu.hidden = true;
}
`;
}