From aac7fe575a3803ac3cc82a562260fd647f290498 Mon Sep 17 00:00:00 2001 From: hypercross Date: Sun, 1 Mar 2026 11:41:54 +0800 Subject: [PATCH] wip: command template csv --- content/commander-test.md | 2 +- content/commands.csv | 4 ++ src/components/md-commander/types.ts | 1 + .../md-commander/utils/commandTemplates.ts | 69 +++++++++++++++++++ 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 content/commands.csv create mode 100644 src/components/md-commander/utils/commandTemplates.ts diff --git a/content/commander-test.md b/content/commander-test.md index 45e25f2..cbd5b17 100644 --- a/content/commander-test.md +++ b/content/commander-test.md @@ -1,3 +1,3 @@ # Commander Test -:md-commander[] \ No newline at end of file +:md-commander[./commands.csv] \ No newline at end of file diff --git a/content/commands.csv b/content/commands.csv new file mode 100644 index 0000000..98899e7 --- /dev/null +++ b/content/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]" \ No newline at end of file diff --git a/src/components/md-commander/types.ts b/src/components/md-commander/types.ts index a23497e..86f876d 100644 --- a/src/components/md-commander/types.ts +++ b/src/components/md-commander/types.ts @@ -5,6 +5,7 @@ export interface MdCommanderProps { class?: string; height?: string; commands?: Record; + commandTemplates?: string | string[]; // CSV 文件路径或路径数组 } export interface MdCommanderCommandMap { diff --git a/src/components/md-commander/utils/commandTemplates.ts b/src/components/md-commander/utils/commandTemplates.ts new file mode 100644 index 0000000..2eabe48 --- /dev/null +++ b/src/components/md-commander/utils/commandTemplates.ts @@ -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 { + const paths = Array.isArray(csvPaths) ? csvPaths : [csvPaths]; + + for (const path of paths) { + try { + const csv = await loadCSV(path); + + // 按命令分组模板 + const templatesByCommand = new Map(); + 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; +}