ttrpg-tools/src/components/md-commander/hooks/useCommander.ts

444 lines
12 KiB
TypeScript
Raw Normal View History

2026-02-28 16:30:58 +08:00
import { createSignal } from "solid-js";
2026-02-28 16:28:07 +08:00
import {
MdCommanderCommand,
CommanderEntry,
CompletionItem,
} from "../types";
2026-02-28 16:49:02 +08:00
import { useDiceRoller } from "./useDiceRoller";
2026-02-28 16:28:07 +08:00
// ==================== 默认命令 ====================
export const defaultCommands: Record<string, MdCommanderCommand> = {
help: {
command: "help",
description: "显示帮助信息或特定命令的帮助",
2026-02-28 17:20:48 +08:00
parameters: [
{
name: "cmd",
2026-02-28 16:28:07 +08:00
description: "要查询的命令名",
type: "enum",
values: [], // 运行时填充
2026-02-28 17:20:48 +08:00
required: false,
2026-02-28 16:28:07 +08:00
},
2026-02-28 17:20:48 +08:00
],
2026-02-28 16:28:07 +08:00
handler: (args) => {
2026-02-28 17:20:48 +08:00
const cmdName = args.params.cmd;
if (cmdName) {
2026-02-28 16:28:07 +08:00
return {
2026-02-28 17:20:48 +08:00
message: `命令:${cmdName}\n描述${defaultCommands[cmdName]?.description || "无描述"}`,
2026-02-28 16:28:07 +08:00
type: "info",
};
}
const cmdList = Object.keys(defaultCommands).join(", ");
return {
message: `可用命令:${cmdList}`,
type: "info",
};
},
},
clear: {
command: "clear",
description: "清空命令历史",
handler: () => ({
message: "命令历史已清空",
type: "success",
}),
},
roll: {
command: "roll",
2026-02-28 16:49:02 +08:00
description: "掷骰子 - 支持骰池、修饰符和组合",
2026-02-28 17:15:41 +08:00
parameters: [
{
name: "formula",
2026-02-28 16:49:02 +08:00
description: "骰子公式,如 3d6+{d4,d8}kh2-5",
2026-02-28 16:28:07 +08:00
type: "string",
required: true,
},
2026-02-28 17:15:41 +08:00
],
2026-02-28 16:28:07 +08:00
handler: (args) => {
2026-02-28 17:15:41 +08:00
const formula = args.params.formula || "1d6";
2026-02-28 16:49:02 +08:00
const { rollSimple } = useDiceRoller();
const result = rollSimple(formula);
2026-02-28 16:28:07 +08:00
return {
2026-02-28 16:49:02 +08:00
message: result.text,
isHtml: result.isHtml,
type: result.text.startsWith("错误") ? "error" : "success",
2026-02-28 16:28:07 +08:00
};
},
},
};
// ==================== 工具函数 ====================
2026-02-28 17:15:41 +08:00
export function parseInput(input: string, commands?: Record<string, MdCommanderCommand>): {
2026-02-28 16:28:07 +08:00
command?: string;
2026-02-28 17:15:41 +08:00
params: Record<string, string>;
2026-02-28 16:28:07 +08:00
options: Record<string, string>;
2026-02-28 17:15:41 +08:00
incompleteParam?: { index: number; value: string };
2026-02-28 16:28:07 +08:00
} {
const result: {
command?: string;
2026-02-28 17:15:41 +08:00
params: Record<string, string>;
2026-02-28 16:28:07 +08:00
options: Record<string, string>;
2026-02-28 17:15:41 +08:00
incompleteParam?: { index: number; value: string };
2026-02-28 16:28:07 +08:00
} = {
2026-02-28 17:15:41 +08:00
params: {},
2026-02-28 16:28:07 +08:00
options: {},
};
const trimmed = input.trim();
if (!trimmed) return result;
const parts = trimmed.split(/\s+/);
let i = 0;
2026-02-28 17:15:41 +08:00
// 获取命令
2026-02-28 16:28:07 +08:00
if (parts[0] && !parts[0].startsWith("-")) {
result.command = parts[0];
i = 1;
}
2026-02-28 17:15:41 +08:00
// 获取命令的参数定义
const cmd = result.command ? commands?.[result.command] : undefined;
const paramDefs = cmd?.parameters || [];
let paramIndex = 0;
// 解析参数和选项
2026-02-28 16:28:07 +08:00
while (i < parts.length) {
const part = parts[i];
2026-02-28 17:15:41 +08:00
2026-02-28 16:28:07 +08:00
if (part.startsWith("--")) {
2026-02-28 17:15:41 +08:00
// 选项 --key=value 或 --key value
2026-02-28 16:28:07 +08:00
const eqIndex = part.indexOf("=");
if (eqIndex !== -1) {
const key = part.slice(2, eqIndex);
const value = part.slice(eqIndex + 1);
result.options[key] = value;
} else {
const key = part.slice(2);
if (i + 1 < parts.length && !parts[i + 1].startsWith("-")) {
result.options[key] = parts[i + 1];
i++;
} else {
2026-02-28 17:15:41 +08:00
// 未完成的选项
2026-02-28 16:28:07 +08:00
}
}
2026-02-28 17:15:41 +08:00
} else if (part.startsWith("-") && part.length === 2) {
// 短选项 -k value
const key = part.slice(1);
if (i + 1 < parts.length && !parts[i + 1].startsWith("-")) {
result.options[key] = parts[i + 1];
i++;
}
} else {
// 位置参数
if (paramIndex < paramDefs.length) {
const paramDef = paramDefs[paramIndex];
result.params[paramDef.name] = part;
paramIndex++;
}
2026-02-28 16:28:07 +08:00
}
i++;
}
2026-02-28 17:15:41 +08:00
// 检查是否有未完成的位置参数
if (paramIndex < paramDefs.length) {
const lastPart = parts[parts.length - 1];
if (lastPart && !lastPart.startsWith("-")) {
result.incompleteParam = {
index: paramIndex,
value: lastPart,
};
}
}
2026-02-28 16:28:07 +08:00
return result;
}
export function getCompletions(
input: string,
commands: Record<string, MdCommanderCommand>,
): CompletionItem[] {
const trimmed = input.trim();
if (!trimmed || /^\s*$/.test(trimmed)) {
return Object.values(commands).map((cmd) => ({
label: cmd.command,
type: "command",
description: cmd.description,
insertText: cmd.command,
}));
}
2026-02-28 17:15:41 +08:00
const parsed = parseInput(trimmed, commands);
2026-02-28 16:28:07 +08:00
if (!parsed.command || !commands[parsed.command]) {
const commandCompletions = Object.values(commands)
.filter((cmd) => cmd.command.startsWith(parsed.command || ""))
.map((cmd) => ({
label: cmd.command,
2026-02-28 16:30:58 +08:00
type: "command" as "command",
2026-02-28 16:28:07 +08:00
description: cmd.description,
insertText: cmd.command,
}));
if (commandCompletions.length > 0) {
return commandCompletions;
}
}
const cmd = commands[parsed.command!];
2026-02-28 17:15:41 +08:00
if (!cmd) return [];
// 检查是否需要参数补全
const paramDefs = cmd.parameters || [];
const usedParams = Object.keys(parsed.params);
// 如果还有未填的参数,提供参数值补全
if (paramDefs.length > usedParams.length) {
const paramDef = paramDefs[usedParams.length];
if (paramDef.type === "enum" && paramDef.values) {
const currentValue = parsed.incompleteParam?.value || "";
return paramDef.values
.filter((v) => v.startsWith(currentValue))
2026-02-28 16:28:07 +08:00
.map((v) => ({
label: v,
type: "value",
2026-02-28 17:15:41 +08:00
description: paramDef.description,
2026-02-28 16:28:07 +08:00
insertText: v,
}));
}
2026-02-28 17:15:41 +08:00
// 其他类型的参数,显示提示
if (parsed.incompleteParam) {
return [{
label: `<${paramDef.name}>`,
type: "value",
description: `${paramDef.type}${paramDef.required !== false ? " (必填)" : ""}: ${paramDef.description || ""}`,
insertText: "",
}];
}
2026-02-28 16:28:07 +08:00
}
2026-02-28 17:15:41 +08:00
// 选项补全
if (!cmd.options) return [];
2026-02-28 16:28:07 +08:00
const usedOptions = Object.keys(parsed.options);
return Object.values(cmd.options)
.filter((opt) => !usedOptions.includes(opt.option))
.map((opt) => ({
label: `--${opt.option}`,
type: "option",
description: opt.description,
insertText: `--${opt.option}=${opt.type === "boolean" ? "" : ""}`,
}));
}
export function getResultClass(
type?: "success" | "error" | "warning" | "info",
): string {
switch (type) {
case "success":
return "text-green-600";
case "error":
return "text-red-600";
case "warning":
return "text-yellow-600";
case "info":
default:
return "text-blue-600";
}
}
// ==================== Commander Hook ====================
export interface UseCommanderReturn {
inputValue: () => string;
entries: () => CommanderEntry[];
showCompletions: () => boolean;
completions: () => CompletionItem[];
selectedCompletion: () => number;
isFocused: () => boolean;
setInputValue: (v: string) => void;
setEntries: (updater: (prev: CommanderEntry[]) => CommanderEntry[]) => void;
setShowCompletions: (v: boolean) => void;
setSelectedCompletion: (v: number | ((prev: number) => number)) => void;
setIsFocused: (v: boolean) => void;
handleCommand: () => void;
updateCompletions: () => void;
acceptCompletion: () => void;
commands: Record<string, MdCommanderCommand>;
2026-02-28 17:15:41 +08:00
historyIndex: () => number;
setHistoryIndex: (v: number) => void;
commandHistory: () => string[];
navigateHistory: (direction: 'up' | 'down') => void;
2026-02-28 16:28:07 +08:00
}
export function useCommander(
customCommands?: Record<string, MdCommanderCommand>,
): UseCommanderReturn {
const [inputValue, setInputValue] = createSignal("");
const [entries, setEntries] = createSignal<CommanderEntry[]>([]);
const [showCompletions, setShowCompletions] = createSignal(false);
const [completions, setCompletions] = createSignal<CompletionItem[]>([]);
const [selectedCompletion, setSelectedCompletion] = createSignal(0);
const [isFocused, setIsFocused] = createSignal(false);
2026-02-28 17:15:41 +08:00
// 命令历史
const [commandHistory, setCommandHistory] = createSignal<string[]>([]);
const [historyIndex, setHistoryIndex] = createSignal(-1);
2026-02-28 16:28:07 +08:00
const commands = { ...defaultCommands, ...customCommands };
2026-02-28 17:20:48 +08:00
// 更新 help 命令的参数值
if (commands.help?.parameters?.[0]) {
commands.help.parameters[0].values = Object.keys(commands).filter(
2026-02-28 16:28:07 +08:00
(k) => k !== "help",
);
}
const handleCommand = () => {
const input = inputValue().trim();
if (!input) return;
2026-02-28 17:15:41 +08:00
const parsed = parseInput(input, commands);
2026-02-28 16:28:07 +08:00
const commandName = parsed.command;
const cmd = commands[commandName!];
2026-02-28 17:15:41 +08:00
let result: { message: string; type?: "success" | "error" | "warning" | "info"; isHtml?: boolean };
2026-02-28 16:28:07 +08:00
if (!cmd) {
result = { message: `未知命令:${commandName}`, type: "error" };
} else if (cmd.handler) {
try {
2026-02-28 17:15:41 +08:00
result = cmd.handler({ params: parsed.params, options: parsed.options });
2026-02-28 16:28:07 +08:00
} catch (e) {
result = {
message: `执行错误:${e instanceof Error ? e.message : String(e)}`,
type: "error",
};
}
} else {
result = { message: `命令 ${commandName} 已执行(无处理器)`, type: "info" };
}
const newEntry: CommanderEntry = {
id: Date.now().toString() + Math.random().toString(36).slice(2),
command: input,
args: parsed.options,
result,
timestamp: new Date(),
};
setEntries((prev) => [...prev, newEntry]);
2026-02-28 17:15:41 +08:00
// 添加到命令历史
setCommandHistory((prev) => [...prev, input]);
setHistoryIndex(-1); // 重置历史索引
2026-02-28 16:28:07 +08:00
setInputValue("");
setShowCompletions(false);
if (commandName === "clear") {
setEntries([]);
}
};
const updateCompletions = () => {
const input = inputValue();
const comps = getCompletions(input, commands);
setCompletions(comps);
setShowCompletions(comps.length > 0 && isFocused());
setSelectedCompletion(0);
};
const acceptCompletion = () => {
const idx = selectedCompletion();
const comp = completions()[idx];
if (!comp) return;
const input = inputValue();
2026-02-28 17:15:41 +08:00
const parsed = parseInput(input, commands);
2026-02-28 16:28:07 +08:00
let newValue: string;
if (comp.type === "command") {
newValue = comp.insertText + " ";
} else if (comp.type === "option") {
const base = parsed.command || "";
const existingOptions = Object.entries(parsed.options)
.map(([k, v]) => `--${k}=${v}`)
.join(" ");
newValue = `${base} ${existingOptions}${existingOptions ? " " : ""}${comp.insertText}`;
2026-02-28 17:15:41 +08:00
} else if (comp.type === "value") {
// 参数值补全
const cmd = parsed.command ? commands[parsed.command] : null;
const paramDefs = cmd?.parameters || [];
const usedParams = Object.keys(parsed.params);
if (paramDefs.length > usedParams.length) {
// 当前参数的补全
2026-02-28 16:28:07 +08:00
const base = parsed.command || "";
2026-02-28 17:15:41 +08:00
const existingParams = Object.values(parsed.params).join(" ");
const existingOptions = Object.entries(parsed.options)
2026-02-28 16:28:07 +08:00
.map(([k, v]) => `--${k}=${v}`)
.join(" ");
2026-02-28 17:15:41 +08:00
newValue = `${base} ${existingParams}${existingParams ? " " : ""}${comp.insertText}${existingOptions ? " " + existingOptions : ""}`;
2026-02-28 16:28:07 +08:00
} else {
newValue = input;
}
2026-02-28 17:15:41 +08:00
} else {
newValue = input;
2026-02-28 16:28:07 +08:00
}
setInputValue(newValue.trim());
setShowCompletions(false);
};
2026-02-28 17:15:41 +08:00
const navigateHistory = (direction: 'up' | 'down') => {
const history = commandHistory();
if (history.length === 0) return;
let newIndex = historyIndex();
if (direction === 'up') {
// 向上浏览历史(更早的命令)
if (newIndex < history.length - 1) {
newIndex++;
}
} else {
// 向下浏览历史(更新的命令)
if (newIndex > 0) {
newIndex--;
} else {
// 回到当前输入
setInputValue("");
setHistoryIndex(-1);
return;
}
}
setHistoryIndex(newIndex);
// 从历史末尾获取命令(最新的在前)
setInputValue(history[history.length - 1 - newIndex]);
};
2026-02-28 16:28:07 +08:00
return {
inputValue,
entries,
showCompletions,
completions,
selectedCompletion,
isFocused,
setInputValue,
setEntries,
setShowCompletions,
setSelectedCompletion,
setIsFocused,
handleCommand,
updateCompletions,
acceptCompletion,
commands,
2026-02-28 17:15:41 +08:00
historyIndex,
setHistoryIndex,
commandHistory,
navigateHistory,
2026-02-28 16:28:07 +08:00
};
}