refactor: impl

This commit is contained in:
2026-03-01 12:41:03 +08:00
parent c2c3956a82
commit 5d026dfd80
7 changed files with 202 additions and 149 deletions
@@ -0,0 +1,123 @@
import { createStore } from "solid-js/store";
import type { MdCommanderCommand, MdCommanderCommandMap } from "../types";
import { setupHelpCommand, clearCommand, rollCommand, trackCommand, untrackCommand, listTrackCommand } from "../commands";
const defaultCommands: MdCommanderCommandMap = {
help: setupHelpCommand({}),
clear: clearCommand,
roll: rollCommand,
track: trackCommand,
untrack: untrackCommand,
list: listTrackCommand,
};
const [commandsStore, setCommandsStore] = createStore<{
commands: MdCommanderCommandMap;
initialized: boolean;
}>({
commands: { ...defaultCommands },
initialized: false,
});
/**
* 初始化命令(包括 help 命令的动态更新)
*/
export function initializeCommands(customCommands?: MdCommanderCommandMap): MdCommanderCommandMap {
const commands = { ...defaultCommands, ...customCommands };
// 更新 help 命令的命令列表
commands.help = setupHelpCommand(commands);
return commands;
}
/**
* 注册命令到 store
*/
export function registerCommands(customCommands?: MdCommanderCommandMap): void {
const commands = initializeCommands(customCommands);
setCommandsStore({ commands, initialized: true });
}
/**
* 从 store 获取命令
*/
export function getCommands(): MdCommanderCommandMap {
return commandsStore.commands;
}
/**
* 检查命令是否已初始化
*/
export function isCommandsInitialized(): boolean {
return commandsStore.initialized;
}
/**
* 获取单个命令
*/
export function getCommand(name: string): MdCommanderCommand | undefined {
return commandsStore.commands[name];
}
/**
* 从 CSV 文件加载命令模板并更新命令定义
*/
export async function loadCommandTemplatesFromCSV(
csvPaths: string | string[],
resolvePath: (base: string, path: string) => string,
articlePath: string
): Promise<void> {
const paths = Array.isArray(csvPaths) ? csvPaths : [csvPaths];
for (const path of paths) {
try {
const { loadCSV } = await import("../../utils/csv-loader");
const csv = await loadCSV<CommandTemplateRow>(resolvePath(articlePath, path));
// 按命令分组模板
const templatesByCommand = new Map<string, CommandTemplateRow[]>();
for (const row of csv) {
if (!row.command || !row.label || !row.insertedText) continue;
if (!templatesByCommand.has(row.command)) {
templatesByCommand.set(row.command, []);
}
templatesByCommand.get(row.command)!.push(row);
}
// 为每个命令添加模板
setCommandsStore("commands", (prev) => {
const updated = { ...prev };
for (const [commandName, rows] of templatesByCommand.entries()) {
const cmd = updated[commandName];
if (!cmd || !cmd.parameters) continue;
const templates = rows.map((row) => ({
label: row.label,
description: row.description || "",
insertText: row.insertedText,
}));
// 为每个参数添加模板
updated[commandName] = {
...cmd,
parameters: cmd.parameters.map((param) => ({
...param,
templates: param.templates ? [...param.templates, ...templates] : templates,
})),
};
}
return updated;
});
} catch (error) {
console.warn(`Error loading command templates from ${path}:`, error);
}
}
}
interface CommandTemplateRow {
command: string;
parameter: string;
label: string;
description: string;
insertedText: string;
}