87 lines
2.7 KiB
TypeScript
87 lines
2.7 KiB
TypeScript
import type { MdCommanderCommand } from "../types";
|
|
import { parseEmmet } from "../utils";
|
|
import { addTrackerItem, removeTrackerItem, getTrackerItems, findTrackerItem } from "../stores";
|
|
|
|
export const trackCommand: MdCommanderCommand = {
|
|
command: "track",
|
|
description: "添加一个新的追踪项目 - 支持 Emmet 语法:track npc#john.dwarf.warrior[hp=4/4 ac=15]",
|
|
parameters: [
|
|
{
|
|
name: "emmet",
|
|
description: "Emmet 格式的追踪项目:tag#class1.class2[attr=value]",
|
|
type: "string",
|
|
required: true,
|
|
},
|
|
],
|
|
handler: (args, commands) => {
|
|
const emmet = args.params.emmet;
|
|
if (!emmet) {
|
|
return { message: "错误:缺少 Emmet 参数", type: "error" };
|
|
}
|
|
|
|
const parsed = parseEmmet(emmet);
|
|
|
|
if (!parsed.tag) {
|
|
return { message: "错误:缺少 tag", type: "error" };
|
|
}
|
|
|
|
// 直接添加追踪项目
|
|
addTrackerItem(parsed);
|
|
|
|
const idStr = parsed.id ? ` #${parsed.id}` : "";
|
|
const classStr = parsed.classes.length > 0 ? `.${parsed.classes.join(".")}` : "";
|
|
const attrCount = Object.keys(parsed.attributes).length;
|
|
const attrStr = attrCount > 0 ? `[${attrCount} 个属性]` : "";
|
|
|
|
return {
|
|
message: `已添加追踪项目:${parsed.tag}${idStr}${classStr}${attrStr}`,
|
|
type: "success",
|
|
};
|
|
},
|
|
};
|
|
|
|
export const untrackCommand: MdCommanderCommand = {
|
|
command: "untrack",
|
|
description: "移除一个追踪项目 - 支持 Emmet 语法:untrack #g2 或 untrack goblin.warrior",
|
|
parameters: [
|
|
{
|
|
name: "emmet",
|
|
description: "Emmet 格式:#id 或 tag.class",
|
|
type: "string",
|
|
required: true,
|
|
},
|
|
],
|
|
handler: (args) => {
|
|
const emmet = args.params.emmet;
|
|
if (!emmet) {
|
|
return { message: "错误:缺少 Emmet 参数", type: "error" };
|
|
}
|
|
|
|
const found = findTrackerItem(emmet);
|
|
if (!found) {
|
|
return { message: `未找到匹配的追踪项目:${emmet}`, type: "error" };
|
|
}
|
|
|
|
const success = removeTrackerItem(emmet);
|
|
if (!success) {
|
|
return { message: `移除失败:${emmet}`, type: "error" };
|
|
}
|
|
|
|
return { message: `已移除追踪项目:${found.tag}${found.id ? '#' + found.id : ''}`, type: "success" };
|
|
},
|
|
};
|
|
|
|
export const listTrackCommand: MdCommanderCommand = {
|
|
command: "list",
|
|
description: "列出所有追踪项目",
|
|
handler: (args, commands) => {
|
|
const items = getTrackerItems();
|
|
if (items.length === 0) {
|
|
return { message: "暂无追踪项目", type: "info" };
|
|
}
|
|
|
|
const list = items.map((i) => `${i.tag}${i.id ? "#" + i.id : ""}${i.classes.length > 0 ? "." + i.classes.join(".") : ""}`).join("\n");
|
|
return { message: `追踪项目:\n${list}`, type: "info" };
|
|
},
|
|
};
|