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]",
|
description: "Emmet 格式的追踪项目:tag#class1.class2[attr=value]",
|
||||||
type: "string",
|
type: "string",
|
||||||
required: true,
|
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) => {
|
handler: (args, commands) => {
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,15 @@ import type {
|
||||||
MdCommanderCommandMap,
|
MdCommanderCommandMap,
|
||||||
} from "../types";
|
} from "../types";
|
||||||
import { parseInput, getCompletions } from "./completions";
|
import { parseInput, getCompletions } from "./completions";
|
||||||
|
import {
|
||||||
|
addTrackerItem,
|
||||||
|
getTrackerHistory,
|
||||||
|
getTrackerItems,
|
||||||
|
moveTrackerItem, removeTrackerClass,
|
||||||
|
removeTrackerItem,
|
||||||
|
updateTrackerAttribute,
|
||||||
|
updateTrackerClasses
|
||||||
|
} from "../stores";
|
||||||
|
|
||||||
// ==================== Commander Hook ====================
|
// ==================== Commander Hook ====================
|
||||||
|
|
||||||
|
|
@ -196,16 +205,6 @@ export function useCommander(initialCommands?: MdCommanderCommandMap): UseComman
|
||||||
};
|
};
|
||||||
|
|
||||||
// ==================== Tracker 操作 ====================
|
// ==================== Tracker 操作 ====================
|
||||||
const {
|
|
||||||
getTrackerItems,
|
|
||||||
getTrackerHistory,
|
|
||||||
addTrackerItem,
|
|
||||||
removeTrackerItem,
|
|
||||||
updateTrackerAttribute,
|
|
||||||
updateTrackerClasses,
|
|
||||||
moveTrackerItem,
|
|
||||||
removeTrackerClass,
|
|
||||||
} = require("../stores") as typeof import("../stores");
|
|
||||||
|
|
||||||
const getEmmetFromIndex = (index: number): string | null => {
|
const getEmmetFromIndex = (index: number): string | null => {
|
||||||
const items = getTrackerItems();
|
const items = getTrackerItems();
|
||||||
|
|
@ -272,7 +271,7 @@ export function useCommander(initialCommands?: MdCommanderCommandMap): UseComman
|
||||||
updateTrackerClassesByIndex,
|
updateTrackerClassesByIndex,
|
||||||
moveTrackerItem,
|
moveTrackerItem,
|
||||||
moveTrackerItemByIndex,
|
moveTrackerItemByIndex,
|
||||||
removeTrackerItemClass,
|
removeTrackerItemClass: removeTrackerClass,
|
||||||
removeTrackerItemClassByIndex,
|
removeTrackerItemClassByIndex,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,8 @@ import { CommanderEntries } from "./CommanderEntries";
|
||||||
import { TrackerView } from "./TrackerView";
|
import { TrackerView } from "./TrackerView";
|
||||||
import { TabBar } from "./TabBar";
|
import { TabBar } from "./TabBar";
|
||||||
import type { MdCommanderProps } from "./types";
|
import type { MdCommanderProps } from "./types";
|
||||||
import { loadElementSrc, resolvePath } from "../utils/path";
|
import { loadElementSrc } from "../utils/path";
|
||||||
import { initializeCommands, loadCommandTemplatesFromCSV } from "./stores/commandsStore";
|
import { initializeCommands, loadCommandTemplatesFromCSV } from "./stores";
|
||||||
|
|
||||||
customElement<MdCommanderProps>(
|
customElement<MdCommanderProps>(
|
||||||
"md-commander",
|
"md-commander",
|
||||||
|
|
@ -21,15 +21,13 @@ customElement<MdCommanderProps>(
|
||||||
const commander = useCommander(commands);
|
const commander = useCommander(commands);
|
||||||
|
|
||||||
// 加载 CSV 模板
|
// 加载 CSV 模板
|
||||||
createEffect(
|
createEffect( async () => {
|
||||||
on(() => props.commandTemplates, async (csvPaths) => {
|
if (!rawSrc || !rawSrc) return;
|
||||||
if (!csvPaths || !rawSrc) return;
|
await loadCommandTemplatesFromCSV(rawSrc, articlePath);
|
||||||
await loadCommandTemplatesFromCSV(csvPaths, resolvePath, articlePath);
|
|
||||||
// 更新 commander 中的命令(从 store 获取)
|
// 更新 commander 中的命令(从 store 获取)
|
||||||
const { getCommands } = await import("./stores/commandsStore");
|
const {getCommands} = await import("./stores/commandsStore");
|
||||||
commander.setCommands(getCommands());
|
commander.setCommands(getCommands());
|
||||||
}),
|
});
|
||||||
);
|
|
||||||
|
|
||||||
const handleKeyDown = (e: KeyboardEvent) => {
|
const handleKeyDown = (e: KeyboardEvent) => {
|
||||||
if (commander.showCompletions() && commander.completions().length > 0) {
|
if (commander.showCompletions() && commander.completions().length > 0) {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
import { createStore } from "solid-js/store";
|
import { createStore } from "solid-js/store";
|
||||||
import type { MdCommanderCommand, MdCommanderCommandMap } from "../types";
|
import type { MdCommanderCommand, MdCommanderCommandMap } from "../types";
|
||||||
import { setupHelpCommand, clearCommand, rollCommand, trackCommand, untrackCommand, listTrackCommand } from "../commands";
|
import { setupHelpCommand, clearCommand, rollCommand, trackCommand, untrackCommand, listTrackCommand } from "../commands";
|
||||||
|
import {resolvePath} from "../../utils/path";
|
||||||
|
import {loadCSV} from "../../utils/csv-loader";
|
||||||
|
|
||||||
const defaultCommands: MdCommanderCommandMap = {
|
const defaultCommands: MdCommanderCommandMap = {
|
||||||
help: setupHelpCommand({}),
|
help: setupHelpCommand({}),
|
||||||
|
|
@ -62,55 +64,49 @@ export function getCommand(name: string): MdCommanderCommand | undefined {
|
||||||
* 从 CSV 文件加载命令模板并更新命令定义
|
* 从 CSV 文件加载命令模板并更新命令定义
|
||||||
*/
|
*/
|
||||||
export async function loadCommandTemplatesFromCSV(
|
export async function loadCommandTemplatesFromCSV(
|
||||||
csvPaths: string | string[],
|
path: string,
|
||||||
resolvePath: (base: string, path: string) => string,
|
|
||||||
articlePath: string
|
articlePath: string
|
||||||
): Promise<void> {
|
): 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 templatesByCommand = new Map<string, CommandTemplateRow[]>();
|
||||||
const { loadCSV } = await import("../../utils/csv-loader");
|
for (const row of csv) {
|
||||||
const csv = await loadCSV<CommandTemplateRow>(resolvePath(articlePath, path));
|
if (!row.command || !row.label || !row.insertedText) continue;
|
||||||
|
|
||||||
// 按命令分组模板
|
if (!templatesByCommand.has(row.command)) {
|
||||||
const templatesByCommand = new Map<string, CommandTemplateRow[]>();
|
templatesByCommand.set(row.command, []);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 为每个命令添加模板
|
||||||
|
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