wip: command template csv

This commit is contained in:
2026-03-01 11:41:54 +08:00
parent 660f820f4c
commit aac7fe575a
4 changed files with 75 additions and 1 deletions
+1
View File
@@ -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;
}