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,15 +64,10 @@ 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];
|
|
||||||
|
|
||||||
for (const path of paths) {
|
|
||||||
try {
|
try {
|
||||||
const { loadCSV } = await import("../../utils/csv-loader");
|
|
||||||
const csv = await loadCSV<CommandTemplateRow>(resolvePath(articlePath, path));
|
const csv = await loadCSV<CommandTemplateRow>(resolvePath(articlePath, path));
|
||||||
|
|
||||||
// 按命令分组模板
|
// 按命令分组模板
|
||||||
|
|
@ -111,7 +108,6 @@ export async function loadCommandTemplatesFromCSV(
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn(`Error loading command templates from ${path}:`, error);
|
console.warn(`Error loading command templates from ${path}:`, error);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CommandTemplateRow {
|
interface CommandTemplateRow {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue