wip: command template csv
This commit is contained in:
parent
660f820f4c
commit
aac7fe575a
|
|
@ -1,3 +1,3 @@
|
|||
# Commander Test
|
||||
|
||||
:md-commander[]
|
||||
:md-commander[./commands.csv]
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
command,parameter,label,description,insertedText
|
||||
track,emmet,monk,破戒佛爷,"破戒佛爷.bandit.monk[gd=4/4,str=14/14,cla=10,spi=8]"
|
||||
track,emmet,bandit,龙虎寨山贼,"龙虎寨山贼.bandit[gd=2/2,str=10/10,cla=10,spi=10]"
|
||||
track,emmet,khitan,契丹护卫,"契丹护卫.khitan.guard[gd=4/4,ap=2,str=14/14,cla=14,spi=14]"
|
||||
|
|
|
@ -5,6 +5,7 @@ export interface MdCommanderProps {
|
|||
class?: string;
|
||||
height?: string;
|
||||
commands?: Record<string, MdCommanderCommand>;
|
||||
commandTemplates?: string | string[]; // CSV 文件路径或路径数组
|
||||
}
|
||||
|
||||
export interface MdCommanderCommandMap {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,69 @@
|
|||
import type { MdCommanderCommandMap, MdCommanderTemplate } from "../types";
|
||||
import { loadCSV } from "../../utils/csv-loader";
|
||||
|
||||
export interface CommandTemplateRow {
|
||||
command: string;
|
||||
parameter: string;
|
||||
label: string;
|
||||
description: string;
|
||||
insertedText: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 CSV 内容构建命令模板
|
||||
*/
|
||||
function buildTemplatesFromRows(rows: CommandTemplateRow[]): MdCommanderTemplate[] {
|
||||
return rows.map(row => ({
|
||||
label: row.label,
|
||||
description: row.description || "",
|
||||
insertText: row.insertedText,
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 CSV 文件加载命令模板并更新命令定义
|
||||
*/
|
||||
export async function loadCommandTemplates(
|
||||
commands: MdCommanderCommandMap,
|
||||
csvPaths: string | string[]
|
||||
): Promise<MdCommanderCommandMap> {
|
||||
const paths = Array.isArray(csvPaths) ? csvPaths : [csvPaths];
|
||||
|
||||
for (const path of paths) {
|
||||
try {
|
||||
const csv = await loadCSV<CommandTemplateRow>(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);
|
||||
}
|
||||
|
||||
// 为每个命令添加模板
|
||||
for (const [commandName, rows] of templatesByCommand.entries()) {
|
||||
const cmd = commands[commandName];
|
||||
if (!cmd || !cmd.parameters) continue;
|
||||
|
||||
const templates = buildTemplatesFromRows(rows);
|
||||
|
||||
// 为每个参数添加模板
|
||||
for (const param of cmd.parameters) {
|
||||
if (!param.templates) {
|
||||
param.templates = [];
|
||||
}
|
||||
// 添加新模板
|
||||
param.templates.push(...templates);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn(`Error loading command templates from ${path}:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
return commands;
|
||||
}
|
||||
Loading…
Reference in New Issue