feat: ai's attempt

This commit is contained in:
2026-03-01 09:36:09 +08:00
parent 8693b28c75
commit f7eabbdeba
14 changed files with 757 additions and 76 deletions
@@ -0,0 +1,116 @@
import type { MdCommanderCommand, MdCommanderCommandMap, TrackerAttributeType } from "../types";
export const trackCommand: MdCommanderCommand = {
command: "track",
description: "添加一个新的追踪项目",
parameters: [
{
name: "tag",
description: "追踪项目的标签",
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 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 classes: string[] = [];
const classOption = args.options.class;
if (classOption) {
const cls = Array.isArray(classOption) ? classOption : [classOption];
classes.push(...cls);
}
// 通过全局 commander 对象添加追踪项目
const event = new CustomEvent("md-commander-track", {
detail: { tag, classes, attributes },
});
window.dispatchEvent(event);
return {
message: `已添加追踪项目:${tag}`,
type: "success",
};
},
};
export const untrackCommand: MdCommanderCommand = {
command: "untrack",
description: "移除一个追踪项目",
parameters: [
{
name: "id",
description: "追踪项目的 ID",
type: "string",
required: true,
},
],
handler: (args) => {
const id = args.params.id;
if (!id) {
return { message: "错误:缺少 id 参数", type: "error" };
}
const event = new CustomEvent("md-commander-untrack", { detail: { id } });
window.dispatchEvent(event);
return { message: `已移除追踪项目:${id}`, type: "success" };
},
};
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" };
},
};