feat: details
This commit is contained in:
@@ -1,80 +1,39 @@
|
||||
import type { MdCommanderCommand, MdCommanderCommandMap, TrackerAttributeType } from "../types";
|
||||
import type { MdCommanderCommand, MdCommanderCommandMap } from "../types";
|
||||
import { parseEmmet } from "../utils/emmetParser";
|
||||
import { addTrackerItem, removeTrackerItem, getTrackerItems } from "../stores";
|
||||
|
||||
export const trackCommand: MdCommanderCommand = {
|
||||
command: "track",
|
||||
description: "添加一个新的追踪项目",
|
||||
description: "添加一个新的追踪项目 - 支持 Emmet 语法:track npc#john.dwarf.warrior[hp=4/4 ac=15]",
|
||||
parameters: [
|
||||
{
|
||||
name: "tag",
|
||||
description: "追踪项目的标签",
|
||||
name: "emmet",
|
||||
description: "Emmet 格式的追踪项目:tag#class1.class2[attr=value]",
|
||||
type: "string",
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
options: {
|
||||
class: {
|
||||
option: "class",
|
||||
description: "添加类(可多次使用)",
|
||||
type: "string",
|
||||
required: false,
|
||||
},
|
||||
attr: {
|
||||
option: "attr",
|
||||
description: "添加属性,格式:name:type:value (type: progress/count/string)",
|
||||
type: "string",
|
||||
required: false,
|
||||
},
|
||||
},
|
||||
handler: (args, commands) => {
|
||||
const tag = args.params.tag;
|
||||
if (!tag) {
|
||||
return { message: "错误:缺少 tag 参数", type: "error" };
|
||||
const emmet = args.params.emmet;
|
||||
if (!emmet) {
|
||||
return { message: "错误:缺少 Emmet 参数", type: "error" };
|
||||
}
|
||||
|
||||
// 解析属性
|
||||
const attributes: Record<string, any> = {};
|
||||
const attrOption = args.options.attr;
|
||||
|
||||
if (attrOption) {
|
||||
const attrs = Array.isArray(attrOption) ? attrOption : [attrOption];
|
||||
for (const attrStr of attrs) {
|
||||
const parts = attrStr.split(":");
|
||||
if (parts.length >= 3) {
|
||||
const [name, typeStr, ...valueParts] = parts;
|
||||
const type = typeStr as TrackerAttributeType;
|
||||
const valueStr = valueParts.join(":");
|
||||
|
||||
let value: any;
|
||||
if (type === "progress") {
|
||||
const [x, y] = valueStr.split("/").map(Number);
|
||||
value = { x: x || 0, y: y || 0 };
|
||||
} else if (type === "count") {
|
||||
value = parseInt(valueStr) || 0;
|
||||
} else {
|
||||
value = valueStr;
|
||||
}
|
||||
|
||||
attributes[name] = { name, type, value };
|
||||
}
|
||||
}
|
||||
const parsed = parseEmmet(emmet);
|
||||
|
||||
if (!parsed.tag) {
|
||||
return { message: "错误:缺少 tag", type: "error" };
|
||||
}
|
||||
|
||||
// 解析类
|
||||
const classes: string[] = [];
|
||||
const classOption = args.options.class;
|
||||
if (classOption) {
|
||||
const cls = Array.isArray(classOption) ? classOption : [classOption];
|
||||
classes.push(...cls);
|
||||
}
|
||||
// 直接添加追踪项目
|
||||
addTrackerItem(parsed);
|
||||
|
||||
// 通过全局 commander 对象添加追踪项目
|
||||
const event = new CustomEvent("md-commander-track", {
|
||||
detail: { tag, classes, attributes },
|
||||
});
|
||||
window.dispatchEvent(event);
|
||||
const classStr = parsed.classes.length > 0 ? `.${parsed.classes.join(".")}` : "";
|
||||
const attrCount = Object.keys(parsed.attributes).length;
|
||||
const attrStr = attrCount > 0 ? `[${attrCount} 个属性]` : "";
|
||||
|
||||
return {
|
||||
message: `已添加追踪项目:${tag}`,
|
||||
message: `已添加追踪项目:${parsed.tag}${classStr}${attrStr}`,
|
||||
type: "success",
|
||||
};
|
||||
},
|
||||
@@ -97,8 +56,7 @@ export const untrackCommand: MdCommanderCommand = {
|
||||
return { message: "错误:缺少 id 参数", type: "error" };
|
||||
}
|
||||
|
||||
const event = new CustomEvent("md-commander-untrack", { detail: { id } });
|
||||
window.dispatchEvent(event);
|
||||
removeTrackerItem(id);
|
||||
|
||||
return { message: `已移除追踪项目:${id}`, type: "success" };
|
||||
},
|
||||
@@ -108,9 +66,12 @@ export const listTrackCommand: MdCommanderCommand = {
|
||||
command: "list",
|
||||
description: "列出所有追踪项目",
|
||||
handler: (args, commands) => {
|
||||
const event = new CustomEvent("md-commander-list-track");
|
||||
window.dispatchEvent(event);
|
||||
|
||||
return { message: "追踪列表已更新", type: "info" };
|
||||
const items = getTrackerItems();
|
||||
if (items.length === 0) {
|
||||
return { message: "暂无追踪项目", type: "info" };
|
||||
}
|
||||
|
||||
const list = items.map((i) => `${i.tag}${i.classes.length > 0 ? "." + i.classes.join(".") : ""}`).join("\n");
|
||||
return { message: `追踪项目:\n${list}`, type: "info" };
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user