refactor: impl
This commit is contained in:
parent
5d026dfd80
commit
62780e3e56
|
|
@ -11,38 +11,6 @@ export const trackCommand: MdCommanderCommand = {
|
|||
description: "Emmet 格式的追踪项目:tag#class1.class2[attr=value]",
|
||||
type: "string",
|
||||
required: true,
|
||||
templates: [
|
||||
{
|
||||
label: "NPC",
|
||||
description: "基础 NPC 模板",
|
||||
insertText: "npc",
|
||||
},
|
||||
{
|
||||
label: "Enemy",
|
||||
description: "敌人模板",
|
||||
insertText: "enemy",
|
||||
},
|
||||
{
|
||||
label: "Ally",
|
||||
description: "盟友模板",
|
||||
insertText: "ally",
|
||||
},
|
||||
{
|
||||
label: "Vehicle",
|
||||
description: "载具模板",
|
||||
insertText: "vehicle",
|
||||
},
|
||||
{
|
||||
label: "Location",
|
||||
description: "地点模板",
|
||||
insertText: "location",
|
||||
},
|
||||
{
|
||||
label: "Item",
|
||||
description: "物品模板",
|
||||
insertText: "item",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
handler: (args, commands) => {
|
||||
|
|
|
|||
|
|
@ -9,6 +9,15 @@ import type {
|
|||
MdCommanderCommandMap,
|
||||
} from "../types";
|
||||
import { parseInput, getCompletions } from "./completions";
|
||||
import {
|
||||
addTrackerItem,
|
||||
getTrackerHistory,
|
||||
getTrackerItems,
|
||||
moveTrackerItem, removeTrackerClass,
|
||||
removeTrackerItem,
|
||||
updateTrackerAttribute,
|
||||
updateTrackerClasses
|
||||
} from "../stores";
|
||||
|
||||
// ==================== Commander Hook ====================
|
||||
|
||||
|
|
@ -196,16 +205,6 @@ export function useCommander(initialCommands?: MdCommanderCommandMap): UseComman
|
|||
};
|
||||
|
||||
// ==================== Tracker 操作 ====================
|
||||
const {
|
||||
getTrackerItems,
|
||||
getTrackerHistory,
|
||||
addTrackerItem,
|
||||
removeTrackerItem,
|
||||
updateTrackerAttribute,
|
||||
updateTrackerClasses,
|
||||
moveTrackerItem,
|
||||
removeTrackerClass,
|
||||
} = require("../stores") as typeof import("../stores");
|
||||
|
||||
const getEmmetFromIndex = (index: number): string | null => {
|
||||
const items = getTrackerItems();
|
||||
|
|
@ -272,7 +271,7 @@ export function useCommander(initialCommands?: MdCommanderCommandMap): UseComman
|
|||
updateTrackerClassesByIndex,
|
||||
moveTrackerItem,
|
||||
moveTrackerItemByIndex,
|
||||
removeTrackerItemClass,
|
||||
removeTrackerItemClass: removeTrackerClass,
|
||||
removeTrackerItemClassByIndex,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,8 +6,8 @@ import { CommanderEntries } from "./CommanderEntries";
|
|||
import { TrackerView } from "./TrackerView";
|
||||
import { TabBar } from "./TabBar";
|
||||
import type { MdCommanderProps } from "./types";
|
||||
import { loadElementSrc, resolvePath } from "../utils/path";
|
||||
import { initializeCommands, loadCommandTemplatesFromCSV } from "./stores/commandsStore";
|
||||
import { loadElementSrc } from "../utils/path";
|
||||
import { initializeCommands, loadCommandTemplatesFromCSV } from "./stores";
|
||||
|
||||
customElement<MdCommanderProps>(
|
||||
"md-commander",
|
||||
|
|
@ -21,15 +21,13 @@ customElement<MdCommanderProps>(
|
|||
const commander = useCommander(commands);
|
||||
|
||||
// 加载 CSV 模板
|
||||
createEffect(
|
||||
on(() => props.commandTemplates, async (csvPaths) => {
|
||||
if (!csvPaths || !rawSrc) return;
|
||||
await loadCommandTemplatesFromCSV(csvPaths, resolvePath, articlePath);
|
||||
createEffect( async () => {
|
||||
if (!rawSrc || !rawSrc) return;
|
||||
await loadCommandTemplatesFromCSV(rawSrc, articlePath);
|
||||
// 更新 commander 中的命令(从 store 获取)
|
||||
const { getCommands } = await import("./stores/commandsStore");
|
||||
const {getCommands} = await import("./stores/commandsStore");
|
||||
commander.setCommands(getCommands());
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
const handleKeyDown = (e: KeyboardEvent) => {
|
||||
if (commander.showCompletions() && commander.completions().length > 0) {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
import { createStore } from "solid-js/store";
|
||||
import type { MdCommanderCommand, MdCommanderCommandMap } from "../types";
|
||||
import { setupHelpCommand, clearCommand, rollCommand, trackCommand, untrackCommand, listTrackCommand } from "../commands";
|
||||
import {resolvePath} from "../../utils/path";
|
||||
import {loadCSV} from "../../utils/csv-loader";
|
||||
|
||||
const defaultCommands: MdCommanderCommandMap = {
|
||||
help: setupHelpCommand({}),
|
||||
|
|
@ -62,55 +64,49 @@ export function getCommand(name: string): MdCommanderCommand | undefined {
|
|||
* 从 CSV 文件加载命令模板并更新命令定义
|
||||
*/
|
||||
export async function loadCommandTemplatesFromCSV(
|
||||
csvPaths: string | string[],
|
||||
resolvePath: (base: string, path: string) => string,
|
||||
path: string,
|
||||
articlePath: string
|
||||
): Promise<void> {
|
||||
const paths = Array.isArray(csvPaths) ? csvPaths : [csvPaths];
|
||||
try {
|
||||
const csv = await loadCSV<CommandTemplateRow>(resolvePath(articlePath, path));
|
||||
|
||||
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;
|
||||
|
||||
// 按命令分组模板
|
||||
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);
|
||||
if (!templatesByCommand.has(row.command)) {
|
||||
templatesByCommand.set(row.command, []);
|
||||
}
|
||||
|
||||
// 为每个命令添加模板
|
||||
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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue